link for the code 1. 다음 코드는 html5 캔버스에 원을 그리고 마우스 클릭을 얻기위한 이벤트 목록 작성자를 추가합니다. 당신의 "원"이 수직으로 늘어좌표와 마우스 스크롤 비교
주의 사항 : 원 안에 마우스 클릭은 기존의 코드를
<body>
<canvas id="canvas" onclick="handleEvent()"></canvas>
</body>
body{
background: #3e3e3e;
}
#canvas{
background:white;
height: 400px;
width: 400px;
border: 2px solid yellow;
}
window.onload=function(){
var canvas=document.getElementById('canvas');
var ctx=canvas.getContext('2d');
var cx=canvas.width/2;
var cy=canvas.height/2;
var r=20;
//circle draw function
ctx.beginPath();
ctx.arc(cx,cy,r,0,2*Math.PI,false);
ctx.stroke();
}
//function to get mouse click coordinates
function handleEvent(e)
{
var evt = e ? e:window.event;
var clickX=0, clickY=0;
if (evt.pageX || evt.pageY)
{
clickX = evt.pageX;
clickY = evt.pageY;
}
if(180<evt.pageX<220)
{
alert("you are inside the circle");
}
alert (evt.type.toUpperCase() + ' mouse event:'
+'\n pageX = ' + clickX
+'\n pageY = ' + clickY
)
return false;
}