Saturday, March 2, 2013

HTML5 mousedown and mouseup example.

The code below shows how a mouseup and mousedown event is handled. The code is a html file that can be run from a browser. The code worked in ie9.

<!DOCTYPE = html>
<html><body>
<canvas id="myCanvas" width="150" style="border:1px solid #d3d3d3;">
Use different browser.
</canvas>
<script>
 var c=document.getElementById("myCanvas");
 var ctx=c.getContext("2d");
 var m="Left Mouse is not pressed";
 gameloop = setInterval(doGameLoop,16);
 window.addEventListener("mousedown",md,true);
 window.addEventListener("mouseup",mu,true);

 function doGameLoop(){
  ctx.clearRect(0,0,c.width,c.height);
  ctx.fillText(m,10,10);   
 }
 function md(evt){
  if(evt.which==1){
   m="left mouse is being pressed.";
  }
 }
 function mu(evt){
  if(evt.which==1){
   m="No mouse is pressed.";
  }
 }
</script></body></html>


<!DOCTYPE = html>
<html><body>

No comments:

Post a Comment