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

              
                <main>
  <div class="result">
    <div class="box"></div>
  </div>
  <p>Generate the equivalent CSS easing from popular <a href="https://greensock.com/docs/v3/Eases" target="_blank" rel="noopener noreferrer">GreenSock easing strings</a>. Enter a string like <code>bounce.inOut</code> and get the <code>linear()</code> easing function back.</p>
  <form>
    <label class="sr-only" for="input">Input GSAP easing string</label>
    <input required=true type="text" placeholder="Input GSAP easing string" value="bounce.out"/>
    <button type="submit">Generate</button>
  </form>
  <pre><code class="language-css"></code></pre>

</main>
<style id="custom-ease"></style>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  align-content: start;
  gap: 1rem;
  padding: 1rem;
  background: hsl(0 0% 98%);
}

main {
  display: grid;
  gap: 2rem;
}

input, button {
  padding: 0.5rem 1rem;
}

.result {
  height: max(48px, 10vmin);
  width: 100%;
  background: hsl(0 0% 80%);
  container-type: inline-size;
}

.box {
  height: 100%;
  aspect-ratio: 1;
  background: hsl(80, 97%, 41%);
  animation: travel 2s infinite var(--custom, ease-in);
}

a, p > code {
  color: hsl(80, 97%, 41%);
  font-weight: bold;
}

