我正在尝试使用HTML5画布制作一个Pong克隆版。我想在游戏场地中央绘制一个虚线,就像原版的Pong一样。我通过扩展
如果我让我的游戏运行超过30秒,它会显著减慢,达到无法玩的程度,并且Firefox和Chrome浏览器的CPU使用率都在134%左右。只有在渲染虚线时才会出现缓慢。我不确定发生了什么,但是我还通过Chrome检查器的分析器运行了我的代码,结果如下:
CanvasRenderingContext2D
对象来实现这一点,如David Geary's的优秀著作所示:CanvasRenderingContext2D.prototype.dashedLine = function (x1, y1, x2, y2, dashLength) {
dashLength = dashLength === undefined ? 5 : dashLength;
var deltaX = x2 - x1;
var deltaY = y2 - y1;
var numDashes = Math.floor(
Math.sqrt(deltaX * deltaX + deltaY * deltaY) / dashLength);
for (var i=0; i < numDashes; ++i) {
context[ i % 2 === 0 ? 'moveTo' : 'lineTo' ]
(x1 + (deltaX / numDashes) * i, y1 + (deltaY / numDashes) * i);
}
我接着有一个render()
函数,它实际上是在画布上渲染元素的所有调用。其中包括我的renderBackground()
函数,它着色背景并绘制虚线:
function render() {
ctx.clearRect(0, 0, cWidth, cHeight);
renderBackground();
// Rest removed for brevity
}
function renderBackground() {
ctx.lineWidth = 5;
ctx.strokeStyle = '#FFFFFF';
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, cWidth, cHeight);
ctx.dashedLine(0, 0, 0, cHeight, 10);
ctx.stroke()
}
最后我有一个名为animLoop()
的函数,它实际上调用render()
函数,并利用requestAnimationFrame()
来实现更平滑的动画效果:
function animLoop() {
render();
requestAnimationFrame(animLoop);
}
window.requestAnimationFrame = (function() {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
}
);
})();
如果我让我的游戏运行超过30秒,它会显著减慢,达到无法玩的程度,并且Firefox和Chrome浏览器的CPU使用率都在134%左右。只有在渲染虚线时才会出现缓慢。我不确定发生了什么,但是我还通过Chrome检查器的分析器运行了我的代码,结果如下:
我的renderBackground()
函数只占用了0.46%的CPU时间。而且我不确定(program)
应该表示什么。你有想法是什么导致了速度变慢吗?
另外,您可以在我的Github存储库上看到我目前的完整代码。
ctx.dashedLine
之前尝试放置ctx.beginPath()
。 - Gustavo Carvalho