我正在研究模擬引力的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