1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
只需使用一個(gè)變量來引用當(dāng)前渲染函數(shù)并在計(jì)時(shí)器事件上更改該變量。
例子
var currentFrameRender = animate; // set current render
requestAnimationFrame(currentFrameRender); // request first frame
setTimeout(() => currentFrameRender = update ,5000); // switch render in 5s
setTimeout(() => currentFrameRender = undefined ,10000); // stop animation in 10s
function animate(){
c.clearRect(0, 0, c.canvas.width, c.canvas.height);
for(let i = 0; i < circles.length; i++){
circleArray[i].scatter();
}
// request frame only if currentFrameRender is defined
currentFrameRender && requestAnimationFrame(currentFrameRender);
}
function update(){
c.clearRect(0, 0, c.canvas.width, c.canvas.height);
for(let i = 0; i < circles.length; i++){
circleArray[i].update();
}
// request frame only if currentFrameRender is defined
currentFrameRender && requestAnimationFrame(currentFrameRender);
}
添加回答
舉報(bào)