Skip to content

toCssLinear

Samples an easing function into a CSS linear() easing, so any curve (a spring, a custom bounce) can be played by CSS transitions and the Web Animations API, which do not take JavaScript functions.

Values are rounded to 4 decimals. More samples follow the curve more closely, in a longer string; 32 is smooth for most springs.

Available since v3.1.5

import { toCssLinear } from '@helpers4/animation';
// or, from the all-in-one package (same code, one install):
import { toCssLinear } from 'helpers4/animation';
toCssLinear(easing: function, samples: number): string
ParameterTypeDescription
easingfunctionThe easing function over `[0, 1]`
samplesnumberNumber of points, evenly spaced in time, at least `2`. Defaults to `32`.

string — A linear(...) string

Pass the spring duration and its sampled curve.

const s = spring({ damping: 15 });
element.animate({ opacity: [0, 1] }, { duration: s.durationMs, easing: toCssLinear(s.easing) });

Fewer samples give a shorter string.

toCssLinear((t) => t * t, 5) // => 'linear(0, 0.0625, 0.25, 0.5625, 1)'

View source on GitHub