當我添加ctx.addEventListener使得畫布繪製消失
ctx.addEventListener('mousedown', onDown, false);
畫布繪製(背景和形狀)消失,頁面是空白的,然後當我刪除他們又重新出現在代碼此事件偵聽器。只是想知道爲什麼會發生這種情況?在此先感謝
<script>
var ctx, W, H;
var x = 10;
window.onload = function() {
var canvas = document.getElementById("canvas");
W = window.innerWidth;
H = window.innerHeight;
canvas.width = W;
canvas.height = H;
ctx = canvas.getContext("2d");
ctx.addEventListener('mousedown', onDown, false); //When this is here, canvas drawing disappears, when it's not here canvas drawing reappears again
setInterval(draw, 1);
function draw() {
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "#E6E6FF";
ctx.fillRect(0, 0, W, H);
ctx.fillStyle = "black";
ctx.fillRect(x,20,10,10);
ctx.font = "30px Arial";
ctx.fillText("Hello World",10,80);
ctx.fill();
}
}
function onDown(event) {
//where x is found
cx = event.pageX
cy = event.pageY
alert("X,Y ="+cx+','+cy);
}
請檢查控制檯是否有錯誤,注意它會說'ctx.addEventListener不是函數'。 –