-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
268 lines (256 loc) · 7.43 KB
/
Copy pathscript.js
File metadata and controls
268 lines (256 loc) · 7.43 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
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
FRAME_TIME = 0;
/**
* Main draw function
*/
function draw() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Get and set sizes
const w = window.innerWidth;
const h = window.innerHeight;
const size = Math.min(w, h) * 0.8;
canvas.style.width = '100%';
canvas.style.height = `${window.innerHeight}px`;
// Rescale canvas
const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
ctx.scale(dpr, dpr);
// Background
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, w, h);
// Get parameters
const ratio = +document.getElementById("hexSizeRatioInput").value;
const pBranch = +document.getElementById("pBranchInput").value;
FRAME_TIME = +document.getElementById("speedInput").value;
const snowflakeRadius = size / 2;
const hexRadius = snowflakeRadius * ratio;
// Snowflake
ctx.fillStyle = 'white';
ctx.strokeStyle = 'white';
if (hexRadius > 0.00001) {
if (FRAME_TIME > 0) {
drawSnowflakeAnimated(ctx, w / 2, h / 2, snowflakeRadius, hexRadius, pBranch);
} else {
drawSnowflake(ctx, w / 2, h / 2, snowflakeRadius, hexRadius, pBranch);
}
}
}
function toggleParams() {
const elements = document.getElementsByClassName('paramInput');
for (let el of elements) {
if (el.style.display === 'inline') {
el.style.display = 'none';
} else {
el.style.display = 'inline';
}
}
}
function hideInfo() {
const info = document.getElementById('info');
info.remove();
}
/**
* Draws a hexagon
* @param {CanvasRenderingContext2D} ctx
* @param {*} cx
* @param {*} cy
* @param {*} radius
*/
function drawHexagon(ctx, cx, cy, radius) {
ctx.beginPath();
for (let i = 0; i < 6; i++) {
// Start at 30° so snowflake can be drawn to the right
const angle = (60 * i + 30) / 180 * Math.PI;
const x = cx + Math.cos(angle) * radius;
const y = cy + Math.sin(angle) * radius;
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.closePath();
}
/**
* For a point px, py at angle 0, returns all other points at rotated positions.
*
* (px, py)
* |
* (cx, xy) -----------------------
*
* @param {*} px
* @param {*} py
* @param {*} cx
* @param {*} cy
* @param {*} radius
*/
function getRotatedPositions(px, py, cx, cy) {
const points = [];
// Start angle between hypotenuse and horizontal
let startAngle = 0;
if (py !== cy) {
const dx = px - cx;
const dy = py - cy;
startAngle = Math.atan(dy / dx);
}
// Radius
const r = Math.hypot((px - cx), (py - cy));
// Get positions
for (let i = 0; i < 6; i++) {
const angle = (60 * i) / 180 * Math.PI + startAngle;
const x = cx + Math.cos(angle) * r;
const y = cy + Math.sin(angle) * r;
points.push({ x, y });
// Mirror if startangle !== 0
if (startAngle !== 0) {
const y2 = cy - Math.sin(angle) * r;
points.push({ x, y: y2 });
}
}
return points;
}
/**
* Draws a hexagon with all 6 symmetrical positions
*/
function drawWithSymmetry(ctx, px, py, cx, cy, hexRadius) {
const positions = getRotatedPositions(px, py, cx, cy);
for (let { x, y } of positions) {
drawHexagon(ctx, x, y, hexRadius);
ctx.stroke();
ctx.fill();
}
}
/**
* Draws a snowflake
*/
function drawSnowflake(ctx, cx, cy, radius, hexRadius, pBranch) {
const hexWidth = 2 * Math.cos(30 / 180 * Math.PI) * hexRadius;
// Center
if (Math.random() < 0.5) {
drawHexagon(ctx, cx, cy, hexRadius);
ctx.fill();
}
// Draw only one arm and use symmetry
const armLength = radius / hexWidth;
for (let i = 1; i < armLength; i++) {
const px = cx + hexWidth * i;
const py = cy;
drawWithSymmetry(ctx, px, py, cx, cy, hexRadius);
// Draw a branch from this position with some probability
if (Math.random() < pBranch) {
branch(ctx, px, py, cx, cy, hexRadius, hexWidth);
}
}
}
/**
* Draws a snowflake
*/
function drawSnowflakeAnimated(ctx, cx, cy, radius, hexRadius, pBranch) {
const hexWidth = 2 * Math.cos(30 / 180 * Math.PI) * hexRadius;
// Center
if (Math.random() < 0.5) {
drawHexagon(ctx, cx, cy, hexRadius);
ctx.fill();
}
// Draw only one arm and use symmetry
const armLength = radius / hexWidth;
let i = 1;
const fn = () => {
const px = cx + hexWidth * i;
const py = cy;
drawWithSymmetry(ctx, px, py, cx, cy, hexRadius);
// Draw a branch from this position with some probability
if (Math.random() < pBranch) {
branchAnimated(ctx, px, py, cx, cy, hexRadius, hexWidth);
}
if (i < armLength) {
i++;
setTimeout(fn, FRAME_TIME);
}
};
setTimeout(fn, FRAME_TIME);
}
/**
* Draws a branch
*/
function branch(ctx, px, py, cx, cy, hexRadius, hexWidth) {
let nextX = px;
let nextY = py;
let nextY2 = py;
while (Math.random() < 0.8) {
nextX += hexWidth / 2;
const yOffset = Math.sin(60 / 180 * Math.PI) * hexWidth;
nextY += yOffset;
drawWithSymmetry(ctx, nextX, nextY, cx, cy, hexRadius);
nextY2 -= yOffset;
// Branch branch
if (Math.random() < 0.25) {
branchBranch(ctx, nextX, nextY, cx, cy, hexRadius, hexWidth);
}
}
}
/**
* Draws a branch
*/
function branchAnimated(ctx, px, py, cx, cy, hexRadius, hexWidth) {
let nextX = px;
let nextY = py;
let nextY2 = py;
const fn = () => {
nextX += hexWidth / 2;
const yOffset = Math.sin(60 / 180 * Math.PI) * hexWidth;
nextY += yOffset;
drawWithSymmetry(ctx, nextX, nextY, cx, cy, hexRadius);
nextY2 -= yOffset;
// Branch branch
if (Math.random() < 0.25) {
branchBranchAnimated(ctx, nextX, nextY, cx, cy, hexRadius, hexWidth);
}
if (Math.random() < 0.8) {
setTimeout(fn, FRAME_TIME);
}
};
if (Math.random() < 0.8) {
setTimeout(fn, FRAME_TIME);
}
}
/**
* Draws a branch on a branch
*/
function branchBranch(ctx, px, py, cx, cy, hexRadius, hexWidth) {
let nextX = px;
let nextX2 = px;
let nextY = py;
while (Math.random() < 0.6) {
nextX -= hexWidth / 2;
const yOffset = Math.sin(60 / 180 * Math.PI) * hexWidth;
nextY += yOffset;
drawWithSymmetry(ctx, nextX, nextY, cx, cy, hexRadius);
nextX2 += hexWidth;
drawWithSymmetry(ctx, nextX2, py, cx, cy, hexRadius);
}
}
/**
* Draws a branch on a branch
*/
function branchBranchAnimated(ctx, px, py, cx, cy, hexRadius, hexWidth) {
let nextX = px;
let nextX2 = px;
let nextY = py;
const fn = () => {
nextX -= hexWidth / 2;
const yOffset = Math.sin(60 / 180 * Math.PI) * hexWidth;
nextY += yOffset;
drawWithSymmetry(ctx, nextX, nextY, cx, cy, hexRadius);
nextX2 += hexWidth;
drawWithSymmetry(ctx, nextX2, py, cx, cy, hexRadius);
if (Math.random() < 0.6) {
setTimeout(fn, FRAME_TIME);
}
};
if (Math.random() < 0.6) {
setTimeout(fn, FRAME_TIME);
}
}