简体   繁体   English

在javascript中顺时针旋转,然后逆时针旋转

[英]clockwise and then anticlockwise rotation in javascript

I want to rotate a canvas element to first do animation of rotation in clockwise direction by 180 degree then do a reverse(anticlockwise) rotation of 180 degree.I have used this code but its not working as i want. 我想旋转画布元素以先按顺时针方向旋转动画180度,然后逆向旋转(逆时针)旋转180度。我已经使用了此代码,但是它无法按我的方式工作。

  function Rotate(arg) { rotateInterval = setInterval(function () { arg.save(); //saves the state of canvas arg.clearRect(0, 0, arg.canvas.width, arg.canvas.height); //clear the canvas arg.translate(arg.canvas.width / 2, arg.canvas.height / 2); //let's translate if (flag2 == false) arg.rotate(-(ang += 5) * Math.PI / 180); //increment the angle and rotate the image //arg.style.rotate(5); else { ang = 0; arg.rotate((ang += 5) * Math.PI / 180); } if (ang == 180) { if (flag2 == true) flag3 = true; else flag2 = true; } if (flag3 == true && ang == 180) { clearInterval(rotateInterval) flag2 = false; } // ctxOne.drawImage(bowl, -ctxOne.canvas.width / 2, -ctxOne.canvas.height / 2, ctxOne.canvas.width, ctxOne.canvas.height); //draw the image ;) arg.drawImage(bowl, -90, -90, 180, 180); arg.restore(); //restore the state of canvas }, 100); } 

Does rotate() rotate an element relative to its current transformation or is it an absolute rotation? 旋转()相对于其当前变换旋转元素还是绝对旋转? Instead of doing 而不是做

    (angle+=5)*Math.PI / 180

try 尝试

    5*Math.PI / 180

for your rotate argument. 为您的旋转参数。 That way rotate will rotate it by an addition 5 degrees every time. 这样旋转将使它每次旋转5度。 And make it -5 when it's going backwards. 并使其向后退时为-5。 I guess at that point you'd use a counter to determine if 180/5 steps have passed and make it time to go backwards. 我猜到那时您将使用一个计数器来确定是否已通过180/5步,并使其有时间倒退。

You have introduced a flag3 but I think you don't need it. 您已经引入了flag3但我认为您不需要它。 I assume that rotation starts with ang = 0 and flag2 shall tell the rotation direction: increment when false and decrement when true. 我假设旋转从ang = 0开始,并且flag2将告诉旋转方向:false时递增,true时递减。 But you always set ang = 0 when it is true. 但是,当它为true时,您总是将ang = 0设置为。 Your code should look like: 您的代码应如下所示:

var rotateInterval, ang = 0, flag2 = false;

function Rotate(arg) {

rotateInterval = setInterval(function () {
    arg.save();
    arg.clearRect(0, 0, arg.canvas.width, arg.canvas.height);
    arg.translate(arg.canvas.width / 2, arg.canvas.height / 2);

    if (flag2 == false) ang += 5; // increment ang if flag2 == false ( = go forward)
    else ang -= 5; // otherwise decrement ang ( = go backwards)

    arg.rotate(ang * Math.PI / 180); // set the new ang to the rotation

    if (ang == 180) flag2 = true; // change direction if 180 deg is reached

    if (flag2 == true && ang == 0) { // if we go backwards and have reached 0 reset
        clearInterval(rotateInterval)
        flag2 = false;
    }

    arg.drawImage(bowl, -90, -90, 180, 180);
    arg.restore(); //restore the state of canvas
}, 100);

}

Now the angle starts with 0, goes stepwise up to 180, then stepwise back to 0, then stops. 现在角度从0开始,逐步上升到180,然后逐步回到0,然后停止。

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

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