第一種方式:
使用fitView()方法,圖形自動適配畫布大小,但是會有一個問題,當圖形節點比較少初始化的時候,節點就會放大好幾倍,看起來很不好。
第二種方式:
重寫fitView()方法,將中心點設置到畫布的中心點
const width = graph.get("width");
const height = graph.get("height");
const group = graph.get("group");
group.resetMatrix();
const bbox = group.getCanvasBBox();
if (bbox.width === 0 || bbox.height === 0) return;
const viewCenter = {
x: width / 2,
y: height / 2,
};
const groupCenter = {
x: bbox.x + bbox.width / 2,
y: bbox.y + bbox.height / 2,
};
graph.translate(viewCenter.x - groupCenter.x, viewCenter.y - groupCenter.y);