@keyframes travel {
  0%, 25% {
    translate: 0 0;
  }
  75%, 100% {
    translate: calc(100cqi - 100%) 0;
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* All the Prism JS styling */
/* PrismJS 1.23.0
https://prismjs.com/download.html#themes=prism-tomorrow&languages=css+css-extras&plugins=line-numbers+inline-color+toolbar+copy-to-clipboard */
/**
 * prism.js tomorrow night eighties for JavaScript, CoffeeScript, CSS and HTML
 * Based on https://github.com/chriskempson/tomorrow-theme
 * @author Rose Pritchard
 */

code[class*="language-"],
pre[class*="language-"] {
  color: #ccc;
  background: none;
  font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
  font-size: 1em;
  text-align: left;
  white-space: pre;
  word-spacing: normal;
  word-break: normal;
  word-wrap: normal;
  line-height: 1.5;

  -moz-tab-size: 4;
  -o-tab-size: 4;
  tab-size: 4;

  -webkit-hyphens: none;
  -moz-hyphens: none;
  -ms-hyphens: none;
  hyphens: none;

}

/* Code blocks */
pre[class*="language-"] {
  padding: 2rem;
  margin: 0;
  overflow: auto;
  outline: transparent;
}

:not(pre) > code[class*="language-"],
pre[class*="language-"] {
  background: #2d2d2d;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
  padding: .1em;
  border-radius: .3em;
  white-space: normal;
}

pre {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.token.comment,
.token.block-comment,
.token.prolog,
.token.doctype,
.token.cdata {
  color: #999;
}

.token.punctuation {
  color: #ccc;
}

.token.tag,
.token.attr-name,
.token.namespace,
.token.deleted {
  color: #e2777a;
}

.token.function-name {
  color: #6196cc;
}

.token.boolean,
.token.number,
.token.function {
  color: #f08d49;
}

.token.property,
.token.class-name,
.token.constant,
.token.symbol {
  color: #f8c555;
}

.token.selector,
.token.important,
.token.atrule,
.token.keyword,
.token.builtin {
  color: #cc99cd;
}

.token.string,
.token.char,
.token.attr-value,
.token.regex,
.token.variable {
  color: #7ec699;
}

.token.operator,
.token.entity,
.token.url {
  color: #67cdcc;
}

.token.important,
.token.bold {
  font-weight: bold;
}
.token.italic {
  font-style: italic;
}

.token.entity {
  cursor: help;
}

.token.inserted {
  color: green;
}

pre[class*="language-"].line-numbers {
  position: relative;
  padding-left: 3.8em;
  counter-reset: linenumber;
}

pre[class*="language-"].line-numbers > code {
  position: relative;
  white-space: inherit;
}

.line-numbers .line-numbers-rows {
  position: absolute;
  pointer-events: none;
  top: 0;
  font-size: 100%;
  left: -3.8em;
  width: 3em; /* works for line-numbers below 1000 lines */
  letter-spacing: -1px;
  border-right: 1px solid #999;

  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;

}

  .line-numbers-rows > span {
    display: block;
    counter-increment: linenumber;
  }

    .line-numbers-rows > span:before {
      content: counter(linenumber);
      color: #999;
      display: block;
      padding-right: 0.8em;
      text-align: right;
    }

span.inline-color-wrapper {
  /*
   * The background image is the following SVG inline in base 64:
   *
   * <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2 2">
   *     <path fill="gray" d="M0 0h2v2H0z"/>
   *     <path fill="white" d="M0 0h1v1H0zM1 1h1v1H1z"/>
   * </svg>
   *
   * SVG-inlining explained:
   * https://stackoverflow.com/a/21626701/7595472
   */
  background: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyIDIiPjxwYXRoIGZpbGw9ImdyYXkiIGQ9Ik0wIDBoMnYySDB6Ii8+PHBhdGggZmlsbD0id2hpdGUiIGQ9Ik0wIDBoMXYxSDB6TTEgMWgxdjFIMXoiLz48L3N2Zz4=");
  /* This is to prevent visual glitches where one pixel from the repeating pattern could be seen. */
  background-position: center;
  background-size: 110%;

  display: inline-block;
  height: 1.333ch;
  width: 1.333ch;
  margin: 0 .333ch;
  box-sizing: border-box;
  border: 1px solid white;
  outline: 1px solid rgba(0,0,0,.5);
  overflow: hidden;
}

span.inline-color {
  display: block;
  /* To prevent visual glitches again */
  height: 120%;
  width: 120%;
}

div.code-toolbar {
  height: 100%;
  position: relative;
}

div.code-toolbar > .toolbar {
  position: absolute;
  top: .3em;
  right: .2em;
  opacity: 1;
}

div.code-toolbar:hover > .toolbar {
  opacity: 1;
}

/* Separate line b/c rules are thrown out if selector is invalid.
   IE11 and old Edge versions don't support :focus-within. */
div.code-toolbar:focus-within > .toolbar {
  opacity: 1;
}

div.code-toolbar > .toolbar .toolbar-item {
  display: inline-block;
}

div.code-toolbar > .toolbar a {
  cursor: pointer;
}

div.code-toolbar > .toolbar button {
  background: none;
  border: 0;
  color: inherit;
  font: inherit;
  line-height: normal;
  overflow: visible;
  padding: 0;
  -webkit-user-select: none; /* for button */
  -moz-user-select: none;
  -ms-user-select: none;
}

div.code-toolbar > .toolbar a,
div.code-toolbar > .toolbar button {
  color: #bbb;
  font-size: 1rem;
  padding: 0.5rem;
  font-family: sans-serif;
  background: hsl(0, 0%, 25%);
  border-radius: .5em;
  outline: transparent;
  cursor: pointer;
}

div.code-toolbar > .toolbar a:hover,
div.code-toolbar > .toolbar a:focus,
div.code-toolbar > .toolbar button:hover,
div.code-toolbar > .toolbar button:focus,
div.code-toolbar > .toolbar span:hover,
div.code-toolbar > .toolbar span:focus {
  background: hsl(0, 0%, 40%);
  text-decoration: none;
}


/* Code theming */
pre {
  border-radius: 1rem;
}

p {
  margin: 0;
  width: 50ch;
  max-width: 100%;
}
              
            
!

JS

              
                import gsap from 'https://cdn.skypack.dev/gsap'
import Prism from 'https://cdn.skypack.dev/prismjs'

console.clear()

const CODE = document.querySelector('pre > code')
const INPUT = document.querySelector('input')
const FORM = document.querySelector('form')
const STYLE = document.querySelector('#custom-ease')
const RESULT = document.querySelector('.result')
const pointsLength = 10_000
const lineLength = 40

/**
 * ALL THE MAGIC STUFF IN HERE FROM LINEAR IS
 * FROM JAKE ARCHIBALD – @JAFFATHECAKE
 * THE CODE IS MODIFIED SLIGHTLY TO WORK HERE AND FOR THE NEEDS I HAD
 * https://github.com/jakearchibald/linear-easing-generator/tree/main
 * **/
const durationFormat = new Intl.NumberFormat('en-US', {
  maximumFractionDigits: 3,
})

function useFriendlyLinearCode(parts, name) {
  if (parts.length === 0) return ''

  let linearStart = `  --${name}: linear(`
  let linearEnd = ');'
  let lines = []
  let line = ''

  const lineIndentSize = 2

  for (const part of parts) {
    // 1 for comma
    if (line.length + part.length + lineIndentSize + 1 > lineLength) {
      lines.push(line + ',')
      line = ''
    }
    if (line) line += ', '
    line += part
  }

  if (line) lines.push(line)

  if (
    lines.length === 1 &&
    linearStart.length + lines[0].length + linearEnd.length < lineLength
  ) {
    return linearStart + lines[0] + linearEnd
  }

  return (
    linearStart +
    '\n    ' +
    lines.join('\n    ') +
    '\n  ' +
    linearEnd
  )
}
/**
 * The main part of the linear generation
 * */

function useLinearSyntax(points, round) {
  if (!points) return []
  const xFormat = new Intl.NumberFormat('en-US', {
    maximumFractionDigits: Math.max(round - 2, 0),
  })
  const yFormat = new Intl.NumberFormat('en-US', {
    maximumFractionDigits: round,
  })

  const pointsValue = points
  const valuesWithRedundantX = new Set()
  const maxDelta = 1 / 10 ** round

  // Figure out entries that don't need an explicit position value
  for (const [i, value] of pointsValue.entries()) {
    const [x] = value
    // If the first item's position is 0, then we don't need to state the position
    if (i === 0) {
      if (x === 0) valuesWithRedundantX.add(value)
      continue
    }
    // If the last entry's position is 1, and the item before it is less than 1, then we don't need to state the position
    if (i === pointsValue.length - 1) {
      const previous = pointsValue[i - 1][0]
      if (x === 1 && previous <= 1) valuesWithRedundantX.add(value)
      continue
    }

    // If the position is the average of the previous and next positions, then we don't need to state the position
    const previous = pointsValue[i - 1][0]
    const next = pointsValue[i + 1][0]

    const averagePos = (next - previous) / 2 + previous
    const delta = Math.abs(x - averagePos)

    if (delta < maxDelta) valuesWithRedundantX.add(value)
  }

  // Group into sections with same y
  const groupedValues = [[pointsValue[0]]]

  for (const value of pointsValue.slice(1)) {
    if (value[1] === groupedValues.at(-1)[0][1]) {
      groupedValues.at(-1).push(value)
    } else {
      groupedValues.push([value])
    }
  }

  const outputValues = groupedValues.map((group) => {
    const yValue = yFormat.format(group[0][1])

    const regularValue = group
      .map((value) => {
        const [x] = value
        let output = yValue

        if (!valuesWithRedundantX.has(value)) {
          output += ' ' + xFormat.format(x * 100) + '%'
        }

        return output
      })
      .join(', ')

    if (group.length === 1) return regularValue

    // Maybe it's shorter to provide a value that skips steps?
    const xVals = [group[0][0], group.at(-1)[0]]
    const positionalValues = xVals
      .map((x) => xFormat.format(x * 100) + '%')
      .join(' ')

    const skipValue = `${yValue} ${positionalValues}`

    return skipValue.length > regularValue.length ? regularValue : skipValue
  })
  return outputValues
}

/**
 * Takes a string literal from the Apps interface and processes
 * before running it through the linear generator
 * */

function processEase(key, easingFunc) {
  return {
    name: key.replace(/[A-Z]/g, (match) => '-' + match.toLowerCase()),
    points: Array.from({ length: pointsLength }, (_, i) => {
      const pos = i / (pointsLength - 1)
      return [pos, Number(easingFunc(pos))]
    }),
    duration:
      'duration' in self && typeof self.duration === 'number'
        ? self.duration || 0
        : 0,
  }
}

/**
 * Last Magic??
 * */
function getSqSegDist(p, p1, p2) {
  let x = p1[0]
  let y = p1[1]
  let dx = p2[0] - x
  let dy = p2[1] - y

  if (dx !== 0 || dy !== 0) {
    var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy)

    if (t > 1) {
      x = p2[0]
      y = p2[1]
    } else if (t > 0) {
      x += dx * t
      y += dy * t
    }
  }

  dx = p[0] - x
  dy = p[1] - y

  return dx * dx + dy * dy
}

/**
 * More Magic
 * */
function simplifyDPStep(points, first, last, sqTolerance, simplified) {
  let maxSqDist = sqTolerance
  let index

  for (let i = first + 1; i < last; i++) {
    const sqDist = getSqSegDist(points[i], points[first], points[last])

    if (sqDist > maxSqDist) {
      index = i
      maxSqDist = sqDist
    }
  }

  if (maxSqDist > sqTolerance) {
    if (index - first > 1) {
      simplifyDPStep(points, first, index, sqTolerance, simplified)
    }

    simplified.push(points[index])

    if (last - index > 1) {
      simplifyDPStep(points, index, last, sqTolerance, simplified)
    }
  }
}

/**
 * Magic stuff goes here
 * */
function simplifyDouglasPeucker(points, tolerance) {
  if (points.length <= 1) return points
  const sqTolerance = tolerance * tolerance
  const last = points.length - 1
  const simplified = [points[0]]
  simplifyDPStep(points, 0, last, sqTolerance, simplified)
  simplified.push(points[last])

  return simplified
}

/**
 * Take points and optimize them for linear generation
 * simplify 0 -> 0.025
 * round 0 -> 5
 * */
function useOptimizedPoints(fullPoints, simplify = 0, round = 5) {
  if (!fullPoints) return null
  // x is represented as a percentage, so no point rounding less than 2 places
  const xRounding = Math.max(round, 2)

  return simplifyDouglasPeucker(fullPoints, simplify).map(([x, y]) => [
    Math.round(x * 10 ** xRounding) / 10 ** xRounding,
    Math.round(y * 10 ** round) / 10 ** round,
  ])
}

let easings = ''
let simplified = 0.0025
let rounded = 4

const generateLinear = (key, ease) => {
  const result = processEase(key, ease)
  const optimised = useOptimizedPoints(result.points, simplified, rounded)
  const linear = useLinearSyntax(optimised, rounded)
  const output = useFriendlyLinearCode(linear, result.name, 0)
  let outputStart = ':root {\n'
  let outputEnd = '\n}' 
  let styles = outputStart + output + outputEnd

  return styles
}


const HANDLE = event => {
  if (event) event.preventDefault()
  
  const ease = gsap.parseEase(INPUT.value)
  
  if (ease) {
    const result = generateLinear('custom', ease)
    STYLE.innerHTML = result
    CODE.innerHTML = result
    RESULT.innerHTML = ''
    requestAnimationFrame(() => {
      RESULT.innerHTML = '<div class="box"></div>'
      
    })
    Prism.highlightAll()
  }
}

FORM.addEventListener('submit', HANDLE)

HANDLE()
              
            
!
999px

Console