Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                * { margin: 0; padding: 0; }


              
            
!

JS

              
                // -------------------------- requestAnimationFrame -------------------------- //

// https://gist.github.com/1866474

var lastTime = 0;
var prefixes = 'webkit moz ms o'.split(' ');
// get unprefixed rAF and cAF, if present
var rAF = window.requestAnimationFrame;
var cAF = window.cancelAnimationFrame;
// loop through vendor prefixes and get prefixed rAF and cAF
var prefix;
for( var i = 0; i < prefixes.length; i++ ) {
  if ( rAF && cAF ) {
    break;
  }
  prefix = prefixes[i];
  rAF = rAF || window[ prefix + 'RequestAnimationFrame' ];
  cAF  = cAF || window[ prefix + 'CancelAnimationFrame' ] ||
    window[ prefix + 'CancelRequestAnimationFrame' ];
}

// fallback to setTimeout and clearTimeout if either request/cancel is not supported
if ( !rAF || !cAF )  {
  rAF = function( callback ) {
    var currTime = new Date().getTime();
    var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) );
    var id = window.setTimeout( function() {
      callback( currTime + timeToCall );
    }, timeToCall );
    lastTime = currTime + timeToCall;
    return id;
  };

  cAF = function( id ) {
    window.clearTimeout( id );
  };
}

// -------------------------- Particle -------------------------- //

( function() {

function Particle( x, y ) {
  this.x = x;
  this.y = y;
  this.velocity = 0;
  this.accel = 0;
  this.friction = 0.2;
}

Particle.prototype.update = function() {
  this.velocity += this.accel;
  this.velocity *= ( 1 - this.friction );
  this.x += this.velocity;
  this.accel = 0;
};

Particle.prototype.applyForce = function( force ) {
  this.accel += force;
};

Particle.prototype.getRestingPosition = function() {
  // get how many ticks until velocity is slow
  var restingVelo = 0.07;
  var ticks = getBaseLog( 1 - this.friction, restingVelo / Math.abs( this.velocity ) );
  // integrate to determine resting position
  // http://mikestoolbox.com/powersum.html
  var damping = 1 - this.friction;
  var sum = ( Math.pow( damping, ticks + 1 ) - 1 ) / ( damping - 1 );
  return this.x + this.velocity * damping * sum;
}

function getBaseLog( a, b ) {
  return Math.log( b ) / Math.log( a );
}
  
window.Particle = Particle;

})();

// -------------------------- Attractor  -------------------------- //

( function() {

function Attractor( x, y, attraction ) {
  this.x = x;
  this.y = y;
}

window.Attractor = Attractor;

})();


// -------------------------- demo  -------------------------- //

var canvas, ctx;
var canvasW, canvasH;
var particle;
var attractors = [];
var maxDistance;
var estimateX = 0;

document.addEventListener( 'DOMContentLoaded', init, false );

function init() {
  canvas = document.querySelector('canvas');
  ctx = canvas.getContext('2d');
  // set size
  canvasW = canvas.width = window.innerWidth - 20;
  canvasH = canvas.height = window.innerHeight - 80;
  

  canvas.addEventListener( 'mousedown', onMousedown, false );

  particle = new Particle( canvasW / 2, canvasH / 2 );
  var x0 = canvasW * 0.15;
  var x1 = canvasW * 0.5;
  var x2 = canvasW * 0.85;
  attractors.push( new Attractor( x0, canvasH / 2 ) );
  attractors.push( new Attractor( x1, canvasH / 2 ) );
  attractors.push( new Attractor( x2, canvasH / 2 ) );
  maxDistance = Math.abs( x1 - x0 ) * 0.5;

  animate();
}

// -------------------------- animate, render, update -------------------------- //


function animate() {
  // apply force of attractors to particle
  if ( !isDragging && activeAttractor ) {
    var attractor = activeAttractor;
    var distance = attractor.x - particle.x;
    var sign = distance < 0 ? -1 : 1;
    // normalize
    var force = Math.abs( distance ) / maxDistance;
    force *= 4;
    force = distance < 0 ? -force : force;
    particle.applyForce( force );
  }

  particle.update();
  // wrap around
  if ( !isDragging ) {
    particle.x = ( particle.x + canvasW ) % canvasW;
  }
  render();
  rAF( animate );
}

function render() {
  ctx.clearRect( 0, 0, canvasW, canvasH );

  // render particle
  ctx.fillStyle = 'hsla(0, 100%, 50%, 0.5)';
  circle( particle.x, particle.y, 15 );

  for ( var i=0, len = attractors.length; i < len; i++ ) {
    var attractor = attractors[i];
      // render attractor
  ctx.fillStyle = attractor === activeAttractor ? 'hsla(240, 100%, 50%, 0.5)' : 'hsla(240, 100%, 50%, 0.2)';
    circle( attractor.x, attractor.y, 8 );
  }

  // render estimate
  ctx.fillStyle = 'hsla(150, 100%, 25%, 0.5)';
  circle( estimateX, canvasH / 2, 4 );
}


function circle( x, y, radius ) {
  ctx.beginPath();
  ctx.arc( x, y, radius, 0, Math.PI * 2, true );
  ctx.fill();
  ctx.closePath();
}

// -------------------------- mouse -------------------------- //

var isDragging = false;

function onMousedown( event ) {
  event.preventDefault();
  isDragging = true;
  window.addEventListener( 'mousemove', onMousemove, false );
  window.addEventListener( 'mouseup', onMouseup, false );
  particle.x = event.pageX;
  particle.velocity = 0;
}

var previousX;
var previousTime;
var currentTime;

function onMousemove( event ) {
  // previous
  previousX = particle.x;
  previousTime = currentTime;
  // current
  particle.x = event.pageX;
  currentTime = new Date();
}

function onMouseup( event ) {
  dragEnd();
  // console.log( particle.velocity );
  isDragging = false;
  window.removeEventListener( 'mousemove', onMousemove, false );
  window.removeEventListener( 'mousemove', onMouseup, false );
}

var activeAttractor;

function dragEnd() {
  if ( previousX ) {
    // set particle velocity
    particle.velocity = ( particle.x - previousX ) / ( currentTime - previousTime );
    particle.velocity *= 17;
    estimateX = particle.getRestingPosition();
    // reset previousX
    previousX = null;
  } else {
    estimateX = particle.x;
  }

  // get closest attractor to end position
  var minDistance = Infinity;
  for ( var i=0, len = attractors.length; i < len; i++ ) {
    var attractor = attractors[i];
    var distance = Math.abs( estimateX - attractor.x );
    if ( distance < minDistance ) {
      activeAttractor = attractor;
      minDistance = distance;
    }
  }
}
              
            
!
999px

Console