【问题标题】:How to rotate a object after the animation is completed using javascript?使用javascript完成动画后如何旋转对象?
【发布时间】:2020-07-17 05:24:31
【问题描述】:

我正在尝试在完成花瓣后旋转一朵花。 如何仅对花朵实现缓慢的连续旋转效果? 我进行了很多搜索,所有搜索都会导致每次清除框架并重绘。

下面是我试过的代码。

const canvas = document.getElementById("flower");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var a = 0,n = 0,r = 0,c = 6;
var x = 0, y = 0;
var w = window.innerWidth, h = window.innerHeight;
var cx = w/2, cy = h/2, rot = 1;

//draw stem
ctx.beginPath();
ctx.moveTo(cx, h);
ctx.lineWidth = 10;
ctx.strokeStyle = "darkgreen";
ctx.quadraticCurveTo(cx+250, cy+100, cx, cy);
ctx.stroke();
ctx.lineWidth = 1;

//start rotating
const startRotate = () =>{
  setInterval(function () {
    ctx.save();
    ctx.translate(cx, cy);
    ctx.rotate(Math.PI / 180 * (rot += 5));
    ctx.restore(); 
  }, 100);
}

const startDrawing = () =>{
  if(n < 50){   //draw bud
    a =  (n * 137.5)*(180/Math.PI);
    r = c * Math.sqrt(n);
    x = r * Math.cos(a*(180/Math.PI)) + cx;
    y = r * Math.sin(a*(180/Math.PI)) + cy;
    ctx.beginPath();
    ctx.arc(x, y, 6, 0, 2 * Math.PI, true);
    ctx.strokeStyle = "lime";
    ctx.fillStyle = 'yellow';
    ctx.fill();
    ctx.stroke();
  }else{ //draw petals
    a =  (n * 137.5)*(180/Math.PI);
    r = c * Math.sqrt(n);
    x = r * Math.cos(a*(180/Math.PI)) + cx;
    y = r * Math.sin(a*(180/Math.PI)) + cy;
    ctx.beginPath();
    ctx.arc(x, y, 6, 0, 2 * Math.PI, true);
    ctx.strokeStyle = "black";
    ctx.fillStyle = 'hsl('+ a%256 +',100%,50%)';
    ctx.fill();
    ctx.stroke();
  }
  n++;
  if(n < 300) requestAnimationFrame(startDrawing);
  else startRotate();
}
startDrawing();
<canvas id="flower"></canvas>
<script src="main.js"></script>

谢谢

【问题讨论】:

    标签: javascript html css canvas


    【解决方案1】:

    这是我建议你做的事情......
    重构您的代码,使每个圆圈都是列表中的一个对象,然后我们清除画布并重新绘制每一帧上的所有内容。

    请看下面的例子:

    class circle {
      constructor(n) {
        let a = (n * 137.5) * (180 / Math.PI);
        let r = 6 * Math.sqrt(n);
        this.x = r * Math.cos(a * (180 / Math.PI));
        this.y = r * Math.sin(a * (180 / Math.PI));
        this.radius = 6
        this.id = n
        if (n < 50) {
          this.strokeStyle = "lime";
          this.fillStyle = 'yellow';
        } else {
          this.strokeStyle = "black";
          this.fillStyle = 'hsl(' + a % 256 + ',100%,50%)';
        }
      }
    
      draw(ctx) {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, true);
        ctx.strokeStyle = this.strokeStyle;
        ctx.fillStyle = this.fillStyle;
        ctx.fill();
        ctx.stroke();
      }
    }
    
    const canvas = document.getElementById("flower");
    const ctx = canvas.getContext("2d");
    var w = canvas.width = window.innerWidth;
    var h = canvas.height = window.innerHeight;
    ctx.translate(w / 2, h / 2);
    var circles = []
    var n = 0;
    
    const startRotate = () => {  
      setInterval(function() {
        ctx.clearRect(-w, -h, w*2, h*2);
        ctx.rotate(0.1)
        circles.forEach(c => { 
          c.draw(ctx);
          if (c.id > 160) {
            c.radius += Math.random() - 0.51;
            c.radius = Math.min(Math.max(c.radius, 2), 10)
          }
        });
      }, 100);
    }
    
    const startDrawing = () => {
      circles.push(new circle(n))
      ctx.clearRect(-w, -h, w*2, h*2);
      circles.forEach(c => { c.draw(ctx) });
      n++;
      
      if (n < 300) 
        requestAnimationFrame(startDrawing);
      else 
        startRotate();
    }
    
    startDrawing();
    &lt;canvas id="flower"&gt;&lt;/canvas&gt;

    有了它,现在您可以控制每个圆圈并创建更复杂的动画,例如让花朵像打开或闭合一样膨胀或收缩,或者像它正在生长一样向上移动。

    【讨论】:

    • 非常感谢?。只是有一个疑问,是否可以使用下面的杆进行旋转?如果有茎就太好了。
    • 当然可以……但我不会为你编写代码。如果我们为你做这件事,那现在是你的功课,你什么也学不到
    • 谢谢你,赫尔德
    • 非常欢迎您@Ashik...我添加了一些随机动画,以便您可以看到动画可以做什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2019-01-24
    • 2014-07-11
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多