Advertisement
here2share

$ JS Drawing

Jan 5th, 2023
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. <html>
  2.   <head>
  3.     <title>JS Drawing</title>
  4.   </head>
  5.   <body>
  6.     <canvas id="gameCanvas" width="800" height="600"></canvas>
  7.     <script>
  8.       var canvas = document.getElementById("gameCanvas");
  9.       var ctx = canvas.getContext("2d");
  10.      
  11.       var isDrawing = false;
  12.       var lastX = 0;
  13.       var lastY = 0;
  14.      
  15.       canvas.addEventListener("mousedown", function(event) {
  16.         isDrawing = true;
  17.         lastX = event.offsetX;
  18.         lastY = event.offsetY;
  19.       });
  20.      
  21.       canvas.addEventListener("mousemove", function(event) {
  22.         if (isDrawing) {
  23.           var x = event.offsetX;
  24.           var y = event.offsetY;
  25.          
  26.           ctx.beginPath();
  27.           ctx.moveTo(lastX, lastY);
  28.           ctx.lineTo(x, y);
  29.           ctx.stroke();
  30.          
  31.           lastX = x;
  32.           lastY = y;
  33.         }
  34.       });
  35.      
  36.       canvas.addEventListener("mouseup", function(event) {
  37.         isDrawing = false;
  38.       });
  39.      
  40.       canvas.addEventListener("mouseleave", function(event) {
  41.         isDrawing = false;
  42.       });
  43.     </script>
  44.   </body>
  45. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement