Javascript 帧刷新冲突超时和requestAnimationFrame
我在较低的画布上使用:Javascript 帧刷新冲突超时和requestAnimationFrame,javascript,html,canvas,Javascript,Html,Canvas,我在较低的画布上使用: var requestAnimFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame ||
var requestAnimFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000/60);
};
我将其用于较低层上方的画布层:
function DrawSpawnAnimation() {
anim();
}
function anim() {
ctxAnimation.drawImage(spriteSheet, ExplodeFrame * 100, 2740,100,100,explodeX,explodeY,100,100);
if (ExplodeFrame < 5) {
ExplodeFrame++;
setTimeout(anim, 500);
}
//alert("Show current frame of animation"); - this shows the animation works, it just does
// not show it one frame per half second.
}
函数DrawSpawnAnimation(){
动漫();
}
函数anim(){
CTX动画.绘图图像(spriteSheet,ExplodeFrame*100274010100,explodeX,explodeY,100100);
如果(帧<5){
框架++;
设置超时(动画,500);
}
//警报(“显示动画的当前帧”);-这显示动画工作,它只是工作
//不显示每半秒一帧。
}
我的问题是,动画在屏幕上闪烁一毫秒。下面的画布刷新是否弄乱了它?以下是如何使用1个RAF ticker为2张画布设置动画。 英国皇家空军的计时器每秒将发射60次 顶部画布将每秒设置30倍的动画(当TopFrameCount降至0时,每2帧设置一次) 底部画布将每秒设置2次动画(当BottomFrameCount降至0时,每30帧设置一次动画)
var-RAFfps=60;
var-TopFPS=30;
var-fps=2;
var topframecont=(RAFfps/TopFPS)-1;
var BottomFrameCount=(RAFfps/BottomFPS)-1;
var=0;
函数绘图(){
setTimeout(函数(){
画框(画);
//如果顶部画布计数器已过期,
//为顶部画布设置动画
如果(-TopFrameCount有没有办法不靠近下画布,只在上画布上运行,让下画布继续运行,就好像什么都没有发生一样?两个画布都需要动画吗?如果需要,那么没有——只有一个RAF计时器。你可以有多个设置超时,但这会使性能倒退一步你真的可以用RAF+计数器处理多个画布动画。因为我有requestAnimFrame(循环);对于我的下画布,只是获取需要更长时间等待的顶层让我感到困惑。顶层是爆炸动画,底层只是玩家移动的fps。用不同的解释编辑了我的答案。这个解释有用吗?
var RAFfps = 60;
var TopFPS=30;
var BottomFPS=2;
var TopFrameCount=(RAFfps/TopFPS)-1;
var BottomFrameCount=(RAFfps/BottomFPS)-1;
var counter2fps=0;
function draw() {
setTimeout(function() {
requestAnimFrame(draw);
// if the top canvas counter has expired,
// animate the top canvas
if(--TopFrameCount<=0){
// put top canvas drawing stuff here
// reset the top counter
TopFrameCount==(RAFfps/TopFPS)-1;
}
// if the bottom canvas counter has expired,
// animate the bottom canvas
if(--BottomFrameCount<=0){
// put bottom canvas drawing stuff here
// reset the top counter
BottomFrameCount=(RAFfps/BottomFPS)-1;
}
}, 1000 / fps);
}