我有一個簡單的畫布測試,我似乎沒有得到它在Firefox上的工作。我真的沒有JS控制檯上的錯誤。版本是17.0.4,稍後將嘗試新版本。下面的代碼:使用Firefox無法在畫布上畫線
<!DOCTYPE HTML>
<HTML>
<HEAD>
<SCRIPT>
var paint = false;
var canvas;
var cntxt;
var top;
var left;
function initialise() {
canvas = document.getElementById("canvas_1");
canvas.addEventListener("mousedown", doMouseDown, false);
canvas.addEventListener("mouseup", doMouseUp, false);
canvas.addEventListener("mousemove", doMouseMove, false);
cntxt = canvas.getContext("2d");
cntxt.strokeStyle = '#ff0000';
cntxt.lineWidth = 5;
cntxt.lineCap = 'round';
rect = canvas.getBoundingClientRect();
top = rect.top;
left = rect.left;
}
function doMouseDown(event) {
paint = true;
x = event.layerX;
y = event.layerY;
document.getElementById("value_x").innerHTML = x;
document.getElementById("value_y").innerHTML = y;
cntxt.moveTo(x-left, y-top);
cntxt.beginPath();
cntxt.stroke();
}
function doMouseUp(event) {
paint = false;
x = event.layerX;
y = event.layerY;
document.getElementById("value_x").innerHTML = x;
document.getElementById("value_y").innerHTML = y;
cntxt.lineTo(x-left+1, y-top+1);
cntxt.stroke();
cntxt.closePath();
}
function doMouseMove(event) {
if(paint) {
x = event.layerX;
y = event.layerY;
document.getElementById("value_x").innerHTML = x;
document.getElementById("value_y").innerHTML = y;
cntxt.lineTo(x-left, y-top);
cntxt.stroke();
}
}
</SCRIPT>
</HEAD>
<BODY onLoad="initialise()">
<canvas width="400" height="400" id="canvas_1">
Canvas Tag not supported
</canvas>
<br>
Value x: <span id="value_x"></span>
<br>
Value y: <span id="value_y"></span>
</BODY>
</HTML>
在MSIE它工作正常(第9版),但Firefox正確顯示的座標在span元素,但沒有繪製線條...我想,使用layerX和layerY應該有做的伎倆,但他們不...
謝謝你和問候
似乎在http://jsfiddle.net/pyHYG/工作得很好,我(雖然v20.0.1)嘗試'控制檯。記錄所有的變量/事件,看他們是否如預期的那樣。 – 2013-04-23 12:46:41
那麼,我能夠找到問題的方式......頂部和左側變量以某種方式映射到其他對象,所以我將它們重命名爲topMargin並leftMargin現在它工作得很好...謝謝你的提示... – Martin 2013-04-23 14:15:17
這就是爲什麼你不應該使用全局變量...;) – 2013-04-23 14:56:57