-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpler-ui-animate.js
More file actions
177 lines (156 loc) · 4.24 KB
/
Copy pathsimpler-ui-animate.js
File metadata and controls
177 lines (156 loc) · 4.24 KB
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
function fnMod (fn, mod) {
return function(t) {
return mod(t, fn)
}
}
function quad (x) {
return x * x
}
function cubic (x) {
return x * x * x
}
function modOut (t, fn) {
return 1 - fn(1 - t)
}
function modTwice (t, fn) {
return fn(t * 2) * 0.5
}
function modInOut (t, fn) {
return (t < 0.5 ?
modTwice(t, fn) :
modOut(t, fnMod(fn, modTwice))
)
}
function clamp01(value) {
if (!Number.isFinite(value)) return 0
return Math.min(1, Math.max(0, value))
}
function easeInOutQuadPulse(progress) {
const t = clamp01(progress)
return modInOut(t, cubic)
// if (t < 0.5) {
// return 2 * t * t
// }
// return 1 - Math.pow(-2 * t + 2, 2) / 2
}
function resolveWaveAmplitude(phaseMs, cycleMs) {
const ascentCycleMs = cycleMs * 0.5
const descentCycleMs = cycleMs * 0.5
if (phaseMs < ascentCycleMs) {
return easeInOutQuadPulse(phaseMs / ascentCycleMs)
}
return 1 - easeInOutQuadPulse((phaseMs - ascentCycleMs) / descentCycleMs)
}
function getPositiveNumber(value, fallback) {
return Number.isFinite(value) && value > 0 ? value : fallback
}
function createIdleWaveAnimator(options) {
const opts = options || {}
const app = opts.app
const getActiveMode = typeof opts.getActiveMode === 'function' ? opts.getActiveMode : () => 'idle'
const totalSize = getPositiveNumber(opts.totalSize, 1)
const waveBarCount = Math.floor(getPositiveNumber(opts.waveBarCount, 10))
const waveCycleMs = getPositiveNumber(opts.waveCycleMs, 3000)
const waveOffsetMs = getPositiveNumber(opts.waveOffsetMs, 300)
const waveMinSegmentMs = getPositiveNumber(opts.waveMinSegmentMs, 200)
const waveMaxSegmentMs = getPositiveNumber(opts.waveMaxSegmentMs, 600)
let rafId = 0
let started = false
let startMs = 0
let pausedElapsedMs = 0
let paused = false
function buildWaveSeries(elapsedMs) {
const series = [[0, 0]]
const bytesPerBar = totalSize / waveBarCount
let transferredBytes = 0
for (let i = 0; i < waveBarCount; i += 1) {
// Positive offset advances bars to the right, so peaks travel right -> left.
const phaseMs = ((elapsedMs + i * waveOffsetMs) % waveCycleMs + waveCycleMs) % waveCycleMs
const pulse = resolveWaveAmplitude(phaseMs, waveCycleMs)
const segmentDurationMs = waveMaxSegmentMs - (waveMaxSegmentMs - waveMinSegmentMs) * pulse
transferredBytes += bytesPerBar
if (i === waveBarCount - 1) {
transferredBytes = totalSize
}
series.push([series[series.length - 1][0] + segmentDurationMs, transferredBytes])
}
return series
}
function stop() {
if (rafId) {
cancelAnimationFrame(rafId)
rafId = 0
}
}
function renderFrame() {
if (getActiveMode() !== 'idle') {
stop()
return
}
if (paused) return
if (!started) {
started = true
startMs = Date.now()
app.startTransfer({
totalSize,
nowMs: startMs,
})
}
const nowMs = Date.now()
const elapsedMs = Math.max(0, nowMs - startMs)
app.replaceRenderedSeries({
series: buildWaveSeries(elapsedMs),
totalSize,
elapsedMs,
finished: false,
nowMs,
})
rafId = requestAnimationFrame(renderFrame)
}
function sync() {
if (getActiveMode() !== 'idle') {
stop()
return
}
if (!rafId && !paused) {
renderFrame()
}
}
function pause(nowMs) {
if (paused || getActiveMode() !== 'idle') return false
pausedElapsedMs = Math.max(0, (Number.isFinite(nowMs) ? nowMs : Date.now()) - startMs)
paused = true
stop()
app.pause(nowMs)
return true
}
function resume(nowMs) {
if (!paused || getActiveMode() !== 'idle') return false
const resumeAt = Number.isFinite(nowMs) ? nowMs : Date.now()
startMs = resumeAt - pausedElapsedMs
paused = false
app.resume(resumeAt)
sync()
return true
}
function reset() {
stop()
started = false
startMs = 0
pausedElapsedMs = 0
paused = false
}
return {
sync,
stop,
pause,
resume,
reset,
isPaused: function () {
return paused
},
}
}
export {
createIdleWaveAnimator,
}