Advertisement
deyanivanov966

Упражнение 5 Банков Canvas Решения на задачи и примерен вариант (2)

Jan 13th, 2022
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 context = canvas.getContext("2d");  
  10.         let mouse;
  11.  
  12.         canvas.addEventListener('click', (e) => {
  13.             mouse = mousePlayer1(e);
  14.             let pointA = new Point(mouse.x, mouse.y);
  15.             let pointB = new Point(canvas.width - mouse.x, mouse.y);
  16.             let pointC = new Point(canvas.width - mouse.x, canvas.height - mouse.y);
  17.             let pointD = new Point(mouse.x, canvas.height - mouse.y);
  18.  
  19.             context.beginPath();
  20.             context.strokeStyle = 'red';
  21.             context.moveTo(pointA.x, pointA.y);
  22.             context.lineTo(pointC.x, pointC.y);
  23.             context.stroke();
  24.             context.closePath();
  25.  
  26.             context.beginPath();
  27.             context.strokeStyle = 'red';
  28.             context.moveTo(pointB.x, pointB.y);
  29.             context.lineTo(pointD.x, pointD.y);
  30.             context.stroke();
  31.             context.closePath();
  32.         });
  33.  
  34.     });
  35.  
  36.     class Point{
  37.         constructor(x, y){
  38.             this.x = x;
  39.             this.y = y;
  40.         }
  41.     }
  42.  
  43.     const mousePlayer1 = (e) => {
  44.       return {
  45.         x: e.layerX,
  46.         y: e.layerY,
  47.       }
  48.     }
  49. </script>
  50. <style>
  51.     canvas{
  52.         background-color: black;
  53.     }
  54. </style>
  55. </head>
  56. <body>
  57.   <canvas width="800" height="800" id="canvasId"></canvas>
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement