Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
92
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>Canvas 2D template</title>
  6.     <style>
  7.         body {
  8.             margin: 0;
  9.         }
  10.  
  11.         canvas {
  12.             display: block;
  13.         }
  14.     </style>
  15. </head>
  16. <body>
  17. <canvas id="canvas"></canvas>
  18. </body>
  19. <script>
  20.  
  21.     let canvas = document.getElementById("canvas");
  22.     // Store context of our canvas
  23.     let c = canvas.getContext("2d");
  24.  
  25.     // Define width and height
  26.     canvas.width = innerWidth;
  27.     canvas.height = innerHeight;
  28.    
  29.     let canvasMidX = canvas.width / 2;
  30.     let canvasMidY = canvas.height / 2;
  31.  
  32.     // Define core vars
  33.     let mouse = {
  34.         x: innerWidth / 2,
  35.         y: innerHeight / 2
  36.     };
  37.  
  38.     let colorArray = [
  39.         '#45a57c',
  40.         '#b1044e',
  41.         '#71e0c9',
  42.         '#e41e70'
  43.     ];
  44.    
  45.     let objectToUpdate = [];
  46.  
  47.     let restarting = false;
  48.  
  49.     // Setup core events
  50.     addEventListener('resize', () => {
  51.         canvas.width = innerWidth;
  52.         canvas.height = innerHeight;
  53.     });
  54.  
  55.     addEventListener('mousemove', (event) => {
  56.         mouse.x = event.clientX;
  57.         mouse.y = event.clientY;
  58.     })
  59.  
  60.     addEventListener('click', () => {
  61.         restarting = true;
  62.     });
  63.  
  64.     // Utility functions
  65.  
  66.     function randomIntFromRange(min, max) {
  67.         return Math.floor(Math.random() * (max - min + 1) + min)
  68.     }
  69.  
  70.     function randomInArray(array) {
  71.         return array[Math.floor(Math.random() * array.length)]
  72.     }
  73.  
  74.     function getDistance(x1, y1, x2, y2) {
  75.         let xDistance = x2 - x1;
  76.         let yDistance = y2 - y1;
  77.         return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
  78.     }
  79.  
  80.     // Script
  81.  
  82.     function Object(x, y, radius, color) {
  83.         this.x = x;
  84.         this.y = y;
  85.         this.radius = radius;
  86.         this.color = color;
  87.         this.lifeTime = 0;
  88.  
  89.         this.update = function(time) {
  90.  
  91.             let random = Math.floor(Math.random() * 4 + 1);
  92.             let x = this.x, y = this.y;
  93.            
  94.             switch(random) {
  95.                 case 1:
  96.                     x+= 5;
  97.                     break;
  98.                 case 2:
  99.                     x -= 5;
  100.                     break;
  101.                 case 3:
  102.                     y+= 5;
  103.                     break;
  104.                 case 4:
  105.                     y-=5;
  106.                     break;
  107.             }
  108.  
  109.             if(x > 0 || x < canvas.width) this.x = x;
  110.             if(y > 0 || y < canvas.height) this.y = y;
  111.  
  112.             this.draw();
  113.         };
  114.  
  115.         this.draw = function() {
  116.             c.beginPath();
  117.             c.fillStyle = this.color;
  118.             c.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
  119.             c.fill();
  120.             c.closePath();
  121.         }
  122.     }
  123.  
  124.  
  125.     // Core functions
  126.  
  127.     function init() {
  128.  
  129.         let ellipse = new Object(canvasMidX, canvasMidY, 2, "#000");
  130.         objectToUpdate.push({id: "ellipse", object: ellipse});
  131.  
  132.         animate();
  133.     }
  134.  
  135.     let appLifeTime = 0;
  136.  
  137.     function animate() {
  138.         requestAnimationFrame(animate);
  139.  
  140.         //c.clearRect(0, 0, canvas.width, canvas.height);
  141.         c.fillStyle = 'rgba(255, 255, 255, 0.01)';
  142.         c.fillRect(0, 0, canvas.width, canvas.height);
  143.  
  144.         // code goes here
  145.         appLifeTime ++;
  146.  
  147.         objectToUpdate.forEach(e => {
  148.             e.object.update(appLifeTime);
  149.         })
  150.  
  151.     }
  152.  
  153.     init();
  154.  
  155. </script>
  156. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement