Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <style class="keyframes-generator"></style>

<div class="content">

  <h3>CSS Elastic ease generator</h3>

  <section class="generator">

    <div class="svgContainer">
      <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewbox="0 0 600 360">
        <g class="grid"></g>
        <g class="line"></g>
      </svg>
      <div class="ball"></div>
    </div>

    <div class="settings">

      <div class="controls">

        Steps:
        <input type="range" class="steps" min="3" max="13" value="8">

        Deceleration:
        <input type="range" class="deceleration" min="1" max="3" step="0.01" value="1.62">

        Velocity:
        <input type="range" class="velocity" min="10" max="100" step="1" value="75">

        Duration:
        <input type="range" class="duration" min="0.5" max="5" step="0.1" value="3">

        Property:
        <select name="property">
          <option value="width: ~~~">Width</option>
          <option value="height: ~~~">Height</option>
          <option value="top: ~~~">Top</option>
          <option value="left: ~~~">Left</option>
          <option value="transform: translateX(~~~)">Transform - TranslateX</option>
          <option value="transform: translateY(~~~)">Transform - TranslateY</option>
        </select>

        <div class="values">
          <div>
            Units:
            <select name="units">
              <option>%</option>
              <option>px</option>
              <option>em</option>
              <option>rem</option>
              <option>ch</option>
              <option value="">None</option>
            </select>
          </div>

          <div>
            Start value:
            <input type="number" name="startValue" value="0">
          </div>

          <div>
            End value:
            <input type="number" name="endValue" value="100">
          </div>
        </div>

      </div>    

      <code class="output">
        <h3>Output code:</h3>
        <div class="outputCode"></div>
      </code>
    </div>

  </section>
</div>

              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');

*, *::before, *::after {
  padding: 0;
  margin: 0 auto;
  box-sizing: border-box;
}

body {
  font-family: 'Open Sans', sans-serif;
  background-image: repeating-linear-gradient(60deg, #000, #111, #000 5px);
  color: #888;
  padding: 2em;
}

.content {
  width: 600px;
}

.generator {
  width: 100%;

  .svgContainer {
    position: relative;
    width: 600px;
    height: 360px;

    & svg {
      width: 100%;
      height: 100%;
    }

    & .grid path {
      stroke: #ccc;
      stroke-width: 1px;
      fill: none;
      stroke-dasharray: 4px 1px;
    }

    & .line path {
      stroke: red;
      stroke-width: 3px;
      fill: none;
    }

    & .ball {
      position: absolute;
      top: 100%; left: 0;
      transform: translate(-50%, -50%);
      width: 1em; height: 1em;
      border-radius: 50%;
      background-color: red;

      @keyframes ballLeft {
        from { left: 0%; }
        to { left: 100%; }
      }
    }
  }

  .settings {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 1em;
    margin-top: 2em;

    & > div {
      width: 100%;
    }
  }

  .values {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 1em;

    & > div {
      width: 100%;
    }
  }

  .output {
    width: 100%;
    padding: 1em;
    background-color: #000;
    border: 1px solid;

    & > h3 {
      border-bottom: 1px solid;
      margin-bottom: 1em;
    }
  }
}

input, select {
  display: block;
  width: 100%;
  margin-bottom: 1em;
  font-size: inherit;
  font-family: inherit;
}

input[type="text"], input[type="number"], select {
  padding: 0.5em;
}
              
            
!

JS

              
                const svgWidth = 600;
const grid = document.querySelector('.grid');
const line = document.querySelector('.line');

document.querySelector('.controls').querySelectorAll(['input', 'select']).forEach(element => {
	element.addEventListener('input', drawGraph);
});

drawGraph();
function drawGraph() {

    const stepsCount = document.querySelector('.steps').value;
    const deceleration = document.querySelector('.deceleration').value;
    const velocity = document.querySelector('.velocity').value;
    const duration = document.querySelector('.duration').value;

    const property = document.querySelector('[name="property"]').value;
    const units = document.querySelector('[name="units"]').value;
    const startValue = Number(document.querySelector('[name="startValue"]').value);
    const endValue = Number(document.querySelector('[name="endValue"]').value);

    console.log({property});
    const keyfarmes = {
        "0%": startValue
    }
    const keyfarmesB = {
        "0%": "100%"
    }
    const stepsPosition = [0];
    let stepSize = 1;
    let lastStep = 0;
    
    for (let i = 0 ; i < stepsCount; i++) {
        const thisStep = lastStep + stepSize;
        stepsPosition.push(thisStep);
        stepSize = stepSize / deceleration;
        lastStep = thisStep;
    }

    grid.innerHTML = `<path d="M 0 170 V 360 M 0 180 h ${svgWidth} M 0 360 h ${svgWidth}" />`;

    const stepsValues = stepsPosition.map((x, ix) => {
        const dir = ix % 2 === 0 ? -1 : 1;
        const thisVelocity = (velocity * ((lastStep - x) / lastStep * dir));
        return thisVelocity;
    });

    const totalStepValue = lastStep;
    lastStep = 0;
    let linePath = 'M 0 360 ';

    stepsPosition.forEach((step, ix) => {
        if (step) {
            const svgStep = step * svgWidth / totalStepValue;
            const stepMidPointA = lastStep + ((svgStep - lastStep) * 0.425)
            const stepMidPointB = lastStep + ((svgStep - lastStep) * 0.575)
            const lastValue = ((ix - 1) === 0) ? 360 : (180 - (stepsValues[ix-1] * 1.8));
            const thisValue = 180 - (stepsValues[ix] * 1.8);
            linePath += `C ${stepMidPointA} ${lastValue} ${stepMidPointB} ${thisValue} ${svgStep} ${thisValue} `;
            lastStep = svgStep;
            grid.insertAdjacentHTML(
                'beforeend', `<path d="M ${svgStep} ${Math.min(170, thisValue - 10)} V 360" />`
            )

            const keyframePosition = (step * 100 / totalStepValue).toFixed(2) + "%";
            const keyframeValue = (endValue + (stepsValues[ix] * (endValue - startValue)) / 100).toFixed(2);
            const keyframeValueB = (50 + (stepsValues[ix] * -50) / 100).toFixed(2);
            keyfarmes[keyframePosition] = keyframeValue;
            keyfarmesB[keyframePosition] = keyframeValueB;
        }
        
    });

    line.innerHTML = `<path d="${linePath}" />`;

    const output = Object.entries(keyfarmes).reduce((a, b) => {
        return a + "&nbsp; " + b[0] + " { " + property.replace('~~~', b[1] + units) + "; }<br>";
    }, '');

    console.log({output});

    const outputB = Object.entries(keyfarmesB).reduce((a, b) => {
        return a + b[0] + " { top: " + b[1] + "% } ";
    }, '');

    document.querySelector('.keyframes-generator').innerHTML = `
        @keyframes elastic {
            ${outputB}
        }
    `;
    
    document.querySelector('.ball').style.animation = `
        elastic ${duration}s ease-in-out infinite,
        ballLeft ${duration}s linear infinite`;
    
    document.querySelector('.outputCode').innerHTML = `

        .elasticObject {<br>
            &nbsp; animation-name: elastic;<br>
            &nbsp; animation-duration: ${duration}s;<br>
            &nbsp; animation-fill-mode: forwards;<br>
            &nbsp; animation-timing-function: ease-in-out;<br>
        }<br>
        <br>
        
        @keyframes elastic {<br>
            ${output}
        }
    `;
}
              
            
!
999px

Console