css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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

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

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

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

              canvas {
  border: 1px solid #000;
              var UPDATE_RATE = 1000/100; //fps goal

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, startTime) {

  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);

  //now draw the ball

  //restart the loop after we're done updating

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);

//get the start time in milliseconds
//sync so all the balls have the same schedule
var startTime = new Date().getTime();

//create our containers
var ball10 = new BallCanvas(
  1000/10, startTime);
var ball30 = new BallCanvas(
  1000/30, startTime);
var ball60 = new BallCanvas(
  1000/60, startTime);

//start 'em up!

function onMouseMove(e) {

  //get the new positions
  var x = e.clientX;
  var y = e.clientY;

  ball10.ball.x = x;
  ball10.ball.y = y;
  ball30.ball.x = x;
  ball30.ball.y = y;
  ball60.ball.x = x;
  ball60.ball.y = y;

//watch the mouse event
document.onmousemove = onMouseMove;
🕑 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 ..................