gpt4 book ai didi

html - Firefox 中的 requestAnimationFrame 闪烁问题

转载 作者:搜寻专家 更新时间:2023-10-31 08:47:30 25 4
gpt4 key购买 nike

我正在尝试在 Canvas 上沿对角线方向制作一个圆圈。我正在使用请求动画框架。它在所有浏览器中都能顺利运行,但在 firefox 中却不行。动画进行时不断闪烁。请有人告诉我我做错了什么。我需要在 Firefox 中以至少合理的速度进行流畅的动画。

这是我的简单代码,也许有人可以指出我在这里遗漏了什么......:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style>
/*html { background: #000; }*/
</style>

<title>Test</title>
</head>

<body>
<script type="text/javascript">

window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();

window.cancelRequestAnimFrame = ( function() {
return window.cancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
clearTimeout
} )();

var canvas, context, toggle;
var ctr = 0;
var request;

var x = 10;
var y = 10;


init();
animate();

function init() {

canvas = document.createElement( 'canvas' );
canvas.width = 512;
canvas.height = 512;

context = canvas.getContext( '2d' );

document.body.appendChild( canvas );

}

function animate() {


request = requestAnimFrame( animate );
draw();

}

function draw() {

context.clearRect(0,0,canvas.width,canvas.height);
y+=2;
x+=2;

context.beginPath();
context.arc(x, y, 3, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();
context.lineWidth = 5;
context.strokeStyle = '#003300';
context.stroke();

//cancelRequestAnimFrame(request);

}

</script>

</body>
</html>

最佳答案

您的代码中有两处需要修改:

首先尝试新的requestAnimationFrame polyfill。

(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelRequestAnimationFrame = window[vendors[x]+
'CancelRequestAnimationFrame'];
}

if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};

if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}())

如需深入解释,请阅读这篇文章:http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating

第二是您需要计算一个时间戳与下一个时间戳之间的增量。您正在使用一个固定值,该值在每次渲染迭代中都会增加一个。因为您的浏览器跟不上它可能会跳过一两帧的速度。这样你就可以顺利度过。

这是计算增量时间的方式:

end = Date.now();
delta = ( end - start ) / 1000.0 * 60;
end = start;

http://jsfiddle.net/p5c6c/1/

关于html - Firefox 中的 requestAnimationFrame 闪烁问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14677684/

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