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