简体   繁体   English

动画中的相对旋转

[英]A relative rotation in an animation

I have a problem with Raphael.js. 我有Raphael.js的问题。 I want to rotate the "compassScale" - set in the following code - in a relative manner. 我想以相对的方式旋转“compassScale” - 在下面的代码中设置。

This works for the paths, but all the texts "animate" to the absolute rotation of 30 degree. 这适用于路径,但所有文本都“动画”到30度的绝对旋转。 I want them to rotate to the 30 degrees relative from their actual positions. 我希望它们相对于实际位置旋转30度。

var compassScale = paper.set();

var centerX = 200;
var centerY = 200;
var radius = 195;

var compasCircle = paper.circle(centerX, centerY, radius);

for(var i = 0; i < 360; i++) {
    var winkelRad = i * (Math.PI/180)
    var xStart = centerX + Math.sin(winkelRad) * radius;
    var yStart = centerY + Math.cos(winkelRad) * radius;
    var diff = 6;

    if(i % 10 === 0){
        compassScale.push(paper.text(centerX, centerY - radius + 18, i).rotate(i, centerX, centerY, true));
        diff = 12;
    } else if(i % 5 === 0) {
        diff = 8;
    }

    var xEnd = centerX + Math.sin(winkelRad) * (radius - diff);
    var yEnd = centerY + Math.cos(winkelRad) * (radius - diff);

    compassScale.push(paper.path("M" + xStart + " " + yStart + " L" + xEnd + " " + yEnd));
}

compassScale.animate({rotation:"30 " + centerX + " " + centerY}, 5000);

Like you said, the problem is that you're animating all elements to 30 degrees, not their current rotation + 30 degrees. 就像你说的那样,问题是你将所有元素设置为30度,而不是它们当前的旋转+30度。 It's actually quite simple once you think of it that way. 一旦你想到这一点,它实际上非常简单。 Here is your revised code that works: 以下是您修改后的代码:

var compassScale = paper.set();
var texts = []; // array to hold the text elements

var centerX = 200;
var centerY = 200;
var radius = 195;

var compasCircle = paper.circle(centerX, centerY, radius);

for(var i = 0; i < 360; i++) {
    var winkelRad = i * (Math.PI/180)
    var xStart = centerX + Math.sin(winkelRad) * radius;
    var yStart = centerY + Math.cos(winkelRad) * radius;
    var diff = 6;

    if(i % 10 === 0){
        texts.push(paper.text(centerX, centerY - radius + 18, i).rotate(i, centerX, centerY, true));
        diff = 12;
    } else if(i % 5 === 0) {
        diff = 8;
    }

    var xEnd = centerX + Math.sin(winkelRad) * (radius - diff);
    var yEnd = centerY + Math.cos(winkelRad) * (radius - diff);

    compassScale.push(paper.path("M" + xStart + " " + yStart + " L" + xEnd + " " + yEnd));
}

compassScale.animate({rotation:"30 " + centerX + " " + centerY}, 5000);
// loop through the text elements, adjusting their rotation by adding 30 to their individual rotation
for (var i = 0, l = texts.length; i < l; i += 1) {
    // node.attr("rotation") returns something like 50 200 200, so we have to split the string and grab the first number with shift
    texts[i].animate({rotation: (30 + +texts[i].attr("rotation").split(" ").shift()) + " " + centerX + " " + centerY}, 5000);
}

Just a quick observation: 快速观察一下:

Looks like "Rotation" isn't part of the Atrr anymore since ver 2, so you can't use it in "animate", but you can replace that with "transform: "r" + (some degree)".. 看起来“旋转”不再是Atrr的一部分,因为版本2,所以你不能在“动画”中使用它,但你可以用“transform:”r“+(某种程度)”替换它。

eg: 例如:

     element.animate( {transform: "r" + (-90)}, 2000, 'bounce');

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

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