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

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.

            
              <div class="bar" id ="a"></div>
<div class="bar" id ="b"></div>
<div class="bar" id ="c"></div>
<div class="bar" id ="d"></div>
<div class="bar" id ="e"></div>
<div class="bar" id ="f"></div>
<div class="bar" id ="g"></div>
            
          
!
            
              body {
  background: #202020
}

.bar {
  height: 25px;
  width: 1px;
  transform-origin: 0px 0px;
  margin: 15px;
}

#a { background: #ffc107; }
#b {  background: #9c27b0; }
#c {  background: #ff5722; }
#d {  background: #2196f3; }
#e {  background: #9e9e9e; }
#f {  background: #009688; }
#g {  background: #f44336; }
            
          
!
            
              /**
* @desc Basic linear value animation that can accept simple easing functions and provides update & complete callbacks
* @param  {Object} values - Object with numerical values. eg. { value1: 0, value2: 20, someKey: 55 }
* @param  {Number} duration - How long (in milliseconds) the animation will be
* @param  {Object} options - target values, update callback & complete callback
* @param  {Function} [options.onComplete=(values) => values] - Callback that fires once animation is complete
* @param  {Function} [options.onUpdate=(values) => values] - Callback that fires when animation frame updates
* @param  {Function} [options.ease=(t) => t] - easing method eg. https://gist.github.com/gre/1650294
* @example
 const a = document.getElementById('a')
 animateValues({ a: 0 }, 800, {
    a: 500,
    onUpdate: v => a.style.transform = 'scaleX('+ v.a +')',
    onComplete: v => alert('Done!'),
    ease: t => t<.5 ? 2*t*t : -1+(4-2*t)*t, // From: https://gist.github.com/gre/1650294 
  })
*/
function animateValues(values, duration, options) {
  // Linear interpolation
  const lerp = (source, target, amount) => source + amount * (target - source)
  
  // Validation methods
  const checkNum = n => typeof n === 'number' ? n : null
  const checkFunc = f => typeof f === 'function' ? f : _ => _
  
  // Ensure methods. 
  const onComplete = checkFunc(options.onComplete)
  const onUpdate = checkFunc(options.onUpdate)
  const ease = checkFunc(options.ease)
  
  // Animation start time
  const start = Date.now()
  
  // Create a map <key: [from, to]>
  const animationMap = Object.keys(values).reduce((map, key) => {
    const _from = checkNum(values[key])
    const _to = checkNum(options[key])
    if (_from !== null && _to !== null) map[key] = [_from, _to]
    return map
  }, {})
  
  // List of animating values
  const keys = Object.keys(animationMap)
  
  // Create & run animation function
  const animation = () => {
    const now = Date.now()
    let t = duration > 0 ? (now - start) / duration : 1
    
    // Update all values using 't'
    keys.forEach(key => {
      // If both 'from' and 'to' are numbers: animate!
      const [_from, _to] = animationMap[key]
      const progress = ease(t, _from, _to, duration)
      // Update value
      values[key] = lerp(_from, _to, progress)
    })
    
    // If complete..
    if (t >= 1) {
      // Final update for all keys
      keys.forEach(key => (values[key] = options[key]))
      onUpdate(values)
      onComplete(values)
    } else {
      // Run update callback and loop until finished
      onUpdate(values)
      requestAnimationFrame(animation)
    }
  }
  animation()
}

// https://gist.github.com/gre/1650294
/*
 * Easing Functions - inspired from http://gizma.com/easing/
 * only considering the t value for the range [0, 1] => [0, 1]
 */
const EasingFunctions = {
  // no easing, no acceleration
  linear: function (t) { return t },
  // accelerating from zero velocity
  easeInQuad: function (t) { return t*t },
  // decelerating to zero velocity
  easeOutQuad: function (t) { return t*(2-t) },
  // acceleration until halfway, then deceleration
  easeInOutQuad: function (t) { return t<.5 ? 2*t*t : -1+(4-2*t)*t },
  // accelerating from zero velocity 
  easeInCubic: function (t) { return t*t*t },
  // decelerating to zero velocity 
  easeOutCubic: function (t) { return (--t)*t*t+1 },
  // acceleration until halfway, then deceleration 
  easeInOutCubic: function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 },
  // accelerating from zero velocity 
  easeInQuart: function (t) { return t*t*t*t },
  // decelerating to zero velocity 
  easeOutQuart: function (t) { return 1-(--t)*t*t*t },
  // acceleration until halfway, then deceleration
  easeInOutQuart: function (t) { return t<.5 ? 8*t*t*t*t : 1-8*(--t)*t*t*t },
  // accelerating from zero velocity
  easeInQuint: function (t) { return t*t*t*t*t },
  // decelerating to zero velocity
  easeOutQuint: function (t) { return 1+(--t)*t*t*t*t },
  // acceleration until halfway, then deceleration 
  easeInOutQuint: function (t) { return t<.5 ? 16*t*t*t*t*t : 1+16*(--t)*t*t*t*t },
  // elastic bounce effect at the beginning
  easeInElastic: function (t) { return (.04 - .04 / t) * Math.sin(25 * t) + 1 },
  // elastic bounce effect at the end
  easeOutElastic: function (t) { return .04 * t / (--t) * Math.sin(25 * t) },
  // elastic bounce effect at the beginning and end
  easeInOutElastic: function (t) { return (t -= .5) < 0 ? (.01 + .01 / t) * Math.sin(50 * t) : (.02 - .01 / t) * Math.sin(50 * t) + 1 }
}


// ----- EXAMPLE ------

const a = document.getElementById('a')
const b = document.getElementById('b')
const c = document.getElementById('c')
const d = document.getElementById('d')
const e = document.getElementById('e')
const f = document.getElementById('f')
const g = document.getElementById('g')

const v = { 
  a: 1, 
  b: 1,
  c: 1,
  d: 1,
  e: 1,
  f: 1,
  g: 1,
}

const duration = 2000

const easeKeys = Object.keys(EasingFunctions)

setInterval(() => {
  const easeKey = easeKeys[Math.floor(Math.random() * easeKeys.length)]
  animateValues(v, duration, {
    a: Math.random() * 500,
    b: Math.random() * 500,
    c: Math.random() * 500,
    d: Math.random() * 500,
    e: Math.random() * 500,
    f: Math.random() * 500,
    g: Math.random() * 500,
    onUpdate: () => {
      a.style.transform = 'scaleX('+ v.a +')'
      b.style.transform = 'scaleX('+ v.b +')'
      c.style.transform = 'scaleX('+ v.c +')'
      d.style.transform = 'scaleX('+ v.d +')'
      e.style.transform = 'scaleX('+ v.e +')'
      f.style.transform = 'scaleX('+ v.f +')'
      g.style.transform = 'scaleX('+ v.g +')'
    },
    onComplete: () => {
      console.log(easeKey)
    },
    ease: EasingFunctions[easeKey],
  })
  
}, duration)


            
          
!
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