我正在研究模拟引力的JavaScript游戏。它使用HTML5 canvas元素为行星绘制2D椭圆。我在谷歌浏览器中测试我的游戏。这里有一个链接到游戏:http://gravitygame.hostingsiteforfree.com/index.php?page=playHTML为什么canvas2d上下文不再填充椭圆?
截至5月24日,它的工作就好了。但是,Chrome从26.0.1410.64升级到27.0.1453.94后,填充的椭圆有时不会绘制。每次我加载我的游戏时都不会发生这种情况,我从来没有在本地运行时让它崩溃。
这里的游戏工作的截图:
这里是一个说明它不填充的椭圆截图:
我不能告诉发生了什么。我将包括绘制所有行星的循环部分。为了便于阅读,我对其进行了修改。
var i = bodies.length;
while(i--){
var I = bodies[i];
var planetRad = (I.width/2)*_scale;
if(_showTrails){
//draw the planet's trail
}
if(//the planet is completely off the screen){
//draw a red planet on the edge of the screen
ctx.beginPath();
ctx.arc(nX, nY, 2.5, 0, TWOPI);
ctx.fillStyle = offScreenColor;
ctx.fill();
ctx.strokeStyle = offScreenOutline;
ctx.stroke();
}
else{
//draw planet
ctx.beginPath();
ctx.arc(nX, nY, (I.width/2)*_scale, 0, TWOPI);
ctx.closePath();
ctx.fillStyle = I.bodyColor;
ctx.fill();
}
if(_showMotionVector){
//draw a line from the center of a planet showing the direction and speed it's travelling
ctx.strokeStyle = motionColor;
ctx.beginPath();
ctx.moveTo(I.getScX(), I.getScY());
ctx.lineTo(I.motion.x * _scale * 12 + I.getScX(), I.motion.y * _scale * 12 + I.getScY());
ctx.stroke();
}
}
为什么它会突然中断?
我遇到了类似的问题与IE浏览器,所以这里试试。如果你添加ctx.closePath();在示例代码中的第一个弧之后? – markE
这没有奏效。每过一段时间,我仍然可以让它加载它,行星不会出现。 – Justin
无法在Chrome或Canary(+其他浏览器)中重现(在FF平移不适用于我btw)。你如何触发动画循环? – K3N