首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >requestAnimationFrame太快了

requestAnimationFrame太快了
EN

Stack Overflow用户
提问于 2017-05-25 06:00:08
回答 1查看 1.3K关注 0票数 2

我在画布上制作简单的动画。我使用请求动画框架来控制动画。有三个圆圈。但我只能看到3圈,动画太快了。我的问题是如何减缓我的动画和如何显示每一帧。这是我的生活链接

代码语言:javascript
复制
const swing = (time) => {
  for(var i = 0; i < 3; i++) {
    circle[i] = new ball();
    circle[i].draw(i, circle[i].color);
  }

  requestAnimationFrame(swing);
}

requestAnimationFrame(swing);
//swing();


function ball(i){
    this.x = random(100, 150);
    this.y = random(40, 60);
    this.radius = 45;
    this.color = getRandomColor(random(1, 30));
    this.strokeText = "m"  

    ctx.clearRect(0, 0, el.width, el.height);
    this.draw = function(i, color){
        ctx.beginPath();
        ctx.font="30px Verdana";
        ctx.arc(i*this.x, this.y, this.radius, 0, 2*Math.PI, false);
        ctx.fillStyle = color;
        ctx.globalAlpha = 0.5;
        ctx.strokeText(i,i*this.x,this.y);
        ctx.fill();
        ctx.closePath();
    }
}

提前谢谢。

编辑:-我正在创建类似的东西:- http://codepen.io/jscottsmith/pen/oWyxjp?editors=1010

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-25 07:00:36

只是一个简单的例子,让三个球做一些圆形的运动:

代码语言:javascript
复制
// refer below 
// http://codepen.io/jscottsmith/pen/oWyxjp?editors=1010
const el = document.getElementById('canvas'),
      ctx = el.getContext('2d');
      let circle = [];
el.width = document.body.clientWidth;
el.height = document.body.clientHeight;

const getRandomColor = (i) => {
  let count = 30,
      color = 1,
      hue = (i / count * color) * 360;
  return `hsla(${hue}, 100%, 50%, 1)`
}

for (var i = 0; i < 3; i++) {
  circle[i] = new ball();
}

let angle = 0;
let speed = 0.02;
  
const swing = (time) => {
  ctx.clearRect(0, 0, el.width, el.height);
  
  for (var i = 0; i < 3; i++) {
    circle[i].x = circle[i].x + Math.cos(angle) * 1;
    circle[i].y = circle[i].y + Math.sin(angle) * 2;
  }
  for (var i = 0; i < 3; i++) {  
    circle[i].draw(i, circle[i].color);
  }
  angle += speed;
  requestAnimationFrame(swing);
}

requestAnimationFrame(swing);
//swing();


function ball(i){
    this.x = random(100, 150);
    this.y = random(40, 60);
    this.radius = 45;
    this.color = getRandomColor(random(1, 30));
    this.strokeText = "m"  
  
    this.draw = function(i, color){
        ctx.beginPath();
        ctx.font="30px Verdana";
        ctx.arc(i*this.x, this.y, this.radius, 0, 2*Math.PI, false);
        ctx.fillStyle = color;
        ctx.globalAlpha = 0.5;
        ctx.strokeText(i,i*this.x,this.y);
        ctx.fill();
        ctx.closePath();
    }
}

function random (num1, num2) {
    var max = Math.max(num1, num2);
    var min = Math.min(num1, num2);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
代码语言:javascript
复制
#canvas {
  width: 600px;
  height: 600px;
}
代码语言:javascript
复制
<canvas id="canvas"></canvas>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44173347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档