Draw a cross at the mouse location on a canvas - Javascript Canvas

Javascript examples for Canvas:Mouse

Description

Draw a cross at the mouse location on a canvas

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.8.3.js"></script> 
      <style id="compiled-css" type="text/css">

#debug//from   w ww.  j  a  va 2s.c o  m
{
   cursor:none;
   border: 1px solid #000000;
   position:absolute;
   z-index: 1;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
var canvas = document.getElementById("debug");
var context = canvas.getContext("2d");
var c = document.getElementById("content");
var co = c.getContext("2d");
canvas.addEventListener("mousemove", function(event){
    var x = event.pageX - canvas.offsetLeft;
    var y = event.pageY - canvas.offsetTop;
    context.clearRect(0, 0, canvas.width, canvas.height);
    context.beginPath();
    context.moveTo(0,y);
    context.lineTo(595,y);
    context.moveTo(x,0);
    context.lineTo(x,595);
    context.strokeStyle = "black";//"rgb(255,255,255)";
    context.stroke();
    context.closePath();
});
co.beginPath();
co.moveTo(0,0);
co.lineTo(500,500);
co.strokeStyle = "red";
co.stroke();
co.closePath();
    });

      </script> 
   </head> 
   <body> 
      <canvas width="500" height="500" id="debug"></canvas> 
      <canvas width="502" height="502" id="content"></canvas>  
   </body>
</html>

Related Tutorials