【发布时间】:2012-12-21 07:36:36
【问题描述】:
我最近开始做一些 HTML5/Canvas 的东西,并且非常愉快地开展我的业务,在 Chrome 中测试东西,直到我决定尝试我在 Firefox 中所做的工作......效果不太好。
这是我正在做的那种事情的一个简单的例子。设置基本的 requestAnimationFrame 垫片,主循环清除画布,然后更新并绘制我的对象。很简单,关于这些东西的例子随处可见。
function loop() {
canvas.width = canvas.width;
requestAnimFrame(loop);
rR.update();
rG.update();
rB.update();
rY.update();
rR.draw();
rG.draw();
rB.draw();
rY.draw();
}
function Rect(color, x, y, speedX, speedY) {
this.x = x;
this.y = y;
this.color = color;
this.speedX = speedX;
this.speedY = speedY;
}
Rect.prototype.draw = function () {
context.fillStyle = this.color;
context.beginPath();
context.rect(this.x, this.y, 10, 10);
context.closePath();
context.fill();
};
Rect.prototype.update = function () {
if (this.x < 0 || this.x > canvas.width) this.speedX = -this.speedX;
if (this.y < 0 || this.y > canvas.height) this.speedY = -this.speedY;
this.x += this.speedX;
this.y += this.speedY;
};
var rR = new Rect("#FF0000", canvas.width/2, canvas.height/2, 2, 2);
var rG = new Rect("#00FF00", canvas.width/2, canvas.height/2, -2, -2);
var rB = new Rect("#0000FF", canvas.width/2, canvas.height/2, 2, -2);
var rY = new Rect("#FFFF00", canvas.width/2, canvas.height/2, -2, 2);
http://jsfiddle.net/Polaris666/psDM9/3/
当我在 Chrome 中测试时,它看起来很棒,但 Firefox 有很多卡顿和撕裂,这似乎是一项相当简单的任务。
我发现了类似的问题,但没有一个明确的解决方案。这是火狐的事情吗? Webkit 浏览器在这方面做得更好吗?我应该放弃它并希望它在未来版本的浏览器中得到修复吗?或者也许这是我的特殊设置?我正在使用 Windows 7 64 位和 FireFox 17.0.1。
感谢任何帮助。
【问题讨论】:
-
这(小提琴)似乎在我的 Firefox 中运行良好。您最后一次清除浏览器的缓存/历史记录是什么时候,您是否有任何其他选项卡正在运行?根据我的经验,FF 可能会“陷入困境”。
-
没有其他选项卡在运行,我什至禁用了每个插件和扩展。我想这与我的特定设置有关,我可以忍受。这很烦人,但我可以忍受:P
-
我注意到,在我的桌面上,当我打开大量标签时,FF 会每 2 秒“卡顿”一次,这在播放视频时尤其明显。也许 FF 每隔一段时间就会渲染一次,从而导致轻微的延迟。不过都是猜测。
-
我在 FF 20(每晚)中也看到了口吃。在我的演示中,我也注意到 FF 中的画布性能更差(例如 fabricjs.com/particles)
-
在调整 JSFiddle 时,我想我偶然发现了一个更少/没有口吃的版本:jsfiddle.net/hakanensari/K52Gd/2
标签: javascript html firefox animation canvas