Created
March 21, 2020 22:53
-
-
Save mikehwagz/ab98ab186a090a6c1e50c2c7bebd5f2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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