好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mxGraph居中的方法

mxGraph自带了zoom相关 方法 ,比如zoomIn和zoomOut,一级zoomTo。但有点遗憾的是没有居中的一个选项,虽然graph.fit()可以适应画布,但怎么看都别扭,于是写了一个原型居中画布的 方法

Code:

20 mxGraph.prototype.zoomToCenter = function(margin){ var bounds = this.getGraphBounds(); margin = margin || 10; this.container.style.overflow = "hidden"; this.view.setTranslate( -bounds.x -(bounds.width - this.container.clientWidth)/ 2, -bounds.y - (bounds.height - this.container.clientHeight) / 2 ); while( (bounds.width + margin * 2) > this.container.clientWidth || (bounds.height + margin * 2) > this.container.clientHeight ){ this.zoomOut(); bounds = this.getGraphBounds(); } this.container.style.overflow = "auto"; };

参数:

margin 可选,边距,默认10

用法:

 graph.zoomToCenter(); // 默认10边距 graph.zoomToCenter(30); // 30边距

其实主要是利用graph.getGraphBounds()与graph.view.setTranslate()方法居中,然后来个while循环,如果内容宽高比container大就zoomOut()缩小,直到内容小于container的宽高为止。

查看更多关于mxGraph居中的方法的详细内容...

  阅读:112次