Overview
Thetransform() function maps numbers from one range to another. It’s useful for creating relationships between values, like scroll position to opacity, or mouse position to rotation.
Import
Signatures
Immediate Transformation
Transform a value immediately:Transformer Function
Create a reusable transform function:Parameters
The value to transform (only in immediate mode)
An array of numbers defining the input range. Must be the same length as
outputRange. Can be ascending or descending.An array of values defining the output range. Supports:
- Numbers
- Colors (hex, rgb, rgba, hsl, hsla)
- Strings with numbers (e.g., “10px”, “45deg”)
- Complex strings (e.g., “0px 5px 10px rgba(0,0,0,0.5)”)
Optional configuration object
Options
Whether to clamp the output value within the output range. Set to
false to allow extrapolation beyond the defined ranges.Easing function(s) to apply between values. If an array, must be one item shorter than the ranges (one easing per transition).
Custom function to interpolate between output values. Useful for custom value types.
Examples
Basic Number Transformation
Map scroll position to opacity:Color Transformation
Interpolate between colors:Multiple Breakpoints
Create complex relationships with multiple stops:With Easing
Apply easing functions for non-linear transitions:Multiple Easings
Different easing for each segment:Without Clamping
Allow extrapolation beyond defined ranges:String Transformations
Interpolate strings with units:With useTransform
Combine with motion values:Reverse Ranges
Input ranges can be descending (reversed):Advanced Usage
Custom Mixer
Provide custom interpolation logic:Complex Value Interpolation
Interpolate complex CSS values:Return Types
The return type matches the output range type:number[]outputs returnnumberstring[]outputs returnstring- Mixed types are supported via union types
Best Practices
Match range lengths
Match range lengths
Always ensure
inputRange and outputRange have the same length. The function will throw an error if they don’t match.Use linear input ranges
Use linear input ranges
Input ranges should be strictly ascending or descending. Non-linear input ranges may produce unexpected results.
Reuse transform functions
Reuse transform functions
Create transform functions once and reuse them rather than calling
transform() repeatedly with the same ranges.Clamp by default
Clamp by default
Keep clamping enabled unless you specifically need extrapolation. This prevents unexpected values outside your defined range.
Related Functions
interpolate
Lower-level interpolation function
useTransform
Transform motion values in React
useScroll
Track scroll position