Advertisement
loreng

paste

Nov 17th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.59 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Canvas - Drawing App</title>
  5.     </head>
  6.     <body>
  7.         <canvas width="900px" height="500px" id="canvas">
  8.         </canvas>
  9.         <br>
  10.         <div>
  11.             <label for="">Color</label><br>
  12.             <input type="color" id="color">
  13.         </div>
  14.         <div>
  15.             <label for="">Size</label>
  16.             <select name="" id="size">
  17.                 <option value="25">25px</option>
  18.                 <option value="50">50px</option>
  19.             </select>
  20.         </div>
  21.         <script type="text/javascript">
  22.             var canvas = document.getElementById('canvas');
  23.             var ctx = canvas.getContext("2d");
  24.  
  25.             var x = 0;
  26.             var goto = "right";
  27.             var dragging = false;
  28.  
  29.                 ctx.fillStyle = "#000";
  30.                 ctx.fillRect(0,0, ctx.canvas.width, ctx.canvas.height);
  31.  
  32.                 function StartDrag(evt) {
  33.                     dragging = true;
  34.                 }
  35.                 function Drag(evt){
  36.  
  37.                     if(dragging === true){                 
  38.                         var mousePost = getMousePost(canvas, evt);
  39.                        
  40.                         var x = getMousePost(canvas,evt).x;
  41.                         var y = getMousePost(canvas,evt).y;
  42.  
  43.                         ctx.beginPath();
  44.                         ctx.fillStyle = document.getElementById('color').value;
  45.                         ctx.arc(x,y, document.getElementById('size').value, 0, 2 * Math.PI );
  46.                         ctx.fill();
  47.                     }
  48.                 }
  49.                 function StopDrag(evt){
  50.                     dragging = false;
  51.                 }
  52.                 canvas.addEventListener('mousedown',StartDrag,false);
  53.                 canvas.addEventListener('mousemove',Drag,false);
  54.                 canvas.addEventListener('mouseup',StopDrag,false);
  55.  
  56.                 function getMousePost(canvas, evt){
  57.                     var rect = canvas.getBoundingClientRect();
  58.  
  59.                     return {
  60.                         x: evt.clientX - rect.left,
  61.                         y: evt.clientY - rect.top
  62.                     };
  63.                 }
  64.         </script>
  65.     </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement