简体   繁体   English

使用Javascript进行SVG旋转

[英]SVG rotation using Javascript

I have created an SVG image in an HTML page, and now I want to move the SVG shapes about using JavaScript buttons. 我在HTML页面中创建了一个SVG图像,现在我想使用JavaScript按钮移动SVG形状。 The JSFiddle for my application is here : http://jsfiddle.net/johndavies91/xwMYY/ 我的应用程序的JSFiddle在这里: http//jsfiddle.net/johndavies91/xwMYY/

The buttons have been displayed but their functions aren't being displayed upon clicking the buttons. 已显示按钮,但单击按钮时不显示其功能。

The functions' codes are as follows; 功能代码如下;

function rotatex() {
document.getElementById("inner-arrow").setRotate(45,NativeCenterX,NativeCenterY)
}
function rotatey() {
document.getElementById("middle-arrow").setRotate(45,NativeCenterX,NativeCenterY)
}
function rotatez() {
document.getElementById("outer-arrow").setRotate(45,NativeCenterX,NativeCenterY)
}

and their corresponding buttons; 和他们相应的按钮;

<input id="button1" type="button" value="Rotate Inner Short Arrows 45*"
        onclick="rotatex()"/>
<input id="button1" type="button" value="Rotate Middle Short Arrows 45*"
        onclick="rotatey()"/>
<input id="button1" type="button" value="Rotate Outer Short Arrows 45*"
        onclick="rotatez()"/>

I am trying to rotate them around the shapes' origin. 我试图围绕形状的原点旋转它们。 Could someone explain to me why this code isn't working. 有人可以向我解释为什么这段代码不起作用。 Thanks 谢谢

See updated fiddle here: http://jsfiddle.net/2da4B/ 请看这里更新的小提琴: http//jsfiddle.net/2da4B/

This uses .setAttribute("transform", "rotate(45)") instead of setRotate : 这使用.setAttribute("transform", "rotate(45)")而不是setRotate

function rotatex() {
    var innerArrow = document.getElementById("inner-arrow");
    innerArrow.setAttribute("transform", "rotate(45)");
}

This rotates 45 degrees around the 0,0 origin - it's not clear from your question whether that's what you're looking for. 它围绕0,0原点旋转45度 - 从你的问题中不清楚这是否是你正在寻找的。

尝试通过用_ (下划线)替换- (破折号)来更改id。

I would suggest you to use external library for SVG works. 我建议你使用外部库来进行SVG工作。 For example - Raphaël. 例如 - Raphaël。 .

Here is the link http://raphaeljs.com . 这是链接http://raphaeljs.com With help of this lib you will easily be able to complete your task. 借助此lib,您可以轻松完成任务。

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

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