File size: 1,274 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
import { is } from '@react-spring/shared'
import { Animated, Payload } from './Animated'

/** An animated number or a native attribute value */
export class AnimatedValue<T = any> extends Animated {
  done = true
  elapsedTime!: number
  lastPosition!: number
  lastVelocity?: number | null
  v0?: number | null
  durationProgress = 0

  constructor(protected _value: T) {
    super()
    if (is.num(this._value)) {
      this.lastPosition = this._value
    }
  }

  /** @internal */
  static create(value: any) {
    return new AnimatedValue(value)
  }

  getPayload(): Payload {
    return [this]
  }

  getValue() {
    return this._value
  }

  setValue(value: T, step?: number) {
    if (is.num(value)) {
      this.lastPosition = value
      if (step) {
        value = (Math.round(value / step) * step) as any
        if (this.done) {
          this.lastPosition = value as any
        }
      }
    }
    if (this._value === value) {
      return false
    }
    this._value = value
    return true
  }

  reset() {
    const { done } = this
    this.done = false
    if (is.num(this._value)) {
      this.elapsedTime = 0
      this.durationProgress = 0
      this.lastPosition = this._value
      if (done) this.lastVelocity = null
      this.v0 = null
    }
  }
}