Advertisement
lemansky

Untitled

Dec 13th, 2020
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.06 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 canvasId = canvas.getContext("2d");  
  10.         let mouse;
  11.         let cubes = [];
  12.         let activeColor = 'red';
  13.         let isActive = false;
  14.  
  15.         canvasId.lineWidth = 10;
  16.  
  17.         for (let i = 1; i <= 10; i++) {
  18.            let newCube = new ColorCube(canvas.width - 50*i - 10*i);
  19.            canvasId.beginPath();
  20.            canvasId.fillStyle = newCube.color;
  21.            canvasId.rect(newCube.x, newCube.y, newCube.w, newCube.h);
  22.            canvasId.fill();
  23.            canvasId.closePath();
  24.            cubes.push(newCube);
  25.        }
  26.        console.log(cubes);
  27.  
  28.        const handler = (mouse) => {
  29.             canvasId.lineTo(mouse.x, mouse.y);
  30.             canvasId.stroke();
  31.         }
  32.  
  33.         canvas.addEventListener('click', function(evt){
  34.             mouse = mouse_player1(evt, canvas);
  35.             cubes.forEach((item) => {
  36.                 canvasId.beginPath();
  37.                 canvasId.rect(item.x, item.y, item.w, item.h);
  38.                 canvasId.closePath();
  39.                 if(canvasId.isPointInPath(mouse.x, mouse.y)){
  40.                     activeColor = item.color;
  41.                     isActive = true;
  42.                 }
  43.             });
  44.            
  45.             if(!isActive){  
  46.                 canvasId.beginPath();
  47.                 canvasId.strokeStyle = activeColor;
  48.                 canvasId.moveTo(mouse.x, mouse.y);
  49.                 canvas.addEventListener('mousemove', handler);
  50.                 isActive = true;
  51.             } else {
  52.                 canvas.removeEventListener('mousemove', handler);
  53.                 isActive = false;
  54.                 canvasId.closePath();
  55.             }  
  56.         });
  57.        
  58.     });
  59.  
  60.     const mouse_player1 = (evt, canvas) => {
  61.         var rect = canvas.getBoundingClientRect(); // взима позицията на платното в документа, в случай, че то не започва от горния ляв ъгъл
  62.         var mouseX = evt.clientX - rect.left; // позицията на курсора по хоризонтала
  63.         var mouseY = evt.clientY - rect.top; // позиията на курсора по вертикала
  64.         return {
  65.             x: mouseX,
  66.             y: mouseY,
  67.         } // фунцкията връша два параметъра, x и y
  68.     }
  69.  
  70.     class ColorCube{
  71.         constructor(x){
  72.             this.x = x;
  73.             this.y = 750;
  74.             this.w = 50;
  75.             this.h = 50;
  76.             this.color = "rgb(" + Math.floor(Math.random()*255 + 1) + ", " + Math.floor(Math.random()*255 + 1) + ", " + Math.floor(Math.random()*255 + 1) + ")";
  77.         }
  78.     }
  79.    
  80. </script>
  81. <style>
  82.     canvas{
  83.         background-color: black;
  84.     }
  85. </style>
  86. </head>
  87. <body style="margin:0px;">
  88.   <canvas width="800" height="800" id="canvasId"></canvas>
  89. </body>
  90. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement