cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

                <div id="ballsHolder">
    <select id="dropBalls">
      <option value="">Balls ??</option>
      <option value="2">2</option>
      <option value="5">5</option>
      <option value="8">8</option>
      <option value="10">10</option>
      <option value="15">15</option>
      <option value="20">20</option>
      <option value="30">30</option>
      <option value="40">40</option>
  </div><!--end balls holder div -->
              #ballsHolder {
  position: relative;
  overflow: hidden;

#dropBalls {
  position: absolute;
              // This is a modification of something I learned here:
//I added the drop list, https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_building_practice
// grabs the <canvas></canvas>, which is simply another html element
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');//seems more like it should be setContext...?

//below is called chaining  variables: chain = otherChain = anotherchain
var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight;
var numberBalls = 10;

//this is select drop list
var dropBalls = document.getElementById('dropBalls');

dropBalls.addEventListener('change', addBallz);
function addBallz(){
  numberBalls = dropBalls.value;
//function to generate random number

function randomMW(min,max) {
  var num = Math.floor(Math.random()*(max-min)) + min;
  return num;

//this is an object constructor, hence the Capital letter
function Ball(x, y, velX, velY, color, size) {
  this.x = x;
  this.y = y;
  this.velX = velX;
  this.velY = velY;
  this.color = color;
  this.size = size;

Ball.prototype.draw = function() {
  ctx.fillStyle = this.color;
The last two parameters (0, 2*PI) specify the start and end number
of degrees round the circle that the arc is drawn between.
Here we specify 0 degrees, and 2 * PI, which is the
equivalent of 360 degrees in radians
(annoyingly, you have to specify this in radians).
That gives us a complete circle.
If you had specified only 1 * PI, you'd get a semi-circle (180 degrees).
  ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);

basically states "finish drawing the path we started with
beginPath(), and fill the area it takes up with
the color we specified earlier in fillStyle."
Ball.prototype.update = function(){
//makes balls 'bounce' off wall
  if ((this.x + this.size) >= width) {
    this.velX = -(this.velX);

  if ((this.x - this.size) <= 0) {
    this.velX = -(this.velX);

  if ((this.y + this.size) >= height) {
    this.velY = -(this.velY);

  if ((this.y - this.size) <= 0) {
    this.velY = -(this.velY);

  this.x += this.velX;
  this.y += this.velY;
}//end ball.update

Ball.prototype.collisionDetect = function(){
  for(var j = 0; j < balls.length; j++){

/*is the ball we are checking not the current one
in the loop, if so, proceed.

Put another way: is the current ball
(whose collisionDetect is being invoked)
the same as the loop ball?
Then we negate the check ( ! ) so the statement only
runs if the are *not* the same
    if(!(this === balls[j])){

      /*check to see if two circles overlap - collide
      var dx = this.x - balls[j].x;
      var dy = this.y - balls[j].y;
      var distance = Math.sqrt(dx * dx + dy * dy);

//if a collision was detected
      if(distance < this.size + balls[j].size){
        balls[j].color = this.color = 'rgb(' + randomMW(0,255) + ',' + randomMW(0,255) + ','  + randomMW(0,255) + ')';
      }//end if(distance....)

    }// end if(!(this))
  }//end for (var j)

var balls = [];// we add array values via balls.push(ball);

function loop(){
  ctx.fillStyle = 'rgba(0, 0, 0, 0.25)';
  ctx.fillRect(0, 0, width, height);

  if (balls.length < numberBalls) {
    var ball = new Ball (
      randomMW(-5,5),//this is velocity
      'rgb(' + randomMW(0,255) + ',' + randomMW(0,255) + ','  + randomMW(0,255) +  ')',
    balls.push(ball);//'push(es) a ball instance into balls[]'
  } else if (balls.length > numberBalls) {
  }//end if balls.length
  for (var i = 0; i < balls.length; i++) {
  requestAnimationFrame(loop);//runs frames continously set speed per second

}//end function loop()

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................