-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstarryNightModel.js
More file actions
113 lines (90 loc) · 2.63 KB
/
starryNightModel.js
File metadata and controls
113 lines (90 loc) · 2.63 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
function StarryNightModel(config) {
this.waveDots = [];
this.radialDots = [];
this.sparkles = [];
this.meteors = [];
this.getTotalObjects = function () {
return this.waveDots.reduce(function (total, dots) { return total + dots.length; }, 0)
+ this.radialDots.length
+ this.sparkles.length
+ this.meteors.length;
};
this.createRadialDots = function () {
var dots = [];
for (var i = 0; i < config.radial.dotCount; i++) {
dots.push({
a: Math.random(), // angle
d: Math.random(), // distance from center
r: Math.random() * 2 - 1
});
}
this.radialDots = dots;
};
this.createWaveDots = function (idx) {
var waveConfig = config.waves[idx];
var dots = [];
var x = 0;
while (x < waveConfig.length) {
var dot = {
a: Math.random() * 2 - 1, // amplitute
r: Math.random() * 2 - 1 // radius
};
var radius = (config.waveDots.minRadius + (1 + dot.r) * (config.waveDots.maxRadius - config.waveDots.minRadius) / 2) / 100;
dot.p = (x + radius) / waveConfig.length; // phase
dots.push(dot);
x += radius * 2 + Math.random() * waveConfig.spacingJitter * waveConfig.spacingJitter;
}
this.waveDots[idx] = dots;
};
this.createAllWaveDots = function () {
for (var idx = 0; idx < config.waves.length; idx++) {
if (config.waves[idx].enabled) {
this.createWaveDots(idx);
} else {
this.waveDots[idx] = [];
}
}
};
this.createMeteor = function (t) {
this.meteors.push({
p: Math.random(),
t: t
});
};
this.createSparkle = function (t) {
var a = Math.random() * Math.PI * 2;
var d = config.sparkles.minDistance + Math.random() * (config.sparkles.maxDistance - config.sparkles.minDistance);
this.sparkles.push({
x: Math.cos(a) * d,
y: Math.sin(a) * d,
t: t
});
};
this.cullMeteors = function (t) {
this.meteors = this.meteors.filter(function (meteor) {
return t - meteor.t <= config.meteors.age * (1 + config.meteors.length);
});
};
this.cullSparkles = function (t) {
this.sparkles = this.sparkles.filter(function (sparkle) {
return t - sparkle.t <= config.sparkles.age;
});
};
this.deleteRadialDots = function () {
this.radialDots = [];
};
this.deleteWaveDots = function (idx) {
this.waveDots[idx] = [];
};
this.deleteAllWaveDots = function () {
for (var i = 0; i < this.waveDots.length; i++) {
this.deleteWaveDots(i);
}
};
this.deleteSparkles = function () {
this.sparkles = [];
};
this.deleteMeteors = function () {
this.meteors = [];
};
}