【发布时间】:2013-01-18 14:49:56
【问题描述】:
我正在尝试在画布上沿对角线方向制作一个圆圈。我正在使用 requestanimationframe。它在所有浏览器中都能顺利运行,但在 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>
【问题讨论】:
-
我期待一个非常快速的回答这个问题,因为场景非常简单。在Firefox中不可能有流畅的html5画布动画吗?为什么在Firefox中完成它如此困难,我想知道!
-
我在 Mac 上的 Firefox 18 中没有看到这种闪烁。您使用的是哪个版本的 Firefox?
-
我刚刚在 windows pc 上的 Firefox 18 中尝试过。它不光滑。它在 chrome 中完美运行,但在 Firefox 中我可以看到它并不流畅。也许这是 Firefox 能做的最好的事情。我的客户使用 Firefox,所以我必须对这个问题做点什么!在这个例子中,我放置了一个简单的圆圈在画布上移动的场景。我的实际项目在每个循环迭代中都包含图像背景和非课程 javascript 逻辑。在 chrome、IE 和 safari 中都运行良好。任何想法!
标签: html firefox canvas requestanimationframe