File size: 4,614 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
---
meta:
  title: Interpolation | React Spring
  'og:title': Interpolation | React Spring
  'twitter:title': Interpolation | React Spring
  description: An advanced guide to the interpolation API in React Spring.
  'og:description': An advanced guide to the interpolation API in React Spring.
  'twitter:description': An advanced guide to the interpolation API in React Spring.
  'og:url': https://www.react-spring.dev/docs/advanced/interpolation
  'twitter:url': https://www.react-spring.dev/docs/advanced/interpolation
sidebar_position: 6
---

import { formatFrontmatterToRemixMeta } from '../helpers/meta'

export const meta = formatFrontmatterToRemixMeta(frontmatter)

# Interpolation

## What is one?

An interpolation typically is a function that takes a value returns another value. You can even map ranges
of values  numbers are typically required as the input range. So for example, if you want to animate a value
from 0 to 100, you can use an interpolation to map the range 0 to 1 to 0 to 100.

## Basic Usage

### Extending the `SpringValue`

The most common use of our interpolation is to convert the value of a `SpringValue` to another value. This is
done by using the `to` method.

```tsx lines=10
import { useSpring, animated } from '@react-spring/web'

function MyComponent() {
  const props = useSpring({
    from: { x: 0 },
    to: { x: 360 },
  })

  return (
    <animated.div
      style={{ transform: props.x.to(value => `rotateZ(${value}deg)`) }}
    >
      Hello World
    </animated.div>
  )
}
```

### Using the `to` function

Similar to the example above, it's also possible to use our `to` function to convert `SpringValue`s.

```tsx lines=1,10
import { useSpring, animated, to } from '@react-spring/web'

function MyComponent() {
  const props = useSpring({
    from: { x: 0 },
    to: { x: 360 },
  })

  return (
    <animated.div
      style={{ transform: to(props.x, value => `rotateZ(${value}deg)`) }}
    >
      Hello World
    </animated.div>
  )
}
```

## Advanced Usage

### Combining values

A more advanced use of our interpolation is to combine multiple `SpringValue`s. This normally requires the
use of our `to` function to create.

```tsx lines=1,10
import { animated, to, useSpring } from '@react-spring/web'

export default function MyComponent() {
  const props = useSpring({
    from: { x: 0, y: 0, z: 0 },
    to: { x: 1, y: 1, z: 1 },
  })

  return (
    <animated.div
      style={{
        transform: to(
          [props.x, props.y, props.z],
          (x, y, z) => `rotate3d(${x}, ${y}, ${z}, 45deg)`
        ),
      }}
    >
      Hello World
    </animated.div>
  )
}
```

### Ranges and Outputs

The `to` function also accepts a range of input values as the first argument and the output of that range.
Interpolations can be chained, as seen in the example below we change a value `0-1` to `0-360` and then
interpolate it to a rotateZ value.

```tsx lines=10
import { useSpring, animated } from '@react-spring/web'

function MyComponent() {
  const props = useSpring({
    from: { x: 0 },
    to: { x: 1 },
  })

  return (
    <animated.div
      style={{
        transform: props.x
          .to([0, 1], [0, 360])
          .to(value => `rotateZ(${value}deg)`),
      }}
    >
      Hello World
    </animated.div>
  )
}
```

## Config

An interpolation can also take a config object as the second argument if using the `to` function or the first
argument when using the `to` method of a `SpringValue`.

import { TablesConfiguration } from '../components/Tables/TablesConfig'
import { INTERPOLATONS_DATA } from '../data/fixtures'

<TablesConfiguration data={INTERPOLATONS_DATA} />

## Overriding the global to function

If you decide to, it is possible to override the global Interpolation factory which is what's called when `to` is used.

```ts
import { Globals } from '@react-spring/web'

Globals.assign({
  to: (source, args) => new CustomInterpolation(source, args),
})
```

## Typescript

```ts
function to<Input extends ReadonlyArray<any>, Output>(
  parents: Input,
  interpolator: (...args: Interpolated<Input>) => Output
): Interpolation<Output>

function to<Input, Output>(
  parent: FluidValue<Input> | Input,
  interpolator: InterpolatorFn<Input, Output>
): Interpolation<Output>

function to<Out>(
  parents: FluidValue[],
  config: InterpolatorConfig<Out>
): Interpolation<Animatable<Out>>

function to<Out>(
  parents: Array<FluidValue<number>> | FluidValue<number[]>,
  range: readonly number[],
  output: readonly Constrain<Out, Animatable>[],
  extrapolate?: 'identity' | 'clamp' | 'extend'
): Interpolation<Animatable<Out>>
```