Advertisement
lemansky

Untitled

May 4th, 2022
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.35 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.  
  11.         let shapes = [];
  12.         for(let i = 0; i < 10; i++){
  13.            let w = Math.floor(Math.random()*(100-50 + 1) + 50);
  14.            let h = Math.floor(Math.random()*(100-50 + 1) + 50);
  15.            let color = colorPicker();
  16.            let x = Math.floor(Math.random()*700);
  17.            let y = Math.floor(Math.random()*700);
  18.            canvasId.fillStyle = color;
  19.            canvasId.fillRect(x, y, w, h);
  20.            shapes.push({x: x, y: y, width: w, height: h, color: color});
  21.        }
  22.        
  23.        canvas.addEventListener('click', (e) => {
  24.  
  25.             let activeColor = undefined;
  26.             for(let i = 0; i < shapes.length; i++){
  27.                canvasId.beginPath();
  28.                canvasId.rect(shapes[i].x, shapes[i].y, shapes[i].width, shapes[i].height);
  29.                canvasId.closePath();
  30.                if(canvasId.isPointInPath(e.offsetX, e.offsetY)){
  31.                    activeColor = shapes[i].color;
  32.                    console.log('works');
  33.                }
  34.            }
  35.  
  36.            if(activeColor == undefined){
  37.                canvasId.clearRect(0, 0, canvas.width, canvas.height);
  38.                for(let i = 0; i < shapes.length; i++){
  39.                    let x = Math.floor(Math.random()*700);
  40.                    let y = Math.floor(Math.random()*700);
  41.                    canvasId.fillStyle = activeColor == undefined ? shapes[i].color : activeColor;
  42.                    canvasId.fillRect(x, y, shapes[i].width, shapes[i].height);
  43.                    shapes[i].x = x;
  44.                    shapes[i].y = y;
  45.                }
  46.            } else {
  47.                canvasId.clearRect(0, 0, canvas.width, canvas.height);
  48.                for(let i = 0; i < shapes.length; i++){
  49.                    canvasId.fillStyle = activeColor == undefined ? shapes[i].color : activeColor;
  50.                    canvasId.fillRect(shapes[i].x, shapes[i].y, shapes[i].width, shapes[i].height);
  51.  
  52.                }
  53.            }
  54.        });
  55.    });
  56.  
  57.    let colorPicker = () => {
  58.         let c = ['A', 'B', 'C', 'D', 'E', 'F', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
  59.         let color = '#';
  60.         for(let i = 0; i < 6; i++){
  61.            color = color + c[Math.floor(Math.random()*16)];
  62.        }
  63.        return color;
  64.    }
  65.  
  66.    const mouse_player1 = (evt, canvas) => {
  67.         var rect = canvas.getBoundingClientRect(); // взима позицията на платното в документа, в случай, че то не започва от горния ляв ъгъл
  68.         var mouseX = evt.clientX - rect.left; // позицията на курсора по хоризонтала
  69.         var mouseY = evt.clientY - rect.top; // позиията на курсора по вертикала
  70.         return {
  71.             x: mouseX,
  72.             y: mouseY,
  73.         } // фунцкията връша два параметъра, x и y
  74.     }
  75. </script>
  76. <style>
  77.     canvas{
  78.         background-color: black;
  79.     }
  80. </style>
  81. </head>
  82. <body>
  83.   <canvas width="800" height="800" id="canvasId"></canvas>
  84. </body>
  85. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement