Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas></canvas>
              
            
!

CSS

              
                canvas {
  border: none; 
  background: #00aeff; 
  position: absolute; 
  z-index: -1; 
  top: 0; 
  left: 0;
}
              
            
!

JS

              
                //Initiate canvas and canvas context
var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');

//Set canvas size (full screen)
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

//Make sure canvas stays the same size on resizing
window.addEventListener('resize', function (event) {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
init();
});

//Set 6 ball colors (array)
var ballColors = ['#ffe0e7','#d3f4ff','#fef5dc','#d2ffe1','#e1e0ff']

//Set initial mouse positions
 var mpos = {
     x: undefined,
     y: undefined
 }
 
//Get mouse position when it moves
window.addEventListener('mousemove', function (event) {
mpos.x = event.x;
mpos.y = event.y;
});

//Create a master object with constructor function "Ball" to host individual ball activities: draw and update position (bouncing and mouse inteaction) This is like the Class in PHP
Ball = function (x, y, r, sx, sy) {
//this = function/object: Ball
//Define all the variables/properties of the object Ball
this.x = x,
this.y = y,
this.r = r,
this.sx = sx,
this.sy = sy,
this.color = ballColors[Math.floor(Math.random() * ballColors.length)]; //Make sure it's a random item in the colors array
this.d = r * 10
this.maxR = r * 5;
this.minR = r;
//function/method #1: draw
this.draw = function () {
c.beginPath();
c.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
}
//function/method #2: update - includes bouncing and mouve interactions
this.update = function () {
//If ball hits canvas boundries, ball bounces back
if (this.x + this.r > innerWidth || this.x - this.r < 0) { this.sx = -this.sx }
if (this.y < this.r || this.y > innerHeight - this.r) {this.sy = -this.sy}
//Oherwise, ball moves right and up (default direction)
this.x += this.sx;
this.y += this.sy;
if (mpos.x - this.x < this.d && mpos.x - this.x > -this.d && mpos.y - this.y < this.d && mpos.y - this.y > -this.d) {
if (this.r < this.maxR) { this.r += 1;}
} else if (this.r > this.minR) {
    this.r -= 1;
}
this.draw(); //Make sure to call draw function inside update otherwise nothing shows up on canvas/screen
}
}
//Wrap the "declare 200 balls function" below in an initiate function to make sure balls fill out the canvas on resizing
init = function () {
balls = []; //Clean up formerly stored balls first so every time its a regeneration
        for (var i = 0; i < 50; i++) {
          var r = Math.random() * 10 + 2;
          var x = Math.random() * (innerWidth - r * 2) + r; //Make sure initial position is inside the canvas
          var y = Math.random() * (innerHeight - r * 2) + r;//Same as above
          var sx = (Math.random() - 0.5) * 5; //Make sure some balls start off moving left and some right
          var sy = (Math.random() - 0.5) * 5; //Make sure some balls start off moving up and some down
          //Create 200 Ball Objects with the above mentioned properties
          balls.push(new Ball(x, y, r, sx, sy));
        }
      }
      init();
      //Animate Ball objects' movements
      animate = function () {
        c.clearRect(0, 0, canvas.width, canvas.height);
        for (var i = 0; i < 200; i++) {
          balls[i].update();
        }
      }
      setInterval(animate, 17);
              
            
!
999px

Console