-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
198 lines (189 loc) · 5.73 KB
/
index.mjs
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
import obtainTweens from "./src/obtainTweens.mjs";
import applyCSSChanges from "./src/utils/CSSChanges.mjs";
import { easeLinear } from "./src/utils/EasingFunctions.mjs";
import { handleFunctionAsAnimationParameter } from "./src/utils/functionalAnimations.mjs";
import { random } from "./src/utils/utils.mjs";
function getNewChangesInString(j, playItems, target) {
try {
let number = []
for (let i = 0; i < j.value.numbers.length; i++) {
if (
(target.animations[j.animation].numbers[i] || 0) === j.value.numbers[i]
) {
number[i] = j.value.numbers[i]
} else
number[i] = CallToAnimationFunction(
playItems,
j.timeExpired * 0.001,
target.InitialState[j.animation].numbers[i] || 0,
j.value.numbers[i] -
(target.InitialState[j.animation].numbers[i] || 0),
target.durationInSeconds,
)
}
return { numbers: number, strings: j.value.strings }
} catch (e) {
console.error(e)
}
}
function findObjOfAnimation(j, InitialState) {
var value = j.value
j.value =
typeof j.valuePassed === 'function'
? handleFunctionAsAnimationParameter(j.valuePassed)
: InitialState[j.animation]
InitialState[j.animation] = value
return { j, InitialState }
}
function CallToAnimationFunction(playItems, t, b, c, d,j) {
return playItems.animationFunction()(t, b, c, d)
}
function getChangedColor(i, j) {
let changedColor = {}
for (var color of ['red', 'green', 'blue']) {
changedColor[color] = Math.ceil(
easeLinear(
j.timeExpired * 0.001,
i.InitialState.backgroundColor[color],
j.value[color] - i.InitialState.backgroundColor[color],
i.durationInSeconds,
),
)
}
return changedColor
}
function getIfToDelay(timeDelayExpired, delay) {
return timeDelayExpired < delay ? 'delay' : 'normal'
}
function getIfToEndDelay(timeExpired, endDelay, duration) {
if (timeExpired > duration + endDelay) {
return 'endPlay'
} else if (timeExpired > duration && timeExpired < endDelay + duration) {
return 'endDelay'
} else {
return 'play'
}
}
function calculateNewTimePassed(timeNow, previousTime) {
return timeNow - previousTime
}
function removePlay(j, i, play) {
let k = j.iteration === true ? true : --j.iteration
if (k === true || k > 0) {
if (i.direction === 'alternate') {
let obj = findObjOfAnimation(j, i.InitialState, i.finalState)
i.InitialState = obj.InitialState
j = obj.j
} else {
j.value = typeof j.valuePassed === 'function' ? handleFunctionAsAnimationParameter(j.valuePassed) : j.value
}
j.timeExpired = 0
} else {
if (j.returnToInitial) {
i.animations[j.animation] = i.InitialState[j.animation]
applyCSSChanges(i, j)
}
play = false
}
return { j, i, play }
}
function Animation(i, j, playItems, t) {
try {
let play = typeof i.play === 'function' ? i.play(t - j.startTime) : i.play
let playRef = play
if (play) {
j.isPlaying = true
if (!(j.delayTimeExpired || j.timeExpired)) {
j.timeExpired = j.delayTimeExpired = 0
}
if (!j.previousTime) {
j.previousTime = t
}
let newFrameTime = calculateNewTimePassed(t, j.previousTime)
let whatToDo = getIfToDelay(j.delayTimeExpired, i.delay)
let ifEndDelay = ''
switch (whatToDo) {
case 'delay':
j.delayTimeExpired += newFrameTime
break
default:
j.timeExpired += newFrameTime
ifEndDelay = getIfToEndDelay(j.timeExpired, i.endDelay, i.duration)
}
if (ifEndDelay === 'endPlay') {
let obj = removePlay(j, i, play)
j = obj.j
i = obj.i
play = obj.play
} else if (ifEndDelay === 'play') {
let change
if (j.animation === 'backgroundColor') {
let changedColor = getChangedColor(i, j)
change = changedColor
} else if (typeof i.animations[j.animation] === 'object') {
change = getNewChangesInString(j, playItems, i)
} else {
change = typeof j.value !== 'function'?
CallToAnimationFunction(
playItems,
j.timeExpired * 0.001,
i.InitialState[j.animation],
(typeof j.value === 'function' ? j.value(j.timeExpired) : j.value) -
i.InitialState[j.animation],
i.durationInSeconds,
j
)
: j.value(j.timeExpired)
}
i.animations[j.animation] = change
applyCSSChanges(i, j)
}
if (play) {
window.requestAnimationFrame((t) => Animation(i, j, playItems, t))
}
j.previousTime = t
}
if (!playRef) {
window.requestAnimationFrame((t) => Animation(i, j, playItems, t))
j.previousTime = t
}
} catch (e) {
console.error(e)
}
}
function createInstectionObserver(i, j, playItems) {
let options = { threshold: playItems.threshold }
let observer = new IntersectionObserver((entry) => {
j.isIntersecting = entry[0].isIntersecting
if (entry[0].isIntersecting && !j.isPlaying) {
window.requestAnimationFrame((t) => {
j.startTime = t
Animation(i, j, playItems, t)
})
}
}, options)
observer.observe(i.target)
}
function playNow(playItems) {
for (let i of playItems.targets) {
for (let j of i.animationInstances) {
i.whenVisible
? createInstectionObserver(i, j, playItems)
: window.requestAnimationFrame((t) => {
j.isIntersecting = true
j.startTime = t
Animation(i, j, playItems, t)
})
}
}
}
export function play(params) {
try {
var playItems = obtainTweens(params)
playNow(playItems)
} catch (e) {
console.error(e)
}
}
play.random = random
play.version = '2.0.0'