Skip to content

Instantly share code, notes, and snippets.

@rikschennink
Last active December 29, 2017 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikschennink/bd3964cafe2ea37f00b2edd4c8c2728d to your computer and use it in GitHub Desktop.
Save rikschennink/bd3964cafe2ea37f00b2edd4c8c2728d to your computer and use it in GitHub Desktop.
const easeInOutSine = t => -0.5 * (Math.cos(Math.PI * t) - 1);
export const addGradientSteps = (
// the gradient to add the steps to
gradient,
// the color of the gradient (in rgb) and target alpha value
color = [0, 0, 0], alpha = 1,
// the ease function to use and the amount of steps to render (higher is more precision)
easeFn = easeInOutSine, steps = 10,
// the stop offset of the gradient, where does it start
offset = 0
) => {
const range = 1 - offset;
const rgb = color.join(',');
for (let i = 0; i <= steps; i++) {
// get current linear progress between 0 and 1
const p = i / steps;
// calculate the position of the stop based on current progress and offset
const stop = offset + range * p;
// add the color stop to the gradient, the alpha is calculated based on the easing function and the target alpha
gradient.addColorStop(stop, `rgba(${rgb}, ${easeFn(p) * alpha})`);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment