lemansky

Untitled

Dec 13th, 2021 (edited)
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.79 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", () =>{
  8.             let canvas = document.getElementById('canvasId');
  9.             let context = canvas.getContext("2d");  
  10.             let shapes = [];
  11.  
  12.             canvas.addEventListener('click', (e) => {
  13.  
  14.                 let radius = Math.floor(Math.random()*51+50);
  15.                 let color = 'rgb(' +
  16.                     Math.floor(Math.random()*(255-50+1) + 50) + ', ' +
  17.                     Math.floor(Math.random()*(255-50+1) + 50) + ', ' +
  18.                     Math.floor(Math.random()*(255-50+1) + 50) + ')';
  19.  
  20.                 let mouse = mousePlayer1(e);
  21.                 context.fillStyle = color;
  22.                 context.beginPath();
  23.                 context.arc(mouse.x, mouse.y, radius, 0, 2*Math.PI);
  24.                 context.closePath();
  25.                 context.fill();
  26.                 shapes.push(new Shape(mouse.x, mouse.y, radius, color));
  27.             });
  28.  
  29.             let btn = document.querySelectorAll('[type=button]')[0];
  30.             btn.addEventListener('click', (e) => {
  31.                 let pole = document.querySelectorAll('#radius')[0].value;
  32.  
  33.                 context.clearRect(0, 0, canvas.width, canvas.height);
  34.                 shapes.forEach(item => {
  35.                    context.fillStyle = item.color;
  36.                     context.beginPath();
  37.                     context.arc(item.x, item.y, pole, 0, 2*Math.PI);
  38.                     context.closePath();
  39.                     context.fill();
  40.                 });
  41.             });
  42.  
  43.  
  44.             document.addEventListener('keyup', (e) => {
  45.                 if(e.code == 'Space'){
  46.                    shapes.forEach(item => {
  47.                     context.fillStyle = 'green';
  48.                         context.beginPath();
  49.                         context.arc(item.x, item.y, item.radius, 0, 2*Math.PI);
  50.                         context.closePath();
  51.                         context.fill();
  52.                     });                    
  53.                 }
  54.             });
  55.         });
  56.  
  57.         class Shape{
  58.             constructor(x, y, radius, color){
  59.                 this.x = x;
  60.                 this.y = y;
  61.                 this.radius = radius;
  62.                 this.color = color;
  63.             }
  64.         }
  65.  
  66.         const mousePlayer1 = (e) => {
  67.             return {
  68.                 x: e.layerX,
  69.                 y: e.layerY,
  70.             }
  71.         }
  72.     </script>
  73.     <style>
  74.         canvas{
  75.             background-color: black;
  76.         }
  77.     </style>
  78. </head>
  79. <body>
  80.     <canvas width="800" height="800" id="canvasId"></canvas>
  81.     <input type="text" id="radius">
  82.     <input type="button" value="Change radius">
  83. </body>
  84. </html>
Add Comment
Please, Sign In to add comment