Advertisement
MysteryCoder456

HTML5 Canvas Circles

Dec 19th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.87 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>Canvas</title>
  4.         <style>
  5.             body {
  6.                 margin: 0;
  7.             }
  8.             #canvas {
  9.                 border: 2px solid grey;
  10.             }
  11.         </style>
  12.     </head>
  13.     <body>
  14.         <canvas id="canvas"></canvas>
  15.         <script>
  16.                 //Code by MysteryCoder456 aka Rehatino Gaming on other social medias. Do not copy code.
  17.                 function Circle(x, y, radius, speed, color) {
  18.                     this.x = x;
  19.                     this.y = y;
  20.                     this.radius = radius;
  21.                     this.speedx = speed;
  22.                     this.speedy = speed;
  23.                     this.color = color;
  24.  
  25.                     this.draw = function() {
  26.                         c.beginPath();
  27.                         c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
  28.                         c.strokeStyle = this.color;
  29.                         c.lineWidth = 5;
  30.                         c.stroke();
  31.                     }
  32.  
  33.                     this.update = function() {
  34.                         if (this.x + this.radius > innerWidth || this.x - this.radius < 0) {
  35.                            this.speedx = this.speedx * -1;
  36.                        }
  37.                        if (this.y + this.radius > innerHeight || this.y - this.radius < 0) {
  38.                            this.speedy = this.speedy * -1;
  39.                        }
  40.  
  41.                        this.x += this.speedx;
  42.                        this.y += this.speedy;
  43.                        this.draw();
  44.                    }
  45.                }
  46.  
  47.                var canvas = document.getElementById('canvas');
  48.                canvas.width = window.innerWidth;
  49.                canvas.height = window.innerHeight;
  50.    
  51.                var c = canvas.getContext('2d');
  52.  
  53.                var circleArray = []
  54.                
  55.                var colorArray = ['red', 'blue', 'cyan', 'orange', 'pink', 'gold', 'yellow', 'green', 'aqua', 'grey']
  56.  
  57.                for (var i = 0; i < 200; i++) {
  58.                    var radius = 50,
  59.                    speed = Math.random() * 5,
  60.                    color = colorArray[Math.floor(Math.random() * colorArray.length)],
  61.                    x = Math.random() * (innerWidth - radius * 2) + radius,
  62.                    y = Math.random() * (innerHeight - radius * 2) + radius;
  63.  
  64.                    circleArray.push(new Circle(x, y, radius, speed, color));
  65.                }
  66.  
  67.                function animate() {
  68.                    requestAnimationFrame(animate);
  69.                    
  70.                    c.clearRect(0, 0, window.innerWidth, window.innerHeight);
  71.  
  72.                    for (var i = 0; i < circleArray.length; i++) {
  73.                        circleArray[i].update();
  74.                    }
  75.                }
  76.  
  77.                animate();
  78.            </script>
  79.     </body>
  80. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement