Advertisement
lemansky

Untitled

Dec 6th, 2021
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.96 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8">
  5.     <title>Document</title>
  6.     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7.     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  8.  
  9.     <script>
  10.         document.addEventListener("DOMContentLoaded", () => {
  11.             let canvas = document.getElementById("canvasId");
  12.             let context = canvas.getContext("2d");
  13.             let mouse;
  14.             let shapesArray = [];
  15.             let activeShape = -1;
  16.  
  17.             const handler = (evt) => {
  18.                 shapesArray[activeShape].x = evt.layerX - shapesArray[activeShape].width/2;
  19.                 shapesArray[activeShape].y = evt.layerY - shapesArray[activeShape].height/2;
  20.                 drawShapes();
  21.                 // console.log(evt);
  22.             }
  23.  
  24.             canvas.addEventListener("click", (e) => {
  25.                 mouse = mousePlayer1(e);
  26.                 if(activeShape != -1){
  27.                     canvas.removeEventListener('mousemove', handler);
  28.                     activeShape = -1;
  29.                 } else {
  30.                     shapesArray.forEach((item, i) => {
  31.                         context.beginPath();
  32.                         context.rect(item.x, item.y, item.width, item.height);
  33.                         context.closePath();
  34.                         if(context.isPointInPath(mouse.x, mouse.y)){
  35.                             if(activeShape == -1){
  36.                                 activeShape = i;
  37.                                 canvas.addEventListener('mousemove', handler);
  38.                             }
  39.                         }
  40.                     });
  41.                 }
  42.  
  43.             });
  44.  
  45.  
  46.             let btn = document.querySelectorAll('#drawShape')[0];
  47.             btn.addEventListener('click', (e) => {
  48.                 let x = document.querySelectorAll('#posX')[0].value;
  49.                 let y = document.querySelectorAll('#posY')[0].value;
  50.                 let width = document.querySelectorAll('#sizeX')[0].value;
  51.                 let height = document.querySelectorAll('#sizeY')[0].value;
  52.                 let color = colorPicker();
  53.                 let newShape = new Shape(x, y, width, height, color, 'inactive');
  54.                 shapesArray.push(newShape);
  55.                 drawShapes();
  56.                 console.log(shapesArray);
  57.  
  58.             });
  59.  
  60.             const drawShapes = () => {
  61.                 context.clearRect(0, 0, canvas.width, canvas.height);
  62.                 for (let i = 0; i < shapesArray.length; i++) {
  63.                     context.fillStyle = shapesArray[i].color;
  64.                     context.fillRect(shapesArray[i].x,
  65.                                      shapesArray[i].y,
  66.                                      shapesArray[i].width,
  67.                                      shapesArray[i].height);
  68.                 }
  69.             }
  70.  
  71.         });
  72.  
  73.         class Shape{
  74.             constructor(x, y, width, height, color, active){
  75.                 this.x = x;
  76.                 this.y = y;
  77.                 this.width = width;
  78.                 this.height = height;
  79.                 this.color = color;
  80.                 this.active = active;
  81.             }
  82.         }
  83.  
  84.         const colorPicker = () => {
  85.             let color = '#';
  86.             let hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
  87.             for (let i = 0; i < 6; i++) {
  88.                 color = color + hex[Math.floor(Math.random()*16)];
  89.             }
  90.  
  91.             return color;
  92.         }
  93.  
  94.         const mousePlayer1 = (e) => {
  95.           return {
  96.             x: e.layerX,
  97.             y: e.layerY,
  98.           }
  99.         }
  100.  
  101.         </script>
  102.     <style type="text/css">
  103.     #canvasId{
  104.         background: black;
  105.     }
  106.     </style>
  107. </head>
  108. <body class="p-2">
  109.     <div class="container-fluid">
  110.         <div class="row">
  111.             <div class="col-auto">
  112.                 <canvas id="canvasId" class="d-block" width="800" height="600"></canvas>
  113.             </div>
  114.             <div class="col-3">
  115.  
  116.                 <div class="form-check">
  117.                     <div class="d-inline-block mx-4">
  118.                         <input type="radio" id="shape1" name="shape" class="form-check-input" value="Rect" >
  119.                         <label class="form-check-label" for="shape1">Четириъгълник</label>
  120.                     </div>
  121.                     <div class="d-inline-block mx-4">
  122.                         <input type="radio" id="shape2" name="shape" class="form-check-input" value="Circle">
  123.                         <label class="form-check-label" for="shape2">Окръжност</label>
  124.                     </div>
  125.                 </div>
  126.  
  127.                 <div class="form-group mb-3">
  128.                     <label> Координат по X </label>
  129.                     <input class="form-control" id="posX" value="100"></input>
  130.                 </div>
  131.                 <div class="form-group mb-3">
  132.                     <label> Координат по Y </label>
  133.                     <input class="form-control" id="posY" value="100"></input>
  134.                 </div>
  135.                 <div class="form-group mb-3">
  136.                     <label> Широчина </label>
  137.                     <input class="form-control" id="sizeX" value="200"></input>
  138.                 </div>
  139.                 <div class="form-group mb-3">
  140.                     <label> Височина </label>
  141.                     <input class="form-control" id="sizeY" value="20"></input>
  142.                 </div>
  143.                 <div class="form-group d-grid">
  144.                     <input type="button" class="btn btn-info text-white" id="drawShape" value="Създай">
  145.                 </div>
  146.             </div>
  147.         </div>
  148.     </div>
  149. </body>
  150. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement