Advertisement
lemansky

Untitled

Apr 21st, 2021
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.30 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 rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
  8.     <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  9.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
  10.   <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" crossorigin="anonymous"></script>
  11.     <style>
  12.         .container-fluid{
  13.             margin:0px;
  14.             padding:0px;
  15.         }
  16.  
  17.     </style>
  18.     <script>
  19.         document.addEventListener("DOMContentLoaded", function(event){
  20.             let canvas = document.getElementById("canvasId");
  21.             let context = canvas.getContext("2d");
  22.             let mouse;
  23.             let shapes = [];
  24.  
  25.             class Shape{
  26.                 constructor(x, y, w, h, color){
  27.                     this.x = x;
  28.                     this.y = y;
  29.                     this.w = w;
  30.                     this.h = h;
  31.                     this.color = color;
  32.                 }
  33.             }
  34.             const handler = (mouse) => {
  35.                 context.clearRect(0, 0, canvas.width, canvas.height);
  36.                 context.fillStyle = shapes[0].color;
  37.                 shapes[0].x = mouse.x - shapes[0].w/2;
  38.                 shapes[0].y = mouse.y - shapes[0].h/2
  39.                 context.fillRect(shapes[0].x, shapes[0].y, shapes[0].w, shapes[0].h);
  40.             }
  41.             let isOnPath = false;
  42.             canvas.addEventListener("click", function(evt){
  43.                 mouse = mouse_player1(evt, canvas);
  44.                 if(!isOnPath){
  45.                     for (iterator of shapes) {
  46.                         let myPath = new Path2D();
  47.                         context.beginPath();
  48.                         myPath.rect(iterator.x, iterator.y, iterator.w, iterator.h);
  49.                         context.closePath();
  50.                         if(context.isPointInPath(myPath, mouse.x, mouse.y)){
  51.                             isOnPath = true;
  52.                             canvas.addEventListener('mousemove', handler);
  53.                         }
  54.                     }
  55.                 } else {
  56.                     canvas.removeEventListener('mousemove', handler);
  57.                     isOnPath = false;
  58.                 }
  59.             });
  60.  
  61.             let btn = document.getElementById('drawShape');
  62.             btn.addEventListener('click', () => {
  63.  
  64.                 let myRectangle = new Shape(
  65.                     document.getElementById('posX').value,
  66.                     document.getElementById('posY').value,
  67.                     document.getElementById('sizeX').value,
  68.                     document.getElementById('sizeY').value,
  69.                     'rgb('+ Math.floor(Math.random()*256) + ', ' +
  70.                         Math.floor(Math.random()*256) + ', ' +
  71.                         Math.floor(Math.random()*256) + ')'
  72.                 );
  73.  
  74.                 context.fillStyle = myRectangle.color;
  75.                 context.fillRect(myRectangle.x, myRectangle.y, myRectangle.w, myRectangle.h);
  76.                 shapes.push(myRectangle);
  77.             });
  78.  
  79.  
  80.         });
  81.  
  82.         const mouse_player1 = (evt, canvas) => {
  83.             var rect = canvas.getBoundingClientRect(); // взима позицията на платното в документа, в случай, че то не започва от горния ляв ъгъл
  84.             var mouseX = evt.clientX - rect.left; // позицията на курсора по хоризонтала
  85.             var mouseY = evt.clientY - rect.top; // позиията на курсора по вертикала
  86.             return {
  87.                 x: mouseX,
  88.                 y: mouseY,
  89.             } // фунцкията връша два параметъра, x и y
  90.         }
  91.  
  92.         </script>
  93.     <style type="text/css">
  94.     #canvasId{
  95.         background: black;
  96.     }
  97.     </style>
  98. </head>
  99. <body class="">
  100.     <div class="container-fluid">
  101.         <div class="row">
  102.             <div class="col-auto">
  103.                 <canvas id="canvasId" class="d-block" width="700" height="600"></canvas>
  104.             </div>
  105.             <div class="col-3">
  106.  
  107.                 <div class="form-group ">
  108.                     <div class="custom-control custom-radio d-inline-block mx-4">
  109.                         <input type="radio" id="shape1" name="shape" class="custom-control-input" value="Rect" >
  110.                         <label class="custom-control-label" for="shape1">Четириъгълник</label>
  111.                     </div>
  112.                     <div class="custom-control custom-radio d-inline-block mx-4">
  113.                         <input type="radio" id="shape2" name="shape" class="custom-control-input" value="Circle">
  114.                         <label class="custom-control-label" for="shape2">Окръжност</label>
  115.                     </div>
  116.                 </div>
  117.  
  118.                 <div class="form-group">
  119.                     <label> Координат по X </label>
  120.                     <input class="form-control" id="posX" value="100"></input>
  121.                 </div>
  122.                 <div class="form-group">
  123.                     <label> Координат по Y </label>
  124.                     <input class="form-control" id="posY" value="100"></input>
  125.                 </div>
  126.                 <div class="form-group">
  127.                     <label> Широчина </label>
  128.                     <input class="form-control" id="sizeX" value="200"></input>
  129.                 </div>
  130.                 <div class="form-group">
  131.                     <label> Височина </label>
  132.                     <input class="form-control" id="sizeY" value="20"></input>
  133.                 </div>
  134.                 <div class="form-group">
  135.                     <input type="button" class="btn btn-info btn-block" id="drawShape" value="Създай">
  136.                 </div>
  137.             </div>
  138.         </div>
  139.     </div>
  140. </body>
  141. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement