File size: 5,551 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import { is, easings } from '@react-spring/shared'
import { EasingFunction } from '@react-spring/types'
import { config as configs } from './constants'
const defaults: any = {
...configs.default,
mass: 1,
damping: 1,
easing: easings.linear,
clamp: false,
}
export class AnimationConfig {
/**
* With higher tension, the spring will resist bouncing and try harder to stop at its end value.
*
* When tension is zero, no animation occurs.
*
* @default 170
*/
tension!: number
/**
* The damping ratio coefficient, or just the damping ratio when `speed` is defined.
*
* When `speed` is defined, this value should be between 0 and 1.
*
* Higher friction means the spring will slow down faster.
*
* @default 26
*/
friction!: number
/**
* The natural frequency (in seconds), which dictates the number of bounces
* per second when no damping exists.
*
* When defined, `tension` is derived from this, and `friction` is derived
* from `tension` and `damping`.
*/
frequency?: number
/**
* The damping ratio, which dictates how the spring slows down.
*
* Set to `0` to never slow down. Set to `1` to slow down without bouncing.
* Between `0` and `1` is for you to explore.
*
* Only works when `frequency` is defined.
*
* @default 1
*/
damping!: number
/**
* Higher mass means more friction is required to slow down.
*
* Defaults to 1, which works fine most of the time.
*
* @default 1
*/
mass!: number
/**
* The initial velocity of one or more values.
*
* @default 0
*/
velocity: number | number[] = 0
/**
* The smallest velocity before the animation is considered "not moving".
*
* When undefined, `precision` is used instead.
*/
restVelocity?: number
/**
* The smallest distance from a value before that distance is essentially zero.
*
* This helps in deciding when a spring is "at rest". The spring must be within
* this distance from its final value, and its velocity must be lower than this
* value too (unless `restVelocity` is defined).
*
* @default 0.01
*/
precision?: number
/**
* For `duration` animations only. Note: The `duration` is not affected
* by this property.
*
* Defaults to `0`, which means "start from the beginning".
*
* Setting to `1+` makes an immediate animation.
*
* Setting to `0.5` means "start from the middle of the easing function".
*
* Any number `>= 0` and `<= 1` makes sense here.
*/
progress?: number
/**
* Animation length in number of milliseconds.
*/
duration?: number
/**
* The animation curve. Only used when `duration` is defined.
*
* Defaults to quadratic ease-in-out.
*/
easing!: EasingFunction
/**
* Avoid overshooting by ending abruptly at the goal value.
*
* @default false
*/
clamp!: boolean
/**
* When above zero, the spring will bounce instead of overshooting when
* exceeding its goal value. Its velocity is multiplied by `-1 + bounce`
* whenever its current value equals or exceeds its goal. For example,
* setting `bounce` to `0.5` chops the velocity in half on each bounce,
* in addition to any friction.
*/
bounce?: number
/**
* "Decay animations" decelerate without an explicit goal value.
* Useful for scrolling animations.
*
* Use `true` for the default exponential decay factor (`0.998`).
*
* When a `number` between `0` and `1` is given, a lower number makes the
* animation slow down faster. And setting to `1` would make an unending
* animation.
*
* @default false
*/
decay?: boolean | number
/**
* While animating, round to the nearest multiple of this number.
* The `from` and `to` values are never rounded, as well as any value
* passed to the `set` method of an animated value.
*/
round?: number
constructor() {
Object.assign(this, defaults)
}
}
export function mergeConfig(
config: AnimationConfig,
newConfig: Partial<AnimationConfig>,
defaultConfig?: Partial<AnimationConfig>
): typeof config
export function mergeConfig(
config: any,
newConfig: object,
defaultConfig?: object
) {
if (defaultConfig) {
defaultConfig = { ...defaultConfig }
sanitizeConfig(defaultConfig, newConfig)
newConfig = { ...defaultConfig, ...newConfig }
}
sanitizeConfig(config, newConfig)
Object.assign(config, newConfig)
for (const key in defaults) {
if (config[key] == null) {
config[key] = defaults[key]
}
}
let { frequency, damping } = config
const { mass } = config
if (!is.und(frequency)) {
if (frequency < 0.01) frequency = 0.01
if (damping < 0) damping = 0
config.tension = Math.pow((2 * Math.PI) / frequency, 2) * mass
config.friction = (4 * Math.PI * damping * mass) / frequency
}
return config
}
// Prevent a config from accidentally overriding new props.
// This depends on which "config" props take precedence when defined.
function sanitizeConfig(
config: Partial<AnimationConfig>,
props: Partial<AnimationConfig>
) {
if (!is.und(props.decay)) {
config.duration = undefined
} else {
const isTensionConfig = !is.und(props.tension) || !is.und(props.friction)
if (
isTensionConfig ||
!is.und(props.frequency) ||
!is.und(props.damping) ||
!is.und(props.mass)
) {
config.duration = undefined
config.decay = undefined
}
if (isTensionConfig) {
config.frequency = undefined
}
}
}
|