0
我想知道用戶是否點擊畫布中繪製的圖片。我點擊圖片,但沒有任何反應。該警報未被調用。最後一個條件永遠不會通過。任何想法?畫布元素:圖片點擊
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="wrapper">
<canvas id="game" height="500" width="700">
</canvas>
</div>
<script>
(function() {
var canvas = document.getElementById('game'),
context = canvas.getContext('2d'),
fps = 1,
character = Image(),
positions = [[125, 55], [480, 55], [125, 185], [480, 182], [125, 315], [480, 315]],
random, x, y, xc, yc = null;
canvas.addEventListener('click', function(event) {
xc = event.screenX - canvas.offsetLeft;
yc = event.screenY - canvas.offsetTop;
if((xc >= x) && (xc <= (x + character.width)) && (yc >= y) && (yc <= (y + character.height))) {
alert('X = ' + x + 'Y = ' + y);
}
}, true);
character.src = 'character.png';
setInterval(function() {
random = (Math.floor(Math.random() * 6));
random = positions[random];
x = random[0];
y = random[1];
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(character, x, y);
}, 1000/fps);
}());
</script>
</body>
</html>
DOM錯誤在這裏沒有發生(FF 3.6)。 clientX和screenX有什麼區別?我會閱讀文章,謝謝。 – thomas 2010-12-02 19:15:10