g6使用-解决图形相对于画布完全居中


第一种方式:

   使用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);

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM