Advertisement
metalx1000

Canvas - Basic line draw on keypress

Dec 12th, 2012
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.65 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3.   <head>
  4.     <style>
  5.       body {
  6.         margin: 0px;
  7.         padding: 0px;
  8.       }
  9.     </style>
  10.   </head>
  11.   <body onkeypress="MyDraw()">
  12.     <button onclick="MyDraw()">Click Me</button><br>
  13.     <canvas id="myCanvas" width="578" height="200"></canvas>
  14.     <script>
  15.         var canvas = document.getElementById('myCanvas');
  16.         var context = canvas.getContext('2d');
  17.         var x = 100;
  18.  
  19.         function MyDraw(){
  20.             x+=10;
  21.           context.beginPath();
  22.           context.moveTo(x, 150);
  23.           context.lineTo(450, 50);
  24.           context.stroke();
  25.         }
  26.  
  27.     </script>
  28.   </body>
  29. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement