Find center of svg shape

送分小仙女□ 提交于 2019-12-10 17:46:21

问题


Im using svgjs to create my shapes. How can I find the center point of an svg shape and add an element there? In my case a red dot. I can't find any information in the documentation for a method or something that helps in this situation.


回答1:


You can use the method getBBox or alternatively getBoundingClientRect. Both methods give you an object with the properties x, y, width, height, which you can use to calculate your center.

Since you tagged your question with svg.js I will also give you a solution with svg.js.

// Vanilla
node = document.getElementById('myEl').getBBox()

// with svg.js
SVG.adopt(document.getElementById('myEl')).bbox()

The second solution gives you back an object which has cx and cy already calculated for you.

This method can be found in the docs



来源:https://stackoverflow.com/questions/40268326/find-center-of-svg-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!