Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>Document</title>
  8.     <link rel="stylesheet" type="text/css" href="draw.css">
  9. </head>
  10. <body>
  11.     <input type="range" min="1" max="" value="10" onchange="changeLine(this.value)">
  12.     <canvas id="canvas"></canvas>
  13.     <script>
  14.         function changeLine(val){
  15.             lineWidth = val;
  16.         }
  17.         var lineWidth = 10;
  18.         window.addEventListener("load",()=>{
  19.             const canvas = document.querySelector("canvas")
  20.             const ctx = canvas.getContext("2d")
  21.             canvas.height = innerHeight;
  22.             canvas.width = innerWidth;
  23.             let painting = false;
  24.             function startPosition(e){
  25.                 painting = true;
  26.                 draw(e)
  27.             }
  28.             function finishedPosition(){
  29.                 painting = false
  30.                 ctx.beginPath();
  31.             }
  32.             function draw(e){
  33.                 if(!painting) return;
  34.                 ctx.lineWidth = lineWidth;
  35.                 ctx.lineCap = "round";
  36.  
  37.                 ctx.lineTo(e.clientX,e.clientY)
  38.                 ctx.stroke();
  39.                 ctx.beginPath();
  40.                 ctx.moveTo(e.clientX,e.clientY)
  41.             }
  42.             canvas.addEventListener("mousedown", startPosition);
  43.             canvas.addEventListener("mouseup", finishedPosition);
  44.             canvas.addEventListener("mousemove", draw);
  45.        
  46.        
  47.        
  48.        
  49.         });
  50.  
  51.  
  52.         window.addEventListener("resize", ()=>{
  53.             canvas.height = innerHeight;
  54.             canvas.width = innerWidth;
  55.         });
  56.    
  57.    
  58.    
  59.    
  60.    
  61.     </script>
  62. </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement