ctx.rotate(Math.atan2(looky-y,lookx-x)-Math.PI/2);// Adjust image 90 degree anti clockwise (PI/2) because the image is pointing in the wrong direction.
ctx.drawImage(img,-img.width/2,-img.height/2);
ctx.setTransform(1,0,0,1,0,0);// restore default not needed if you use setTransform for other rendering operations
}
functiondrawCrossHair(x,y,color){
ctx.strokeStyle=color;
ctx.beginPath();
ctx.moveTo(x-10,y);
ctx.lineTo(x+10,y);
ctx.moveTo(x,y-10);
ctx.lineTo(x,y+10);
ctx.stroke();
}
functionmouseEvent(e){// get the mouse coordinates relative to the canvas top left
varbounds=canvas.getBoundingClientRect();
mouse.x=e.pageX-bounds.left;
mouse.y=e.pageY-bounds.top;
mouse.cx=e.clientX-bounds.left;// to compare the difference between client and page coordinates