admin管理员组文章数量:1290981
Considering this example with 2 circles (red and blue):
<svg width="500px" height="500px">
<circle cx="100" cy="50" r="40" fill="red" id="redcircle" />
<g transform="translate(200,-20)">
<g transform="scale(2)">
<g transform="rotate(45)">
<g transform="translate(5,10)">
<circle cx="100" cy="50" r="40" fill="blue" id="bluecircle" />
</g>
</g>
</g>
</g>
</svg>
I wonder if there is a way to write a generic function like:
function move(selection){
// ???
}
That would allow to write move("#redcircle")
or move("#bluecircle")
which would visually move the target element 100px to the right (for example).
Considering this example with 2 circles (red and blue):
<svg width="500px" height="500px">
<circle cx="100" cy="50" r="40" fill="red" id="redcircle" />
<g transform="translate(200,-20)">
<g transform="scale(2)">
<g transform="rotate(45)">
<g transform="translate(5,10)">
<circle cx="100" cy="50" r="40" fill="blue" id="bluecircle" />
</g>
</g>
</g>
</g>
</svg>
I wonder if there is a way to write a generic function like:
function move(selection){
// ???
}
That would allow to write move("#redcircle")
or move("#bluecircle")
which would visually move the target element 100px to the right (for example).
2 Answers
Reset to default 8See this jsfiddle
function moveSection(idStr, xOffset, yOffset) {
var domElemnt = document.getElementById(idStr);
if (domElemnt) {
var transformAttr = ' translate(' + xOffset + ',' + yOffset + ')';
domElemnt.setAttribute('transform', transformAttr);
}
}
moveSection("bluecircle", 50, 20);
http://jsfiddle/dKxZt/4/
It uses translate to move the element.
See this demo here to implement drag of an SVG element: Getting the Screen Pixel coordinates of a Rect element
It is the drag and drop SVG with JQuery Hope this link can help http://www.codedread./blog/archives/2005/12/21/how-to-enable-dragging-in-svg/
This is the demo http://www.codedread./dragtest.svg
本文标签: javascriptA generic JS function to move any SVG elementStack Overflow
版权声明:本文标题:javascript - A generic JS function to move any SVG element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741500653a2382038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论