Overview
Theinterpolate() function creates an interpolator function that maps values from an input range to an output range. It’s a lower-level utility that powers the transform() function and useTransform() hook.
Import
Signature
Parameters
A linear series of numbers defining the input range. Must be the same length as the output range.
A series of values defining the output range. Can be:
- Numbers
- Colors (hex, rgb, rgba, hsl, hsla)
- Strings with embedded numbers
- Complex strings with multiple values
- Objects and arrays (with the same structure)
Optional configuration object
Options
Clamp output values to within the defined output range. Set to
false to allow extrapolation.Easing function(s) to apply during interpolation. If array, must be one item shorter than the ranges.
Custom mixer function for interpolating between output values.
Return Value
A function that takes an input value and returns the corresponding interpolated output value.
Examples
Basic Interpolation
Create a simple number interpolator:Color Interpolation
Interpolate between colors:Multi-Stop Interpolation
Create interpolators with multiple breakpoints:With Easing
Apply easing functions:Per-Segment Easing
Apply different easing to each segment:Without Clamping
Allow values outside the defined range:String with Units
Interpolate strings containing numbers:Complex Strings
Interpolate complex CSS values:Supported Value Types
Numbers
Numbers
Basic numeric interpolation:
Colors
Colors
Supports multiple color formats:
- Hex:
"#ff0000","#f00" - RGB:
"rgb(255, 0, 0)" - RGBA:
"rgba(255, 0, 0, 1)" - HSL:
"hsl(0, 100%, 50%)" - HSLA:
"hsla(0, 100%, 50%, 1)"
Strings with numbers
Strings with numbers
Automatically extracts and interpolates numbers:
Complex values
Complex values
Interpolates all numbers in complex strings:
Behavior Details
Range Direction
If the input range is descending (highest to lowest), both ranges are automatically reversed:Single Value
If only one input/output pair is provided, returns a constant function:Identical Values
If all output values are identical, returns the last value:Zero Delta Range
If input values are identical, special handling applies:Performance
The
interpolate() function creates the interpolator once and can be called repeatedly with minimal overhead. Interpolators are optimized for performance.Optimization Tips
Create once, use many times
Create once, use many times
Create interpolators outside render loops or animation frames:
Avoid complex mixers in hot paths
Avoid complex mixers in hot paths
Custom mixer functions run frequently. Keep them lightweight:
Prefer built-in mixers
Prefer built-in mixers
The default mixers for numbers, colors, and strings are highly optimized. Only use custom mixers when necessary.
API Reference
MixerFactory Type
Differences from transform()
| Feature | interpolate() | transform() |
|---|---|---|
| Returns | Interpolator function | Value or function |
| Immediate mode | No | Yes |
| Usage | Lower-level | Higher-level |
| Typical use | Internal/advanced | Public API |
Related
transform
Higher-level transformation function
useTransform
Transform motion values in React