draw image on cursor position - Javascript Canvas

Javascript examples for Canvas:image

Description

draw image on cursor position

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.11.0.js"></script> 
      <style id="compiled-css" type="text/css">

#myCanvas {/*from  www. ja  v a 2  s  .co m*/
   background-color: #adadad;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var snipeImg=new Image(); // Image ??? ?????
snipeImg.src="https://www.java2s.com/style/demo/Google-Chrome.png";
var holeImg = new Image();
holeImg.src="https://www.java2s.com/style/demo/Google-Chrome.png";
var holeList =[];
$('#myCanvas').mousemove(function(event){
    context.clearRect(0,0,800,500);
    var eventX =event.offsetX -50;
    var eventY =event.offsetY -50;
    context.drawImage(snipeImg,eventX,eventY,100,100);
    drawScreen();
}).mousedown(function(event){
    context.clearRect(0,0,800,500);
    var holeX =event.offsetX-10;
    var holeY =event.offsetY-10;
    console.log(holeX + " , " + holeY);
    var obj={};
    obj.x = holeX;
    obj.y = holeY;
    holeList.push(obj);
    drawScreen();
});
function drawScreen(){
    for(var i in holeList){
        context.save();
        var tmp = holeList[i];
        context.drawImage(holeImg,tmp.x,tmp.y,20,20);
        context.restore();
    }
};
    });

      </script> 
   </head> 
   <body> 
      <canvas id="myCanvas" width="800" height="500"></canvas>  
   </body>
</html>

Related Tutorials