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
크롬에서 오류가 발생하지만 함수를 호출하면 새 이미지가 반환된다는 보장이 없습니다. 차이점은'screenX'는 화면 왼쪽 상단에서 시작한다는 것입니다. 'clientX'는 웹 페이지의 왼쪽 상단에서 시작합니다. 즉,이 경우에는 후자 만 올바른 결과를 줄 것입니다. –
웹 페이지와 화면의 차이점을 얻지 못했습니다. 나는 그 기사를 읽을 것이다. 고맙습니다. – thomas