-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
263 lines (246 loc) · 10.5 KB
/
main.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
"use strict";
// do
// next do
// doing
// done
// ===== Define the movemenet of elements =====
function makeArr(startValue, stopValue, cardinality) {
var arr = [];
var step = (stopValue - startValue) / (cardinality - 1);
for (var i = 0; i < cardinality; i++) {
arr.push(startValue + (step * i));
}
return arr;
}
function getScriptDir() {
var root;
var scripts = document.getElementsByTagName("script");
var i = scripts.length;
while (i--) {
var match = scripts[i].src.match(/(^|.*\/)main\.js$/);
if (match) {
root = match[1];
break;
}
}
return root;
}
function getSeasonalInformation() {
// === Winter ===
var winterMovements = {"verticalMovement":{1:[], 2:[]}, "horizontalMovement":{1:[], 2:[]}};
// = vertical =
for (var i=0; i<4; i++) {
winterMovements.verticalMovement[1].push([]);
for (var j=0; j<500; j++) {
winterMovements.verticalMovement[1][i].push(0.7);
}
}
for (var i=0; i<4; i++) {
for (var j=0; j<500; j++) {
winterMovements.verticalMovement[2].push([]);
winterMovements.verticalMovement[2][i].push(1);
}
}
// = horizontal =
for (var i=0; i<4; i++) {
winterMovements.horizontalMovement[1].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
winterMovements.horizontalMovement[1][i].push(Math.sin(linspaceArr[j])*7);
}
}
for (var i=0; i<4; i++) {
winterMovements.horizontalMovement[2].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
winterMovements.horizontalMovement[2][i].push(Math.sin(linspaceArr[j]));
}
}
// = inf =
const winter_inf = {"ratio":0.8, "path":{1:`${fallDir}/pics/winter1.png`, 2:`${fallDir}/pics/winter2.png`}, "size":{1:10, 2:20}, "verticalMovement":winterMovements.verticalMovement, "horizontalMovement":winterMovements.horizontalMovement};
// === Spring ===
var springMovements = {"verticalMovement":{1:[], 2:[]}, "horizontalMovement":{1:[], 2:[]}};
// = vertical =
for (var i=0; i<4; i++) {
springMovements.verticalMovement[1].push([]);
for (var j=0; j<500; j++) {
springMovements.verticalMovement[1][i].push(1);
}
}
for (var i=0; i<4; i++) {
for (var j=0; j<500; j++) {
springMovements.verticalMovement[2].push([]);
springMovements.verticalMovement[2][i].push(1.2);
}
}
// = horizontal =
for (var i=0; i<4; i++) {
springMovements.horizontalMovement[1].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
springMovements.horizontalMovement[1][i].push(Math.sin(linspaceArr[j])*7);
}
}
for (var i=0; i<4; i++) {
springMovements.horizontalMovement[2].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
springMovements.horizontalMovement[2][i].push(Math.sin(linspaceArr[j])*7);
}
}
// = inf =
const spring_inf = {"ratio":0.7, "path":{1:`${fallDir}/pics/spring1.png`, 2:`${fallDir}/pics/spring2.png`}, "size":{1:15, 2:15}, "verticalMovement":springMovements.verticalMovement, "horizontalMovement":springMovements.horizontalMovement};
// === Summer ===
var summerMovements = {"verticalMovement":{1:[], 2:[]}, "horizontalMovement":{1:[], 2:[]}};
// = vertical =
for (var i=0; i<4; i++) {
summerMovements.verticalMovement[1].push([]);
for (var j=0; j<500; j++) {
summerMovements.verticalMovement[1][i].push(1);
}
}
for (var i=0; i<4; i++) {
for (var j=0; j<500; j++) {
summerMovements.verticalMovement[2].push([]);
summerMovements.verticalMovement[2][i].push(1.2);
}
}
// = horizontal =
for (var i=0; i<4; i++) {
summerMovements.horizontalMovement[1].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
summerMovements.horizontalMovement[1][i].push(Math.sin(linspaceArr[j])*2);
}
}
for (var i=0; i<4; i++) {
summerMovements.horizontalMovement[2].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
summerMovements.horizontalMovement[2][i].push(Math.sin(linspaceArr[j])*7);
}
}
// = inf =
const summer_inf = {"ratio":0.7, "path":{1:`${fallDir}/pics/summer1.png`, 2:`${fallDir}/pics/summer2.png`}, "size":{1:20, 2:15}, "verticalMovement":summerMovements.verticalMovement, "horizontalMovement":summerMovements.horizontalMovement};
// === Autumn ===
var autumnMovements = {"verticalMovement":{1:[], 2:[]}, "horizontalMovement":{1:[], 2:[]}};
// = vertical =
for (var i=0; i<4; i++) {
autumnMovements.verticalMovement[1].push([]);
for (var j=0; j<500; j++) {
autumnMovements.verticalMovement[1][i].push(1);
}
}
for (var i=0; i<4; i++) {
for (var j=0; j<500; j++) {
autumnMovements.verticalMovement[2].push([]);
autumnMovements.verticalMovement[2][i].push(1.2);
}
}
// = horizontal =
for (var i=0; i<4; i++) {
autumnMovements.horizontalMovement[1].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
autumnMovements.horizontalMovement[1][i].push(Math.sin(linspaceArr[j])*7);
}
}
for (var i=0; i<4; i++) {
autumnMovements.horizontalMovement[2].push([]);
var linspaceArr = makeArr(Math.PI*(i/2), Math.PI*((i+4)/2), 500);
for (let j=0; j<linspaceArr.length; j++) {
autumnMovements.horizontalMovement[2][i].push(Math.sin(linspaceArr[j])*7);
}
}
// = inf =
const autumn_inf = {"ratio":0.7, "path":{1:`${fallDir}/pics/autumn1.png`, 2:`${fallDir}/pics/autumn2.png`}, "size":{1:20, 2:20}, "verticalMovement":autumnMovements.verticalMovement, "horizontalMovement":autumnMovements.horizontalMovement};
return [winter_inf, spring_inf, summer_inf, autumn_inf];
}
const fallDir = getScriptDir();
const [winter_inf, spring_inf, summer_inf, autumn_inf] = getSeasonalInformation();
var init = true;
var count = 0;
// ===== Main =====
function fallElement(elements, quantities) {
if (elements.length != quantities.length) {
console.log('\u001b[31m' + "Fall_Module_ERR : elements.length does not match quantities.length");
return;
}
for (let i=0; i<elements.length; i++) {
if (quantities[i] === "low") {
var element_quantity = 10;
} else if (quantities[i] === "medium") {
var element_quantity = 50;
} else if (quantities[i] === "high") {
var element_quantity = 100;
} else {
return;
}
const targetElement = $(elements[i]);
const width = targetElement.width();
const height = targetElement.height();
if (targetElement.css("z-index") === "auto") {
var zIndex = 999;
} else {
var zIndex = Number(targetElement.css("z-index"))+1;
}
const fallLayer = $("<div>", {class:"fall-layer"}).css("width", `${targetElement.width()}`).css("height", `${targetElement.height()}`).css("z-index", `${zIndex}`).css("overflow", "hidden").css("background-color", "transparent").css("position", "relative").css("top", `${-targetElement.height()}px`).css("margin-bottom", `${-targetElement.height()}px`).css("pointer-events", "none").css("user-select", "none").insertAfter(targetElement);
const month = new Date().getMonth()+1;
if (month == 12 || (month < 3)) {
var inf = winter_inf;
} else if (month >= 4 && month < 7) {
var inf = spring_inf;
} else if (month >= 7 && month < 10) {
var inf = summer_inf;
} else if (month >= 10 && month < 12) {
var inf = autumn_inf;
}
// for debug
// inf = autumn_inf;
for (let j=0; j<element_quantity; j++) {
if (Math.random() >= (1-inf.ratio)) {
var type = 1;
} else {
var type = 2;
}
var positionX = Math.random()*width;
var positionY = Math.random()*height - height - inf.size[type];
$("<img>", {src:inf.path[type], class:"ele", originalX:`${positionX}`, picType:`${type}`, moveRoute:`${Math.floor(Math.random()*(inf.verticalMovement[1].length/2))}`}).css("width", `${inf.size[type]}px`).css("position","ralative").css("position", "absolute").css("top",`${positionY}px`).css("left",`${positionX}px`).css("user-select", "none").appendTo(fallLayer);
}
precipitateElement(inf, fallLayer);
updateFallLayerSize(fallLayer);
}
};
function precipitateElement(inf, fallLayer) {
var startTime = performance.now();
count += 1;
const elements = fallLayer.find(".ele");
elements.each(function(i, ele){
var ele = $(ele);
if (Number(ele.css("top").replace("px", "")) < fallLayer.height()) {
var picType = Number(ele.attr("picType"));
var moveRoute = Number(ele.attr("moveRoute"));
ele.css("top", `${Number(ele.css("top").replace("px", "")) + inf.verticalMovement[picType][moveRoute][count%(inf.verticalMovement[picType][moveRoute].length)]}px`);
ele.css("left", `${Number(ele.attr("originalX")) + (inf.horizontalMovement[picType][moveRoute][count%inf.horizontalMovement[picType][moveRoute].length])}px`);
} else {
ele.css("top", `${ele.height()*-1}px`);
ele.css("left", `${Math.random()*Number(fallLayer.css("width").replace("px", ""))}px`);
}
});
var endTime = performance.now();
if ((endTime - startTime) < 15) {
var intervalTime = 14 - (endTime - startTime);
} else {
var intervalTime = 0;
}
setTimeout(function(){
precipitateElement(inf, fallLayer);
}, intervalTime);
}
function updateFallLayerSize(fallLayer) {
fallLayer.css("width", `${fallLayer.prev().css("width")}`);
fallLayer.css("height", `${fallLayer.prev().css("height")}`);
setTimeout(function(){
updateFallLayerSize(fallLayer);
}, 1000);
}