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

              
                // ----- Cell ----- //

function Cell( props ) {
  this.width = props.width;
  this.height = props.height;
  this.x = props.x;
  this.index = props.index;
}

// ----- Slider ----- //

function Slider() {
  this.x = 0;
  this.accel = 0;
  this.velocity = 0;
  this.friction = 0.3;
}

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

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

Slider.prototype.getRestingPosition = function() {
  // little simulation where thing will rest
  var restingVelo = 0.07;
  var velo = this.velocity;
  var restX = this.x;
  while ( Math.abs( velo ) > restingVelo ) {
    velo *= 1 - this.friction;
    restX += velo;
  }
  return restX;
};

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

var canvas, ctx;
var canvasW, canvasH;
var slider;
var cells = [];
var estimateX = 0;
var selectedIndex = 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 = 200;

  // create cells
  var cellWidth = canvasW / 3;
  for ( var i=0; i < 5; i++ ) {
    var cell = new Cell({
      width: cellWidth,
      height: canvasH - 40,
      x: ( cellWidth + 40 ) * i,
      index: i
    });
    cells.push( cell );
  }

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

  slider = new Slider();


  animate();
}

// -------------------------- animate -------------------------- //

function animate() {
  if ( !isDragging ) {
    slider.applyForce( getAttraction() );
  }

  slider.update();

  render();
  rAF( animate );
}

function getAttraction() {
  var cell = cells[ selectedIndex ];
  var attractorX = cell.x + cell.width / 2;
  var distance = -attractorX - slider.x;
  var force = distance * 0.03;
  return force;
}

// -------------------------- render -------------------------- //

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

  // cursor
  var cursorX = canvasW / 2;
  ctx.lineWidth = 1;
  ctx.strokeStyle = 'hsla(300, 100%, 40%, 1)';
  line( cursorX, 0, cursorX, canvasH );

  // cells
  ctx.save();
  ctx.translate( slider.x + cursorX, 0 );
  for ( var i=0, len = cells.length; i < len; i++ ) {
    var cell = cells[i];
    ctx.fillStyle = 'hsla(0, 100%, 50%, 0.5)';
    ctx.fillRect( cell.x, 20, cell.width, cell.height );
    // target line
    var cellTargetX = cell.x + cell.width / 2;
    ctx.strokeStyle  = 'yellow';
    line( cellTargetX, 40, cellTargetX, cell.height );
  }

  ctx.restore();
}

function line( x1, y1, x2, y2 ) {
  ctx.beginPath();
  ctx.moveTo( x1, y1 );
  ctx.lineTo( x2, y2 );
  ctx.stroke();
  ctx.closePath();
}

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

var isDragging = false;

var dragStartX;

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

var previousX;
var previousTime;
var currentTime;

function onMousemove( event ) {
  // previous
  previousX = slider.x;
  previousTime = currentTime;
  // current
  var moveX = event.pageX - dragStartX;
  slider.x = slider.dragStartX + moveX;
  currentTime = new Date();
}

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

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

  // remember this for checking later
  var prevIndex = selectedIndex;

  // get closest attractor to end position
  var minDistance = Infinity;
  var targetX, distance;
  for ( var i=0, len = cells.length; i < len; i++ ) {
    var cell = cells[i];
    targetX = cell.x + cell.width / 2;
    distance = Math.abs( -estimateX - targetX );
    if ( distance < minDistance ) {
      selectedIndex = i;
      minDistance = distance;
    }
  }

  isDragging = false;

}


              
            
!
999px

Console