简体   繁体   English

画布动画

[英]Animation with canvas

I need to create animation with circle. 我需要用圆圈创建动画。 When we see a block with a circle, then the animation starts to fill the boundary of the circle and stops in a certain position. 当我们看到带有圆圈的块时,动画将开始填充圆圈的边界并停止在某个位置。 Interesting how to do this animation on native js. 有趣的如何在本机js上执行此动画。 enter image description here 在此处输入图片说明

The best option is to call window.requestAnimationFrame , which calls your render function. 最好的选择是调用window.requestAnimationFrame ,它会调用渲染函数。 Within your render function you have to measure the elapsed time since your last animation frame (and calculate your new angle), redraw the animation and call requestAnimationFrame. 在渲染函数中,您必须测量自上一个动画帧以来的经过时间(并计算新角度),重新绘制动画并调用requestAnimationFrame。

I quickly made a sample at CodePen : 我很快在CodePen中制作了一个样本:

function draw(angle) { ... }

var duration = 3000; // duration of the animation in ms
var max_angle = 3 / 2 * Math.PI; // maximum animation angle

var angle = 0; // current animation angle
var lastRendered = performance.now();

function render(timestamp) {
  angle += (timestamp - lastRendered) * 2 * Math.PI / duration; // calculate new angle
  if (angle > max_angle) angle = max_angle;
  lastRendered = timestamp;
  draw(angle);
  if (angle < max_angle) requestAnimationFrame(render);
}

requestAnimationFrame(render);

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

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