Advertisement
lemansky

Untitled

Dec 1st, 2022
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.34 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 ctx = canvas.getContext("2d");
  13.             let mouse;
  14.             let shapes = [];
  15.             let btn = document.querySelector('.btn-info');
  16.             let x = document.querySelector('#posX');
  17.             let y = document.querySelector('#posY');
  18.             let w = document.querySelector('#sizeX');
  19.             let h = document.querySelector('#sizeY');
  20.  
  21.             const drawShapes = () => {
  22.                 ctx.clearRect(0,0, canvas.width, canvas.height);
  23.                 for(let i = 0; i < shapes.length; i++){
  24.                    ctx.fillStyle = shapes[i].color;
  25.                    ctx.fillRect(shapes[i].x, shapes[i].y, shapes[i].w, shapes[i].h);
  26.                }
  27.            }
  28.  
  29.            const moveShape = (evt) => {
  30.                 let active = 0;
  31.                 shapes.forEach((item, index) => {
  32.                     if(item.isActive == true){
  33.                         active = index;
  34.                     }
  35.                 });
  36.                 shapes[active].x = evt.offsetX - shapes[active].w/2;
  37.                 shapes[active].y = evt.offsetY - shapes[active].h/2;
  38.                 drawShapes();
  39.             }
  40.            
  41.             btn.addEventListener('click', (e) => {
  42.                 let colorShape = rndColor();
  43.                 shapes.push({
  44.                     x: x.value,
  45.                     y: y.value,
  46.                     w: w.value,
  47.                     h: h.value,
  48.                     color: colorShape,
  49.                     isActive: false
  50.                 });
  51.                 ctx.fillStyle = colorShape;
  52.                 ctx.fillRect(x.value, y.value, w.value, h.value);
  53.             });
  54.  
  55.             canvas.addEventListener("click", (e) => {
  56.                 mouse = mousePlayer1(e);
  57.  
  58.                 for(let i = 0; i < shapes.length; i++){
  59.                    ctx.beginPath();
  60.                    ctx.rect(shapes[i].x, shapes[i].y, shapes[i].w, shapes[i].h);
  61.                    ctx.closePath();
  62.                    if(ctx.isPointInPath(mouse.x, mouse.y)){
  63.                        if(shapes[i].isActive == false){
  64.                            canvas.addEventListener('mousemove', moveShape);
  65.                            shapes[i].isActive = true;
  66.                        } else if(shapes[i].isActive == true){
  67.                            canvas.removeEventListener('mousemove', moveShape);
  68.                            shapes[i].isActive = false;
  69.                        }
  70.                    } else {
  71.                        console.log('clicked outside');
  72.                    }
  73.                }
  74.             });
  75.         });
  76.  
  77.         const mousePlayer1 = (e) => {
  78.           return {
  79.             x: e.offsetX,
  80.             y: e.offsetY,
  81.           }
  82.         }
  83.  
  84.         const rndColor = () => {
  85.             return '#' + Math.floor(Math.random()*256*256*256).toString(16);
  86.         }
  87.  
  88.         </script>
  89.     <style type="text/css">
  90.     #canvasId{
  91.         background: black;
  92.     }
  93.     </style>
  94. </head>
  95. <body class="p-2">
  96.     <div class="container-fluid">
  97.         <div class="row">
  98.             <div class="col-auto">
  99.                 <canvas id="canvasId" class="d-block" width="800" height="600"></canvas>
  100.             </div>
  101.             <div class="col-3">
  102.  
  103.                 <div class="form-check">
  104.                     <div class="d-inline-block mx-4">
  105.                         <input type="radio" id="shape1" name="shape" class="form-check-input" value="Rect" >
  106.                         <label class="form-check-label" for="shape1">Четириъгълник</label>
  107.                     </div>
  108.                     <div class="d-inline-block mx-4">
  109.                         <input type="radio" id="shape2" name="shape" class="form-check-input" value="Circle">
  110.                         <label class="form-check-label" for="shape2">Окръжност</label>
  111.                     </div>
  112.                 </div>
  113.  
  114.                 <div class="form-group mb-3">
  115.                     <label> Координат по X </label>
  116.                     <input class="form-control" id="posX" value="100"></input>
  117.                 </div>
  118.                 <div class="form-group mb-3">
  119.                     <label> Координат по Y </label>
  120.                     <input class="form-control" id="posY" value="100"></input>
  121.                 </div>
  122.                 <div class="form-group mb-3">
  123.                     <label> Широчина </label>
  124.                     <input class="form-control" id="sizeX" value="200"></input>
  125.                 </div>
  126.                 <div class="form-group mb-3">
  127.                     <label> Височина </label>
  128.                     <input class="form-control" id="sizeY" value="80"></input>
  129.                 </div>
  130.                 <div class="form-group d-grid">
  131.                     <input type="button" class="btn btn-info text-white" id="drawShape" value="Създай">
  132.                 </div>
  133.             </div>
  134.         </div>
  135.     </div>
  136. </body>
  137. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement