gpt4 book ai didi

javascript - 如何从 'SetInterval'切换到 'request animation frame'

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:30 24 4
gpt4 key购买 nike

<分区>

我创建了一个简单的 Canvas 动画,听说使用“请求动画帧”比“设置间隔”更好,但我不确定该怎么做?

现在是这样的:

http://jsfiddle.net/rC7zJ/
var width = 48;
var height = 87;
var speed = 100; //ms
var frames = 1; // Total frames - 1, as frame start from 0 not
var currentFrame = 0;

canvas = document.getElementById("CanvasAnimate");
ctx = canvas.getContext("2d");
imageTadPole = new Image()
imageTadPole.src = 'https://dl.dropbox.com/u/19418366/tadpole.png';

function draw(){
ctx.clearRect(0, 0, width, height);
ctx.drawImage(imageTadPole, width * currentFrame, 0, width, height, 0, 0, width, height);

if (currentFrame == frames) {
currentFrame = 0;
} else {
currentFrame++;
}
}

setInterval(draw, speed);

任何帮助将不胜感激!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com