Skip to content

Instantly share code, notes, and snippets.

@mikehwagz
Created March 21, 2020 22:53
Show Gist options
  • Save mikehwagz/ab98ab186a090a6c1e50c2c7bebd5f2f to your computer and use it in GitHub Desktop.
Save mikehwagz/ab98ab186a090a6c1e50c2c7bebd5f2f to your computer and use it in GitHub Desktop.
import cubicBezier from 'bezier-easing'
import { map as mapRange, round } from '@/util/math'
export function easedGradient({ direction, rgb, steps, bezier }) {
let ease = cubicBezier(...bezier)
let vals = Array(steps)
.fill()
.map((_, i) => {
const percent = round(mapRange(i, 0, steps - 1, 0, 1))
const alpha = 1 - ease(percent)
return { percent: percent * 100, alpha }
})
const getColorStop = ({ rgb, alpha, percent }) =>
`rgba(${rgb.join(', ')}, ${alpha}) ${percent}%`
return `linear-gradient(
${direction},
${vals
.map(({ alpha, percent }) => getColorStop({ rgb, alpha, percent }))
.join(',\n')}
)`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment