简体   繁体   English

如何从SVG路径获取边界坐标

[英]How to get bounding coordinates from an SVG path

I'm working on a project that requires bounding draggable elements inside complex shapes, defined ideally by SVG Path elements, using Javascript. 我正在一个需要将可拖动元素限制在复杂形状内的项目上,该形状最好由SVG Path元素使用Javascript定义。

I'm open to other ways of defining the bounds but SVG would tie in better with the source material. 我对定义边界的其他方法持开放态度,但是SVG可以更好地与源材料结合在一起。

I've tried using pathSegList however this returns undefined, and I've read it's deprecated. 我尝试使用pathSegList,但是此方法返回undefined,并且我已阅读它已弃用的情况。

How would I go about getting a list of coordinates from an SVG path element, which I could translate to X/Y coordinates for Javascript? 我将如何从SVG路径元素获取坐标列表,可以将其转换为Javascript的X / Y坐标?

You can use the .getBBox() method on the path element. 您可以在path元素上使用.getBBox()方法。 Take a look at the snippet: 看一下代码片段:

 console.log(document.getElementById('mypath').getBBox()); 
 <svg width="300" height="100" viewBox="0 0 300 100" style="background:#efefef"> <path id="mypath" d="M20,20 L40,20 40,40, 20,40 Z" fill="red" /> </svg> 

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

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