最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - A relative rotation in an animation - Stack Overflow

programmeradmin6浏览0评论

I have a problem with Raphael.js. I want to rotate the "passScale" - set in the following code - in a relative manner.

This works for the paths, but all the texts "animate" to the absolute rotation of 30 degree. I want them to rotate to the 30 degrees relative from their actual positions.

var passScale = paper.set();

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

var pasCircle = 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){
        passScale.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);

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

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

I have a problem with Raphael.js. I want to rotate the "passScale" - set in the following code - in a relative manner.

This works for the paths, but all the texts "animate" to the absolute rotation of 30 degree. I want them to rotate to the 30 degrees relative from their actual positions.

var passScale = paper.set();

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

var pasCircle = 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){
        passScale.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);

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

passScale.animate({rotation:"30 " + centerX + " " + centerY}, 5000);
Share Improve this question edited Oct 2, 2019 at 9:30 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Mar 3, 2011 at 15:49 speznazspeznaz 981 silver badge10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Like you said, the problem is that you're animating all elements to 30 degrees, not their current rotation + 30 degrees. It's actually quite simple once you think of it that way. Here is your revised code that works:

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

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

var pasCircle = 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);

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

passScale.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)"..

eg:

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

评论列表(0)

  1. 暂无评论