我正在使用SVG.js绘制形状。形状之一必须是带有半径线的圆,这样才能得到该行为,我在“ g”元素内部使用了椭圆和线。问题是,每当我引用该g元素并尝试获取bbox时,我总是会得到一个像这样的值:{ x: 0, y: 0, width: widthOfBox, height: heightOfBox, cx: widthOfBox/2, cy: heightOfBox/2}x和y不应为0。下面是有问题的元素,显然不是(0,0),但似乎在报告它是。<g id="MLG1008" stroke="#000000" stroke-width="2" fill="#000000" transform="translate(100 200)" width="993.3559281546569" height="993.3559281546569" x="12.963409423828125" y="-290.0365905761719"> <ellipse id="MLEllipse1009" rx="496.67796407732845" ry="496.67796407732845" cx="496.67796407732845" cy="496.67796407732845" stroke="#000000" stroke-width="2"></ellipse> <line id="MLLine1010" x1="496.67796407732845" y1="496.67796407732845" x2="801.6779640773284" y2="888.6779640773284" stroke="#000000" stroke-width="2"></line></g>有没有一种方法可以定义bbox应该具有的值?还是设置bbox()所看值的方法? 最佳答案 来自W3 specs on getBBox  SVGRect getBBox()      在所有包含的图形元素的几何图形上返回当前用户空间(即,如果应用了”属性(如果有)之后的紧密边界框,不包括描边,剪切,蒙版和滤镜效果)。 [...]重点矿如您所见,getBBox返回的SVGRect是针对包含的图形元素计算的,这意味着您在<g>元素上应用的转换在此SVGRect中将不予考虑。应用于元素本身,而不应用于其图形内容。您可以通过将转换后的x元素包装在另一个y中来获取这些<g>和<g>值,并从中调用其getBBox()方法,console.log(document.getElementById("wrapper").getBBox());<svg viewBox="0 0 1500 1500" width="300" height="300"> <g id="wrapper"> <g id="MLG1008" stroke="#000000" stroke-width="2" fill="#000000" transform="translate(100 200)"> <ellipse id="MLEllipse1009" rx="496.67796407732845" ry="496.67796407732845" cx="496.67796407732845" cy="496.67796407732845" stroke="#000000" stroke-width="2"></ellipse> <line id="MLLine1010" x1="496.67796407732845" y1="496.67796407732845" x2="801.6779640773284" y2="888.6779640773284" stroke="#000000" stroke-width="2"></line> </g> </g></svg>甚至在父<svg>上(如果其所有图形元素都在此<g>内):console.log(document.querySelector("svg").getBBox());<svg viewBox="0 0 1500 1500" width="300" height="300"> <g id="MLG1008" stroke="#000000" stroke-width="2" fill="#000000" transform="translate(100 200)"> <ellipse id="MLEllipse1009" rx="496.67796407732845" ry="496.67796407732845" cx="496.67796407732845" cy="496.67796407732845" stroke="#000000" stroke-width="2"></ellipse> <line id="MLLine1010" x1="496.67796407732845" y1="496.67796407732845" x2="801.6779640773284" y2="888.6779640773284" stroke="#000000" stroke-width="2"></line> </g></svg>另外,即使与您的问题无关,请注意<g>元素没有width,height,x或y属性。
09-07 15:49