简体   繁体   English

svg.js bbox()在scale()之后返回相同的值

[英]svg.js bbox() returns the same values after scale()

var SVGElement = SVG('elementId');
var group = SVGElement.group().path('M50,49.67a18.5,18.5,0,1,0-18.5-18.5A18.52,18.52,0,0,0,50,49.67Zm0-31a12.5,12.5,0,1,1-12.5,12.5A12.51,12.51,0,0,1,50,18.67Z')

bbox_beforeScale = group.bbox()
group.scale(2)
bbox_afterScale = group.bbox()

bbox_beforeScale == bbox_afterScale // true

BBox function call not calculating updated position, height and width BBox函数调用未计算更新的位置,高度和宽度

To expand on Robert's comments, the SVG getBBox() method - which svg.js will be using under the hood of bbox() - returns the bounding box of the element. 为了扩展罗伯特的评论,SVG getBBox()方法-svg.js将在bbox()bbox()使用-返回元素的边界框。 But it disregards any transform on that element. 但是它忽略了对该元素的任何transform

To get the bounding box after the transform, you would need to wrap the element in a group, and call getBBox() ( bbox() ) on that. 要在转换获得边界框,您需要将元素包装在一个组中,然后在该组上调用getBBox()bbox() )。

In your case, you are already wrapping the path in a group. 就您而言,您已经将路径包装在一个组中。 But the thing you are calling group is actually the path, not the group. 但是,您所说的group实际上是路径,而不是组。

Try something like this: 尝试这样的事情:

var group = SVGElement.group();
var path = group.path(..);

bbox_beforeScale = group.bbox();
path.scale(2);
bbox_afterScale = group.bbox();

 var SVGElement = SVG('elementId'); var group = SVGElement.group(); var path = group.path('M50,49.67a18.5,18.5,0,1,0-18.5-18.5A18.52,18.52,0,0,0,50,49.67Zm0-31a12.5,12.5,0,1,1-12.5,12.5A12.51,12.51,0,0,1,50,18.67Z'); bbox_beforeScale = group.bbox(); path.scale(2); bbox_afterScale = group.bbox(); // draw bbox SVGElement.rect(bbox_afterScale.width, bbox_afterScale.height).addClass('box').move(bbox_afterScale.x, bbox_afterScale.y); 
 path { fill: #f06; } .box { fill: none; strokeWidth: 1; stroke: green; } 
 <script src="https://cdn.rawgit.com/svgdotjs/svg.js/master/dist/svg.min.js"></script> <div id="elementId"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM