-
-
Notifications
You must be signed in to change notification settings - Fork 798
/
Utils.ts
90 lines (72 loc) · 1.93 KB
/
Utils.ts
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
import {
type IDelta,
type IDimension,
getRangeValue,
millisecondsToSeconds,
randomInRange,
setRangeValue,
} from "@tsparticles/engine";
import type { LifeParticle } from "./Types.js";
const noTime = 0,
infiniteValue = -1,
noLife = 0,
minCanvasSize = 0;
/**
*
* @param particle
* @param delta
* @param canvasSize
*/
export function updateLife(particle: LifeParticle, delta: IDelta, canvasSize: IDimension): void {
if (!particle.life) {
return;
}
const life = particle.life;
let justSpawned = false;
if (particle.spawning) {
life.delayTime += delta.value;
if (life.delayTime >= particle.life.delay) {
justSpawned = true;
particle.spawning = false;
life.delayTime = noTime;
life.time = noTime;
} else {
return;
}
}
if (life.duration === infiniteValue) {
return;
}
if (particle.spawning) {
return;
}
if (justSpawned) {
life.time = noTime;
} else {
life.time += delta.value;
}
if (life.time < life.duration) {
return;
}
life.time = noTime;
if (particle.life.count > noLife) {
particle.life.count--;
}
if (particle.life.count === noLife) {
particle.destroy();
return;
}
const widthRange = setRangeValue(minCanvasSize, canvasSize.width),
heightRange = setRangeValue(minCanvasSize, canvasSize.width);
particle.position.x = randomInRange(widthRange);
particle.position.y = randomInRange(heightRange);
particle.spawning = true;
life.delayTime = noTime;
life.time = noTime;
particle.reset();
const lifeOptions = particle.options.life;
if (lifeOptions) {
life.delay = getRangeValue(lifeOptions.delay.value) * millisecondsToSeconds;
life.duration = getRangeValue(lifeOptions.duration.value) * millisecondsToSeconds;
}
}