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.

            
              <canvas id='canvas10' width="100" height="100"></canvas>
<canvas id='canvas30' width="100" height="100"></canvas>
<canvas id='canvas60' width="100" height="100"></canvas>

            
          
!
            
              function Ball(w, h) {

  //let's just set this to 10 for now
  this.size = 10;

  //initial ball positions
  this.x = 0;
  this.y = 0;

  //this is the expected change per direction per update
  this.dx = 0.3;
  this.dy = 2.75;

  //bounding box (can't escape)
  this.w = w;
  this.h = h;
};

Ball.prototype.update = function() {
  //update the ball position
  this.x += this.dx;
  this.y += this.dy;

  //check to see if the ball is still in bounds
  //if it's not, change its direction to the opposite
  if (this.x <= 0 || this.x >= this.w - this.size)
    this.dx = -this.dx;

  if (this.y <= 0 || this.y >= this.h - this.size)
    this.dy = -this.dy; 
};

Ball.prototype.draw = function(ctx) {
  //let's draw the ball (box) 
  ctx.fillRect(this.x, this.y, 10, 10);
};


//this wrapper will handle the ball
function BallCanvas(canvas, frameRate) {

  this.frameRate = frameRate;
  
  //this will move around
  this.ball = new Ball(100, 100);

  //get the context, set the fill color
  this.ctx = canvas.getContext('2d');
  this.ctx.fillStyle = '#DD0000';
}

BallCanvas.prototype.animate = function () {
  
  //clear the canvas between frames
  this.ctx.clearRect(0, 0, 100, 100);

  //update the ball position
  this.ball.update();

  //now draw the ball
  this.ball.draw(this.ctx);

  //restart the loop after we're done updating
  this.start();
};

BallCanvas.prototype.start = function () {
  //we're forcing very specific framerates here
  //there are better ways...
  var that = this;
  window.setTimeout(function() { that.animate() }, this.frameRate);
};


//create our containers
var ball10 = new BallCanvas(
  document.getElementById('canvas10'),
  1000/10);
var ball30 = new BallCanvas(
  document.getElementById('canvas30'),
  1000/30);
var ball60 = new BallCanvas(
  document.getElementById('canvas60'),
  1000/60);

//start 'em up!
ball10.start(); 
ball30.start(); 
ball60.start(); 
            
          
!
999px
🕑 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 ..................

Console