Advertisement
lemansky

Untitled

Apr 21st, 2021
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.54 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.  
  11.         class Shape{
  12.             constructor(x, y, w, h, color){
  13.                 this.x = x;
  14.                 this.y = y;
  15.                 this.w = w;
  16.                 this.h = h;
  17.                 this.color = color;
  18.                 this.ogColor = color;
  19.             }
  20.         }
  21.  
  22.         let shapes = [];
  23.         for (let index = 0; index < 10; index++) {
  24.            let myRectangle = new Shape(
  25.                Math.floor(Math.random()*701),
  26.                Math.floor(Math.random()*701),
  27.                Math.floor(Math.random()*(100 - 50 + 1) + 50),
  28.                Math.floor(Math.random()*(100 - 50 + 1) + 50),
  29.                // Math.floor(Math.random()*(max - min + 1) + min)
  30.                'rgb('+ Math.floor(Math.random()*256) + ', ' +
  31.                        Math.floor(Math.random()*256) + ', ' +
  32.                        Math.floor(Math.random()*256) + ')'
  33.            );
  34.            shapes.push(myRectangle);
  35.            context.fillStyle = myRectangle.color;
  36.            context.fillRect(myRectangle.x, myRectangle.y, myRectangle.w, myRectangle.h);
  37.        }
  38.        
  39.        canvas.addEventListener('click', (e) => {
  40.             let mouse = mouse_player1(e, canvas);
  41.             let isOnPath = false;
  42.             for (iterator of shapes) {
  43.                 let myPath = new Path2D();
  44.                 context.beginPath();
  45.                 myPath.rect(iterator.x, iterator.y, iterator.w, iterator.h);
  46.                 context.closePath();
  47.                 if(context.isPointInPath(myPath, mouse.x, mouse.y)){
  48.                     isOnPath = true;
  49.                     for (let index = 0; index < shapes.length; index++) {
  50.                        if(iterator.color != iterator.ogColor){
  51.                            shapes[index].color = iterator.ogColor;
  52.                        } else {
  53.                            shapes[index].color = iterator.color;
  54.                        }
  55.                    }
  56.                }
  57.            }
  58.            context.clearRect(0, 0, canvas.width, canvas.height);
  59.            if(!isOnPath){
  60.                for(item of shapes){
  61.                    item.x = Math.floor(Math.random()*701);
  62.                    item.y = Math.floor(Math.random()*701);
  63.                    item.color = item.ogColor;
  64.                }
  65.            }
  66.            for(item of shapes){
  67.                context.fillStyle = item.color;
  68.                context.fillRect(item.x, item.y, item.w, item.h);
  69.            }
  70.        });
  71.  
  72.    });
  73.  
  74.    const mouse_player1 = (evt, canvas) => {
  75.         var rect = canvas.getBoundingClientRect(); // взима позицията на платното в документа, в случай, че то не започва от горния ляв ъгъл
  76.         var mouseX = evt.clientX - rect.left; // позицията на курсора по хоризонтала
  77.         var mouseY = evt.clientY - rect.top; // позиията на курсора по вертикала
  78.         return {
  79.             x: mouseX,
  80.             y: mouseY,
  81.         } // фунцкията връша два параметъра, x и y
  82.     }
  83. </script>
  84. <style>
  85.     canvas{
  86.         background-color: black;
  87.     }
  88. </style>
  89. </head>
  90. <body>
  91.   <canvas width="800" height="800" id="canvasId"></canvas>
  92. </body>
  93. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement