-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake.js
185 lines (158 loc) · 4.92 KB
/
snowflake.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
const canvasEl = document.querySelector('#ice-canvas');
const resetBtnEl = document.querySelector('#refresh-btn');
const titleEl = document.querySelector('.title');
const ctx = canvasEl.getContext('2d');
let maxNumberOfLines = 6;
let screen, lineIdx, drawingAllowed, baseAngle, mouseAngle, startPoint, coords, mouseDistance;
let cursorPosition = [0, 0];
updateCanvasSize();
window.onresize = updateCanvasSize;
refresh();
loop();
// ------------------------------------------------------------
// Auto draw mode. Tbh, only for the codepen preview not to be empty :)
let autoDrawInterval = setInterval(autoDraw, 200);
let autoDrawIsActive = true;
resetBtnEl.style.opacity = 0;
function autoDraw() {
if (!drawingAllowed) {
refresh();
}
onMove([Math.random() * window.innerWidth, Math.random() * window.innerHeight]);
onDown();
}
function stopAutoDraw() {
if (autoDrawIsActive) {
clearInterval(autoDrawInterval);
refresh();
updateCanvasSize();
autoDrawIsActive = false;
resetBtnEl.style.opacity = 1;
titleEl.style.opacity = 0;
}
}
// ------------------------------------------------------------
resetBtnEl.addEventListener('click', refresh);
document.addEventListener('mousedown', onDown);
document.addEventListener('mousemove', (e) => {
cursorPosition = [ e.pageX, e.pageY ];
stopAutoDraw();
onMove();
});
window.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
stopDrawing();
}
});
document.addEventListener("touchstart", (e) => {
cursorPosition = [ e.touches[0].pageX, e.touches[0].pageY ];
stopAutoDraw();
onMove();
});
document.addEventListener("touchend", onDown);
document.addEventListener("touchmove", (e) => {
cursorPosition = [ e.touches[0].pageX, e.touches[0].pageY ];
onMove();
});
function updateCanvasSize() {
screen = {
w: window.innerWidth,
h: window.innerHeight,
c: [ .5 * window.innerWidth, .5 * window.innerHeight ],
r: Math.max(100, .3 * Math.min(window.innerWidth, window.innerHeight))
}
canvasEl.setAttribute('width', screen.w);
canvasEl.setAttribute('height', screen.h);
}
function refresh() {
drawingAllowed = true;
lineIdx = 0;
startPoint = [0, 0];
baseAngle = 0;
mouseAngle = 0;
coords = [];
for (let i = 0; i < maxNumberOfLines; i++) {
coords.push([[], [], []]);
}
resetBtnEl.style.visibility = 'hidden';
onMove();
}
function onDown() {
if (drawingAllowed) {
if (lineIdx === 0) {
baseAngle = mouseAngle;
screen.r = mouseDistance;
}
screen.r *= .85;
startPoint = randomPointOnBaseAngle();
lineIdx ++;
if (lineIdx >= maxNumberOfLines) {
lineIdx = 0;
updateCanvasSize();
stopDrawing()
}
}
}
function onMove([x, y] = cursorPosition) {
if (drawingAllowed) {
let mousePoint = [x - screen.c[0], y - screen.c[1]];
mouseDistance = Math.sqrt(Math.pow(mousePoint[0], 2) + Math.pow(mousePoint[1], 2));
mouseAngle = Math.atan2(mousePoint[1], mousePoint[0]);
if (mouseDistance > screen.r) {
mousePoint = [
screen.r * Math.cos(mouseAngle),
screen.r * Math.sin(mouseAngle)
];
mouseDistance = screen.r;
}
const mirroredPoint = [
Math.cos(2 * baseAngle - mouseAngle) * mouseDistance,
Math.sin(2 * baseAngle - mouseAngle) * mouseDistance
];
coords[lineIdx][0] = startPoint;
coords[lineIdx][1] = mousePoint;
coords[lineIdx][2] = mirroredPoint;
}
}
function stopDrawing() {
drawingAllowed = false;
resetBtnEl.style.visibility = 'visible';
}
function randomPointOnBaseAngle() {
const r = Math.random() * screen.r;
return [
r * Math.cos(baseAngle),
r * Math.sin(baseAngle)
]
}
function loop() {
ctx.globalCompositeOperation = 'lighter';
ctx.globalAlpha = .2;
const color = '255, 255, 255';
ctx.strokeStyle = 'rgb(' + color + ')';
ctx.fillStyle = 'rgb(' + color + ')';
ctx.shadowColor = 'rgb(' + color + ')';
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
ctx.lineWidth = 4;
ctx.shadowBlur = 5;
coords.forEach((line, lineIdx) => {
for (let i = 0; i < 6; i++) {
ctx.translate(screen.c[0], screen.c[1]);
ctx.rotate(i * Math.PI / 3);
ctx.beginPath();
ctx.moveTo(line[0][0], line[0][1]);
ctx.lineTo(line[1][0], line[1][1]);
ctx.stroke();
if (lineIdx) {
ctx.beginPath();
ctx.moveTo(line[0][0], line[0][1]);
ctx.lineTo(line[2][0], line[2][1]);
ctx.stroke();
}
ctx.setTransform(1, 0, 0, 1, 0, 0);
}
})
window.requestAnimationFrame(loop);
}