-
Notifications
You must be signed in to change notification settings - Fork 1
/
fx.js
448 lines (430 loc) · 12.1 KB
/
fx.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* fx.js
* @description A library for providing simple animations in ZeppOS. 一个用于在ZeppOS中提供简单动画的库
* @version 2.0.0
* @date 2023/03/15
* @author XiaomaiTX
* @license MIT
* https://github.com/XiaomaiTX/zeppos-fx
*
* */
function normalGetTime() {
switch (zosAPILevel) {
case "2.0":
return new Time().getTime();
case "1.0":
return new Date().getTime();
}
return;
}
export class Fx {
constructor(animProfile, animObj) {
this.objects = animObj;
this.status = "stop";
this.tracks = [];
for (let i = 0; i < animProfile.length; i++) {
let track = animProfile[i];
this.tracks.push(track);
}
this.progress = [];
for (let i = 0; i < this.tracks.length; i++) {
this.progress.push(0);
}
this.timers = [];
}
start() {
if (this.status !== "stop" && this.status !== "pause") return;
this.status = "start";
console.log("start fx");
for (let i = 0; i < this.tracks.length; i++) {
this.scheduleTrack(i);
}
}
scheduleTrack(trackIndex) {
// 清理之前的定时器
if (this.timers[trackIndex]) {
clearTimeout(this.timers[trackIndex]);
}
let track = this.tracks[trackIndex];
let progress = this.progress[trackIndex];
const executeEffect = () => {
const currentEffect = track[progress];
FxInside.fx(
currentEffect.props.fps,
currentEffect.props.duration,
currentEffect.props.fx_style,
currentEffect.func,
currentEffect.onStop,
"static delay"
);
if (
currentEffect.props.repeat === true &&
progress === track.length - 1
) {
progress = 0;
} else {
progress++;
}
this.progress[trackIndex] = progress;
// 如果已经到达最后一个效果并且不需要重复,则停止调度
if (progress < track.length) {
this.timers[trackIndex] = setTimeout(
executeEffect,
track[progress].props.startTime * 1000
);
}
};
this.timers[trackIndex] = setTimeout(
executeEffect,
track[progress].props.startTime * 1000
);
}
pause() {
if (this.status !== "start") return;
this.status = "pause";
this.timers.forEach((timer) => clearTimeout(timer));
}
stop() {
this.status = "stop";
this.timers.forEach((timer) => clearTimeout(timer));
this.progress = this.tracks.map(() => 0);
}
restart() {
if (this.status != "stop" && this.status != "pause") return;
this.status = "start";
this.progress = 0;
this.start();
}
status() {
return this.status;
}
static Styles = {
LINEAR: function (now_x, begin, end, max_x) {
function math_func(x) {
return x;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_SINE: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 - cos((x * Math.PI) / 2);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_SINE: function (now_x, begin, end, max_x) {
function math_func(x) {
return sin((x * Math.PI) / 2);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_SINE: function (now_x, begin, end, max_x) {
function math_func(x) {
return -(cos(Math.PI * x) - 1) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_QUAD: function (now_x, begin, end, max_x) {
function math_func(x) {
return x * x;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_QUAD: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 - (1 - x) * (1 - x);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_QUAD: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_CUBIC: function (now_x, begin, end, max_x) {
function math_func(x) {
return x * x * x;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_CUBIC: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 - Math.pow(1 - x, 3);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_CUBIC: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5
? 4 * x * x * x
: 1 - Math.pow(-2 * x + 2, 3) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_QUART: function (now_x, begin, end, max_x) {
function math_func(x) {
return x * x * x * x;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_QUART: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 - Math.pow(1 - x, 4);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_QUART: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5
? 8 * x * x * x * x
: 1 - Math.pow(-2 * x + 2, 4) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_QUINT: function (now_x, begin, end, max_x) {
function math_func(x) {
return x * x * x * x * x;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_QUINT: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 - Math.pow(1 - x, 4);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_QUINT: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5
? 16 * x * x * x * x * x
: 1 - Math.pow(-2 * x + 2, 5) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_EXPO: function (now_x, begin, end, max_x) {
function math_func(x) {
return x === 0 ? 0 : Math.pow(2, 10 * x - 10);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_EXPO: function (now_x, begin, end, max_x) {
function math_func(x) {
return x === 1 ? 1 : 1 - Math.pow(2, -10 * x);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_EXPO: function (now_x, begin, end, max_x) {
function math_func(x) {
return x === 0
? 0
: x === 1
? 1
: x < 0.5
? Math.pow(2, 20 * x - 10) / 2
: (2 - Math.pow(2, -20 * x + 10)) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_CIRC: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 - Math.sqrt(1 - Math.pow(x, 2));
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_CIRC: function (now_x, begin, end, max_x) {
function math_func(x) {
return Math.sqrt(1 - Math.pow(x - 1, 2));
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_CIRC: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5
? (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2
: (Math.sqrt(1 - Math.pow(-2 * x + 2, 2)) + 1) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_BACK: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1.70158 + 1 * x * x * x - 1.70158 * x * x;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_BACK: function (now_x, begin, end, max_x) {
function math_func(x) {
return (
1 +
1.70158 +
1 * Math.pow(x - 1, 3) +
1.70158 * Math.pow(x - 1, 2)
);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_BACK: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5
? (Math.pow(2 * x, 2) *
((1.70158 * 1.525 + 1) * 2 * x - 1.70158 * 1.525)) /
2
: (Math.pow(2 * x - 2, 2) *
((1.70158 * 1.525 + 1) * (x * 2 - 2) +
1.70158 * 1.525) +
2) /
2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_ELASTIC: function (now_x, begin, end, max_x) {
function math_func(x) {
return x === 0
? 0
: x === 1
? 1
: -Math.pow(2, 10 * x - 10) *
sin(((x * 10 - 10.75) * (2 * Math.PI)) / 3);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_ELASTIC: function (now_x, begin, end, max_x) {
function math_func(x) {
return x === 0
? 0
: x === 1
? 1
: Math.pow(2, -10 * x) *
sin(((x * 10 - 0.75) * (2 * Math.PI)) / 3) +
1;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_ELASTIC: function (now_x, begin, end, max_x) {
function math_func(x) {
return x === 0
? 0
: x === 1
? 1
: x < 0.5
? -(
Math.pow(2, 20 * x - 10) *
sin(((20 * x - 11.125) * (2 * Math.PI)) / 4.5)
) / 2
: (Math.pow(2, -20 * x + 10) *
sin(((20 * x - 11.125) * (2 * Math.PI)) / 4.5)) /
2 +
1;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_BOUNCE: function (now_x, begin, end, max_x) {
function math_func(x) {
return 1 - bounceOut(1 - x);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_OUT_BOUNCE: function (now_x, begin, end, max_x) {
function math_func(x) {
return bounceOut(x);
}
return begin + (end - begin) * math_func(now_x / max_x);
},
EASE_IN_OUT_BOUNCE: function (now_x, begin, end, max_x) {
function math_func(x) {
return x < 0.5
? (1 - bounceOut(1 - 2 * x)) / 2
: (1 + bounceOut(2 * x - 1)) / 2;
}
return begin + (end - begin) * math_func(now_x / max_x);
},
};
}
class FxInside {
constructor() {}
static fx(fps, duration, fx_style, callback, onStop, mode) {
this.x_now = 0;
this.per_clock = 1000 / fps;
console.log("per_clock", this.per_clock);
console.log("mode", mode);
switch (mode) {
case "static delay":
let timer = setInterval(() => {
callback(fx_style((this.x_now += 1), 0, 1, fps * duration));
if (this.x_now >= fps * duration) {
clearInterval(timer);
timer = null;
if (onStop != undefined) {
onStop(
fx_style(fps * duration, 0, 1, fps * duration)
);
}
}
}, per_clock);
break;
case "dynamic delay":
(function loop() {
timer = setTimeout(() => {
callback(
fx_style((this.x_now += 1), 0, 1, fps * duration)
);
if (this.x_now > fps * duration) {
if (onStop != undefined) {
onStop();
clearTimeout(timer);
timer = null;
}
}
loop();
}, per_clock);
})();
break;
}
}
}
// class Timer {
// constructor(callback, interval, errorAdjustmentInterval = 1000) {
// this.callback = callback; // 要执行的回调函数
// this.interval = interval; // 计时器的间隔时间,以毫秒为单位
// this.timerId = null; // 保存计时器的 ID,用于停止计时器
// this.startTime = null; // 计时器的开始时间
// this.nextTick = null; // 下一次 tick(执行回调)的时间点
// this.errorAdjustmentInterval = errorAdjustmentInterval; // 误差调整间隔
// this.lastAdjustmentTime = null; // 上一次误差调整的时间点
// this.drift = 0; // 累积的误差
// }
// start(delay = 0) {
// this.startTime = normalGetTime() + delay; // 设置开始时间,加上可选的延迟
// this.nextTick = this.startTime + this.interval; // 计算第一次 tick 的时间点
// this.lastAdjustmentTime = this.startTime; // 初始化上一次误差调整的时间点
// this.timerId = setInterval(() => {
// this.tick();
// }, this.interval);
// }
// stop() {
// clearInterval(this.timerId);
// this.timerId = null;
// this.timer = null;
// }
// tick() {
// const currentTime = normalGetTime();
// // 执行回调
// this.callback();
// // 更新下一次 tick 的时间点
// this.nextTick += this.interval;
// // 检查是否需要调整误差
// if (
// currentTime - this.lastAdjustmentTime >=
// this.errorAdjustmentInterval
// ) {
// // 计算误差
// const drift = currentTime - this.nextTick + this.interval;
// this.nextTick += drift; // 调整下一次 tick 的时间点
// this.lastAdjustmentTime = currentTime; // 更新上一次误差调整的时间点
// // 清除并重新启动计时器,以适应误差调整后的间隔
// clearInterval(this.timerId);
// this.timerId = setInterval(() => {
// this.tick();
// }, this.interval);
// }
// }
// }