-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (47 loc) · 1.66 KB
/
index.js
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
import { tweened } from 'svelte/motion'
import { get } from 'svelte/store'
export function animation(initial, options={}) {
const speed = options.speed || 1
const duration = options.duration || 300
const adjustedOptions = {...options, duration: duration / speed, speed}
const store = tweened(initial, adjustedOptions)
const set = store.set
store.lastSet = initial
store.lastSpeed = speed
store.pause = () => {
const value = get(store)
return set(value, {duration: 0})
}
store.reset = () => {
return set(initial, {duration: 0})
}
store.continue = () => {
const value = get(store)
const percentageCompleted = (store.lastSet - initial) == 0 ? 1 : (value - initial) / (store.lastSet - initial)
const remaining = (options.duration - (options.duration * percentageCompleted))
return set(store.lastSet, {duration: remaining / adjustedOptions.speed})
}
store.replay = async () => {
await store.reset()
return set(store.lastSet, adjustedOptions)
}
store.reverse = async () => {
const value = get(store)
const percentageCompleted = (store.lastSet - initial) == 0 ? 1 : (value - initial) / (store.lastSet - initial)
const completed = options.duration * percentageCompleted / store.lastSpeed
set(value, {duration: 0})
return set(initial, {duration: completed})
}
store.set = (newValue, options) => {
store.lastSet = newValue
return set(newValue, adjustedOptions)
}
store.accelerate = (newSpeed) => {
adjustedOptions.speed = newSpeed
adjustedOptions.duration = duration / newSpeed
const promise = store.continue()
store.lastSpeed = newSpeed
return promise
}
return store
}