admin管理员组文章数量:1302404
I have to rotate three SVG-Objects around its center with JavaScript and not the <animation>
tag. The big star at the top of the tree rotates correctly, but from the others I don't find the center coordinates.
This is the second star and the rotation in JS:
star2.setAttribute("transform", "rotate(" + angle + " 2000 200)");
and this is the svg:
<defs>
<symbol id="stern">
<polygon points="1000 50 1100 310 850 160 1150 160 900 310 1000 50" fill="yellow" />
</symbol>
</defs>
<use id="stern2" xlink:href="#stern" transform="translate(500,600) scale(0.5)" onmouseover="rotate2();" onmouseout="stop();" />
Here is a fiddle:
/
I have to rotate three SVG-Objects around its center with JavaScript and not the <animation>
tag. The big star at the top of the tree rotates correctly, but from the others I don't find the center coordinates.
This is the second star and the rotation in JS:
star2.setAttribute("transform", "rotate(" + angle + " 2000 200)");
and this is the svg:
<defs>
<symbol id="stern">
<polygon points="1000 50 1100 310 850 160 1150 160 900 310 1000 50" fill="yellow" />
</symbol>
</defs>
<use id="stern2" xlink:href="#stern" transform="translate(500,600) scale(0.5)" onmouseover="rotate2();" onmouseout="stop();" />
Here is a fiddle:
http://jsfiddle/F325k/
Share edited Dec 11, 2013 at 10:10 sumisu asked Dec 11, 2013 at 9:40 sumisusumisu 1351 gold badge4 silver badges12 bronze badges 1- Your fiddle is not working. – Paul LeBeau Commented Dec 11, 2013 at 22:18
2 Answers
Reset to default 3to rotate around the center you need to translate it first: Rotate rectangle around its own center in SVG
ps: the latest and reworked lib from the author of Raphaël is http://snapsvg.io/ in case you can use one.
For SVG, definately use third-party libraries. SVG is a pain with plain javascript. My suggestion: Definately go for D3js, I have been using it for a year at my workplace and it is great: dynamic, good munity, brilliant documentation.
See the following example with spinning circle around its center which is called Planetarium: Planetarium Does the rotation without animation as you wanted. It uses a D3- timer and that is all.
There are a lot of examples there, so read the full example list here: d3js Full list of examples
本文标签: javascriptJS with SVG Rotate SVGElement around its centerStack Overflow
版权声明:本文标题:javascript - JS with SVG: Rotate SVG-Element around its center - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741683824a2392326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论