-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
235 lines (201 loc) · 5.3 KB
/
script.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
const canvas = document.getElementById("canvas");
const c = canvas.getContext("2d");
// TODO
// update color settings without full color reset
// update line number without full reset
// add color support
// basic performance and cleanliness improvements
var last = performance.now() / 1000;
var fpsThreshold = 0;
var colorSettings = {
h: 160,
s: 65,
l: 50,
hVar: 120,
sVar: 70,
lVar: 50
};
var wallpaperSettings = {
numLines: 10,
newLineProb: 1 / 50,
lineSpeed: 1,
direction: "rtl",
fps: 0
};
var lines = [];
function mod(n1, n2) {
return (n1 % n2 + n2) % n2;
}
function clamp(v, l, h) {
return (v < l) ? l : (v > h) ? h : v;
}
function getColor() {
let sBase = colorSettings.s;
let lBase = colorSettings.l;
let sVar = colorSettings.sVar;
let lVar = colorSettings.lVar;
if (sVar / 2 > sBase) {
sBase = (sBase + sVar / 2) / 2;
sVar = colorSettings.s + sVar / 2;
} else if (sBase + sVar / 2 > 100) {
sBase = 100 - ((100 - sBase) + sVar / 2) / 2;
sVar = (100 - colorSettings.s) + sVar / 2;
}
if (lVar / 2 > lBase) {
lBase = (lBase + lVar / 2) / 2;
lVar = colorSettings.l + lVar / 2;
} else if (lBase + lVar / 2 > 100) {
lBase = 100 - ((100 - lBase) + lVar / 2) / 2;
lVar = (100 - colorSettings.l) + lVar / 2;
}
let h = mod(colorSettings.h + Math.random() * colorSettings.hVar - colorSettings.hVar / 2, 360);
let s = clamp(sBase + Math.random() * sVar - sVar / 2, 0, 100);
let l = clamp(lBase + Math.random() * lVar - lVar / 2, 0, 100);
return "hsl(" + h + "," + s + "%," + l + "%)";
}
function updateColors() {
for(let line of lines) {
line.c1 = getColor();
line.c2 = getColor();
}
}
function updateSpeed(pSpeed, speed) {
let m = speed / pSpeed;
for(let line of lines) {
line.v *= m;
}
}
function constructLines() {
lines = [];
let lineSize = canvas.height / wallpaperSettings.numLines;
let numLines = wallpaperSettings.numLines;
for (let i = 0; i < numLines; i += 1) {
lines.push({
c1: getColor(),
c2: getColor(),
y: i * lineSize,
h: lineSize,
p: Math.round(Math.random() * canvas.width),
v: wallpaperSettings.lineSpeed / 2 + Math.random() * wallpaperSettings.lineSpeed / 2
});
}
}
function updateLinePos() {
if (wallpaperSettings.direction == "rtl") {
for(let line of lines) {
if (line.p > 0) {
line.p -= line.v;
} else if (Math.random() <= wallpaperSettings.newLineProb) {
line.p = canvas.width;
line.c1 = line.c2;
line.c2 = getColor();
}
}
} else {
for(let line of lines) {
if (line.p < canvas.width) {
line.p += line.v;
} else if (Math.random() <= wallpaperSettings.newLineProb) {
line.p = 0;
line.c2 = line.c1;
line.c1 = getColor();
}
}
}
}
function drawLines() {
for(let line of lines) {
c.fillStyle = line.c1;
c.fillRect(0, line.y, line.p, line.h);
c.fillStyle = line.c2;
c.fillRect(line.p, line.y, canvas.width - line.p, line.h);
}
}
function step() {
window.requestAnimationFrame(step);
let now = performance.now() / 1000;
let dt = Math.min(now - last, 1);
last = now;
if (wallpaperSettings.fps > 0) {
fpsThreshold += dt;
if (fpsThreshold < 1.0 / wallpaperSettings.fps) {
return;
}
fpsThreshold -= 1.0 / wallpaperSettings.fps;
}
updateLinePos();
drawLines();
}
function rgbToHsl(r, g, b) {
let cmin = Math.min(r, g, b),
cmax = Math.max(r, g, b),
delta = cmax - cmin,
h = 0,
s = 0,
l = 0;
if (delta == 0)
h = 0;
else if (cmax == r)
h = ((g - b) / delta) % 6;
else if (cmax == g)
h = (b - r) / delta + 2;
else
h = (r - g) / delta + 4;
h = Math.round(h * 60);
h = mod(h, 360);
l = (cmax + cmin) / 2;
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
s = Math.round(s * 100);
l = Math.round(l * 100);
return [h, s, l];
}
window.wallpaperPropertyListener = {
applyGeneralProperties: function(properties) {
if (properties.fps) {
wallpaperSettings.fps = properties.fps;
}
},
applyUserProperties: function(properties) {
if (properties.color) {
let p = properties.color.value.split(" ");
let v = rgbToHsl(p[0], p[1], p[2]);
colorSettings.h = v[0];
colorSettings.s = v[1];
colorSettings.l = v[2];
updateColors();
}
if (properties.numlines) {
wallpaperSettings.numLines = properties.numlines.value;
constructLines();
}
if (properties.speed) {
let pSpeed = wallpaperSettings.lineSpeed;
wallpaperSettings.lineSpeed = properties.speed.value;
updateSpeed(pSpeed, wallpaperSettings.lineSpeed);
}
if (properties.direction) {
wallpaperSettings.direction = properties.direction.value;
}
if (properties.huerange) {
colorSettings.hVar = properties.huerange.value;
updateColors();
}
if (properties.saturationrange) {
colorSettings.sVar = properties.saturationrange.value;
updateColors();
}
if (properties.lightnessrange) {
colorSettings.lVar = properties.lightnessrange.value;
updateColors();
}
},
}
window.onResize = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.onload = function() {
window.onResize();
constructLines();
window.requestAnimationFrame(step);
}