第一种方式:
使用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);