Advertisement
lemansky

Untitled

Dec 13th, 2021
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.66 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Document</title>
  6.     <script type="text/javascript">
  7.     document.addEventListener("DOMContentLoaded", function(){
  8.         let canvas = document.getElementById('canvasId');
  9.         let context = canvas.getContext("2d");  
  10.         let mouse;
  11.         let cubes = [];
  12.         let activeColor = 'red';
  13.         let activeState = false;
  14.  
  15.        
  16.         for (let i = 1; i <= 10; i++) {
  17.            let newCube = new ColorCube(canvas.width - 50*i - 10*i, canvas.height - 50, 50, 50);
  18.            context.beginPath();
  19.            context.fillStyle = newCube.color;
  20.            context.rect(newCube.x, newCube.y, newCube.width, newCube.height);
  21.            context.fill();
  22.            context.closePath();
  23.            cubes.push(newCube);
  24.        }
  25.        console.log(cubes);
  26.  
  27.        const handler = (evt) => {
  28.             mouse = mousePlayer1(evt);
  29.             context.strokeStyle = activeColor;
  30.             context.lineTo(mouse.x, mouse.y);
  31.             context.stroke();
  32.         }
  33.  
  34.         canvas.addEventListener('click', (e) => {
  35.             mouse = mousePlayer1(e);
  36.             cubes.forEach(item => {
  37.                 context.beginPath();
  38.                 context.rect(item.x, item.y, item.width, item.height);
  39.                 context.closePath();
  40.                 if(context.isPointInPath(mouse.x, mouse.y)){
  41.                     activeColor = item.color;
  42.                 }
  43.             });
  44.  
  45.  
  46.             if(!activeState){
  47.                
  48.                 context.beginPath();
  49.                 context.moveTo(mouse.x, mouse.y);
  50.                 canvas.addEventListener('mousemove', handler);
  51.                 activeState = true;
  52.             } else {
  53.                 canvas.removeEventListener('mousemove', handler);
  54.                 activeState = false;
  55.             }
  56.  
  57.         });
  58.  
  59.        
  60.     });
  61.  
  62.     const mousePlayer1 = (e) => {
  63.       return {
  64.         x: e.layerX,
  65.         y: e.layerY,
  66.       }
  67.     }
  68.  
  69.     const colorPicker = () => {
  70.         let color = '#';
  71.         let hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
  72.         for (let i = 0; i < 6; i++) {
  73.             color = color + hex[Math.floor(Math.random()*16)];
  74.         }
  75.  
  76.         return color;
  77.     }
  78.    class ColorCube{
  79.        constructor(x, y, width, height){
  80.            this.x = x;
  81.            this.y = y;
  82.            this.width = width;
  83.            this.height = height;
  84.            this.color = colorPicker();
  85.        }
  86.    }
  87.    
  88. </script>
  89. <style>
  90.     canvas{
  91.         background-color: black;
  92.     }
  93. </style>
  94. </head>
  95. <body>
  96.   <canvas width="800" height="800" id="canvasId"></canvas>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement