canvas绘图

 <canvas id="tetris" width="1800" height="900"></canvas>
 <script type="text/javascript">
  var canvas = document.getElementById("tetris");
  var context = canvas.getContext("2d");
  
  var isDown = false;
  var prePoint = null;
  context.strokeStyle="#ea7421";
  context.lineWidth = 5;
  context.lineCap = 'round';
  canvas.onmousedown = function(){
   isDown = true;
  };
  canvas.onmouseup = function(){
   isDown = false;
   prePoint = null;
  }
  canvas.onmousemove = function(e){
   if(isDown){
    context.beginPath();
    if(!prePoint){
     prePoint = {
      x: e.clientX,
      y: e.clientY
     };
    }else{
     context.moveTo(prePoint.x, prePoint.y);
     prePoint = {
       x: e.clientX,
       y: e.clientY
      };
     context.lineTo(prePoint.x, prePoint.y);
     context.stroke();
     context.closePath();
    }
   }
  }
  canvas.onclick = function(e){
   context.beginPath();
   context.moveTo(e.clientX-1, e.clientY);
   context.lineTo(e.clientX, e.clientY);
   context.stroke();
   context.closePath();
  };
 </script>

猜你喜欢

转载自com-niu.iteye.com/blog/2330087