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.

            
              
#help
  label(name='stiffness') Stifness: 
    b 30
  input(type='range' name='stiffness' min='0' max='100' step='0.1' value='30')
  p Stiffness of the spring. Higher values will create more sudden movement.

  label(name='damping') Damping: 
    b 1.5
  input(type='range' name='damping' min='0' max='5' step='0.1' value='1.5')
  p Damping, strength of opposing force. If set to 0, spring will oscillate indefinitely.
  
  label(name='mass') Mass: 
    b 0.1
  input(type='range' name='mass' min='0' max='0.5' step='0.001' value='0.1')
  p Mass of the moving object. Higher values will result in more lethargic movement.
  
  label(name='duration') Duration: 
    b 0ms

#circles
  .circle
  .circle
            
          
!
            
              *
  color: inherit
  font-size: inherit

body
  background: #1d1e22
  font-family: monospace
  padding: 50px
  color: #c6c8d4
  font-size: 1.2rem

#help
  max-width: 600px
  margin: 0 auto
  
  input
    width: 100%

  p
    margin-top: 0
    margin-bottom: 25px
    color: #63686f
    font-size: 0.8em

#circles
  max-width: 600px
  margin: 50px auto 50px
  display: flex
  flex-flow: row nowrap
  justify-content: space-around
  
  .circle
    width: 200px
    height: 200px
    border-radius: 100px
    z-index: 9999
    margin: 0 10px
    
    &:nth-child(1)
      left: calc(50% - 120px)
      background: radial-gradient(circle at 33% 33%, #5cabff, #000)
    
    &:nth-child(2)
      left: calc(50% + 120px)
      background: radial-gradient(circle at 33% 33%, #fb335c, #000)
            
          
!
            
              const SpringSystem = ( () => {

  class Spring {

    constructor( system, id, options = {} ) {

      options = Object.assign( {
        config: {},
        rest: {},
        onUpdate: ( () => {} ),
        onComplete: ( () => {} ),
      }, options );

      this._config = Object.assign( {
        stiffness: 30,
        damping: 1.5,
        mass: 0.1,
      }, options.config );

      this._rest = Object.assign( {
        delta: 0.001,
        velocity: 0.01,
      }, options.rest );

      this._onUpdate = options.onUpdate;
      this._onComplete = options.onComplete;

      this._system = system;
      this._id = id;
      this._active = false;

      this._position = 0;
      this._target = 0;
      this._velocity = 0;

      this._update = this._update.bind( this );

    }

    _update( delta ) {

      delta /= 1000;

      const spring = ( this._position - this._target ) * -this._config.stiffness;
      const damper = this._velocity * -this._config.damping;
      const accelaration = ( spring + damper ) / this._config.mass;

      this._velocity += accelaration * delta;
      this._position += this._velocity * delta;

      this._onUpdate( this._position );

      const positionThreshold = this._round( this._position, this._rest.delta ) == this._target;
      const velocityThreshold = this._round( this._velocity, this._rest.velocity ) == 0;

      if ( positionThreshold && velocityThreshold ) {

        this._position = this._target;

        this._stop();
        this._onUpdate( this._position );
        this._onComplete( this._position );

      }

    }

    _round( value, precision ) {

      return Math.round( value / precision ) * precision;

    }

    _start() {

      this._system._addSpring( this );

      return this;

    }

    _stop() {

      this._system._removeSpring( this );

      return this;

    }

    setConfig( config = {} ) {

      this._config = Object.assign( this._config, config );

      return this;

    }

    setRest( rest = {} ) {

      this._rest = Object.assign( this._rest, rest );

      return this;

    }

    setPosition( v ) {

      this._position = v;
      this._start();

      return this;

    }

    setTarget( v ) {

      this._target = v;
      this._start();

      return this;

    }

    setAtRest() {

      this._stop();

      return this;

    }

    onUpdate( callback ) {

      this._onUpdate = callback;

      return this;

    }

    onComplete( callback ) {

      this._onComplete = callback;

      return this;

    }

    isActive() {

      return this._active;

    }

  }

  class SpringSystem {

    constructor() {

      this._uniq = 0;
      this._ids = [];
      this._springs = {};
      this._update = this._update.bind( this );
      this._raf = 0;
      this._time = 0;

    }

    _update() {

      const now = performance.now();
      const delta = now - this._time
      this._time = now;

      let i = this._ids.length;

      this._raf = i ? requestAnimationFrame( this._update ) : 0;

      while ( i-- )
        this._springs[ this._ids[ i ] ] && this._springs[ this._ids[ i ] ]._update( delta );

    }

    _addSpring( spring ) {

      if ( this._ids.indexOf( spring._id ) < 0 ) {

        this._ids.push( spring._id );
        this._springs[ spring._id ] = spring;
        spring._active = true;

      }

      if ( this._raf !== 0 ) return;

      this._time = performance.now();
      this._raf = requestAnimationFrame( this._update );

    }

    _removeSpring( spring ) {

      const index = this._ids.indexOf( spring._id );

      if ( index < 0 ) return;

      this._ids.splice( index, 1 );
      delete this._springs[ spring._id ];
      spring._active = false;
      spring = null;

    }

    createSpring( options = {} ) {

      const spring = new Spring( this, this._uniq++, options );

      return spring;

    }

  }

  return SpringSystem;

} )();

/*
██████  ███████ ███    ███  ██████  
██   ██ ██      ████  ████ ██    ██ 
██   ██ █████   ██ ████ ██ ██    ██ 
██   ██ ██      ██  ██  ██ ██    ██ 
██████  ███████ ██      ██  ██████  
*/

let start;

const system = new SpringSystem()
const springs = [];

const durationLabel = document.querySelector( 'label[name="duration"] b' );
const circles = document.querySelectorAll( '.circle' );

circles.forEach( circle => {

  const spring = system.createSpring( {
    onUpdate: position => {

      circle.style.transform = `scale(${ 1 - position / 5 })`;

    },
    onComplete: () => {

      const duration = Date.now() - start;
      if ( duration > 50 ) durationLabel.innerHTML = `${duration}ms`;

    },
  } );

  springs.push( spring );

  circle.addEventListener( 'mousedown', e => {

    start = Date.now();
    spring.setTarget( 1 )

  }, false );

} );

document.onmouseup = event => {

  start = Date.now();

  springs.forEach( spring => spring.setTarget( 0 ) );

};

const setupRange = name => {

  const label = document.querySelector( `label[name="${name}"] b` );
  const input = document.querySelector( `input[name="${name}"]` );

  input.oninput = () => {

    label.innerHTML = input.value;

    springs.forEach( spring => spring._config[ name ] = input.value );

  }

}

setupRange( 'stiffness' );
setupRange( 'damping' );
setupRange( 'mass' );
            
          
!
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