-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
274 lines (213 loc) · 7.22 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
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
// * SVG namespace
const _SVG_NS = "http://www.w3.org/2000/svg";
const DIV = document.getElementById("svg-experiment");
const SVG = document.createElementNS(_SVG_NS, "svg");
const DEFS = document.createElementNS(_SVG_NS, "defs");
DIV.appendChild(SVG);
const filter_style = document.head.querySelector("style#filter-style");
// ! Global variables
let WIDTH = window.innerWidth;
let HEIGHT = window.innerHeight;
let CENTER = { x: WIDTH / 2, y: HEIGHT / 2 };
let minimumRadius = Math.min(WIDTH, HEIGHT) / 100;
// ! Parameters (from index page)
const theta_factor = document.querySelector("input#theta_factor");
let THETA_VAR = parseFloat(theta_factor.value);
const drop_shadow = document.querySelector("input#drop_shadow");
let SHADOW_VAR = drop_shadow.checked;
// ! Drop Shadow
if (window.mobileAndTabletCheck()) {
// * If it is mobile device, remove drop shadow toggler
SHADOW_VAR = false;
drop_shadow.closest("tr").remove();
filter_style.innerHTML = "";
}
// ! Utils
function rotate(p, cx, cy, theta) {
// * Rotate polygon -> object
rx = (p[0] - cx) * Math.cos(theta) - (p[1] - cy) * Math.sin(theta) + cx;
ry = (p[0] - cx) * Math.sin(theta) + (p[1] - cy) * Math.cos(theta) + cy;
// * Output
return { x: rx, y: ry };
}
function getPoints(cx, cy, r, theta) {
// * Get points -> string
let pa = rotate([cx + r, cy], cx, cy, theta);
let pb = rotate([cx, cy + r], cx, cy, theta);
let pc = rotate([cx - r, cy], cx, cy, theta);
let pd = rotate([cx, cy - r], cx, cy, theta);
// * Output
return `${pa.x}, ${pa.y} ${pb.x}, ${pb.y} ${pc.x}, ${pc.y} ${pd.x}, ${pd.y}`;
}
function randomColor() {
// * Get random hsl channels -> object
const h = parseInt(Math.random() * 360);
const s = parseInt(30 + Math.random() * 40);
const l = parseInt(50 + Math.random() * 20);
// * Output
return `hsl(${h}, ${s}%, ${l}%)`;
}
// ! Event functions
// * Stop propagation when click on input
function stop_propagation() {
event.stopPropagation();
}
// * Theta factor parameter events
function set_theta_factor() {
THETA_VAR = parseFloat(theta_factor.value);
main();
}
// * Drop shadow parameter events
function set_drop_shadow() {
SHADOW_VAR = drop_shadow.checked;
filter_style.innerHTML = !SHADOW_VAR
? ""
: `
svg polygon {
filter: url(#drop-shadow);
}
`;
main();
}
// * Change center/axis
window.addEventListener("click", (event) => {
CENTER.x = event.clientX;
CENTER.y = event.clientY;
main();
});
// * Resize window
window.addEventListener("resize", () => {
WIDTH = window.innerWidth;
HEIGHT = window.innerHeight;
CENTER.x = WIDTH / 2;
CENTER.y = HEIGHT / 2;
minimumRadius = Math.min(WIDTH, HEIGHT) / 100;
main();
});
// ! Build functions
function setSVG() {
SVG.setAttribute("version", "1.1");
SVG.setAttribute("xmlns", _SVG_NS);
SVG.setAttribute("width", WIDTH);
SVG.setAttribute("height", HEIGHT);
}
function dropShadow() {
// ! Build drop shadow filter
let filter, blur, offset, flood, composite;
// * Init filter
filter = document.createElementNS(_SVG_NS, "filter");
filter.id = "drop-shadow";
filter.setAttribute("x", `-50%`);
filter.setAttribute("y", `-50%`);
filter.setAttribute("width", `200%`);
filter.setAttribute("height", `200%`);
// ? Drop Shadow A
// * Blur 1
blur = document.createElementNS(_SVG_NS, "feGaussianBlur");
blur.setAttribute("in", "SourceAlpha");
blur.setAttribute("stdDeviation", "7");
blur.setAttribute("result", "blur1");
filter.appendChild(blur);
// * Composite 1
composite = document.createElementNS(_SVG_NS, "feComposite");
composite.setAttribute("in2", "SourceAlpha");
composite.setAttribute("operator", "arithmetic");
composite.setAttribute("k2", -1);
composite.setAttribute("k3", 1);
composite.setAttribute("result", "composite1");
filter.appendChild(composite);
// * Flood 1
flood = document.createElementNS(_SVG_NS, "feFlood");
flood.setAttribute("flood-color", "black");
flood.setAttribute("flood-opacity", 0.75);
filter.appendChild(flood);
// * Composite 2
composite = document.createElementNS(_SVG_NS, "feComposite");
composite.setAttribute("in2", "composite1");
composite.setAttribute("operator", "in");
composite.setAttribute("result", "composite2");
filter.appendChild(composite);
// * Composite 3
composite = document.createElementNS(_SVG_NS, "feComposite");
composite.setAttribute("in2", "SourceGraphic");
composite.setAttribute("operator", "over");
composite.setAttribute("result", "composite3");
filter.appendChild(composite);
// ? Drop Shadow B
// * Blur 2
blur = document.createElementNS(_SVG_NS, "feGaussianBlur");
blur.setAttribute("in", "SourceAlpha");
blur.setAttribute("stdDeviation", "5");
blur.setAttribute("result", "blur2");
filter.appendChild(blur);
// * Offset 2
offset = document.createElementNS(_SVG_NS, "feOffset");
offset.setAttribute("dx", 5);
offset.setAttribute("dy", 7);
filter.appendChild(offset);
// * Composite 4
composite = document.createElementNS(_SVG_NS, "feComposite");
composite.setAttribute("in2", "SourceAlpha");
composite.setAttribute("operator", "arithmetic");
composite.setAttribute("k2", -1);
composite.setAttribute("k3", 1);
composite.setAttribute("result", "composite4");
filter.appendChild(composite);
// * Flood 2
flood = document.createElementNS(_SVG_NS, "feFlood");
flood.setAttribute("flood-color", "black");
flood.setAttribute("flood-opacity", 0.75);
filter.appendChild(flood);
// * Composite 5
composite = document.createElementNS(_SVG_NS, "feComposite");
composite.setAttribute("in2", "composite4");
composite.setAttribute("operator", "in");
composite.setAttribute("result", "composite5");
filter.appendChild(composite);
// * Composite 6
composite = document.createElementNS(_SVG_NS, "feComposite");
composite.setAttribute("in2", "SourceGraphic");
composite.setAttribute("operator", "over");
composite.setAttribute("result", "composite6");
filter.appendChild(composite);
DEFS.appendChild(filter);
}
function draw() {
// ! Draw new squares based on the idea of
// ! squares inscribed in another square
// * Incremental variables
let phi = 0; // Current angle
let phi_a = 0; // Current angle (absolute value)
let theta = 0; // Incremental angle
// * Define initial radius
let radius = 2 * Math.max(WIDTH, HEIGHT);
while (radius > minimumRadius) {
// * Create new square
let rect = document.createElementNS(_SVG_NS, "polygon");
// * Draw square and fill
rect.setAttribute("points", getPoints(CENTER.x, CENTER.y, radius, theta));
rect.setAttribute("fill", randomColor());
// * Increase theta and define phi randomly
theta += phi = Math.PI * (Math.random() * 2 - 1) * THETA_VAR;
phi_a = Math.abs(phi);
// * Find the opposite of phi based on the inscribed square
let x = (2 * radius * Math.tan(phi_a)) / (Math.tan(phi_a) + 1);
// * Define the new radius based on the sine function definition
// * The new radius is in the hypotenuse
radius = x / (2 * Math.sin(phi_a));
SVG.appendChild(rect);
}
}
function main() {
// ! Main function
// * Clear SVG
SVG.innerHTML = "";
DEFS.innerHTML = "";
SVG.appendChild(DEFS);
setSVG();
if (!window.mobileAndTabletCheck() && SHADOW_VAR) {
dropShadow();
}
draw();
}
main();