-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpixel-canvas.js
261 lines (210 loc) · 6.12 KB
/
pixel-canvas.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
class Pixel {
constructor(canvas, context, x, y, color, speed, delay) {
this.width = canvas.width;
this.height = canvas.height;
this.ctx = context;
this.x = x;
this.y = y;
this.color = color;
this.speed = this.getRandomValue(0.1, 0.9) * speed;
this.size = 0;
this.sizeStep = Math.random() * 0.4;
this.minSize = 0.5;
this.maxSizeInteger = 2;
this.maxSize = this.getRandomValue(this.minSize, this.maxSizeInteger);
this.delay = delay;
this.counter = 0;
this.counterStep = Math.random() * 4 + (this.width + this.height) * 0.01;
this.isIdle = false;
this.isReverse = false;
this.isShimmer = false;
}
getRandomValue(min, max) {
return Math.random() * (max - min) + min;
}
draw() {
const centerOffset = this.maxSizeInteger * 0.5 - this.size * 0.5;
this.ctx.fillStyle = this.color;
this.ctx.fillRect(this.x + centerOffset, this.y + centerOffset, this.size, this.size);
}
appear() {
this.isIdle = false;
if (this.counter <= this.delay) {
this.counter += this.counterStep;
return;
}
if (this.size >= this.maxSize) {
this.isShimmer = true;
}
if (this.isShimmer) {
this.shimmer();
} else {
this.size += this.sizeStep;
}
this.draw();
}
disappear() {
this.isShimmer = false;
this.counter = 0;
if (this.size <= 0) {
this.isIdle = true;
return;
} else {
this.size -= 0.1;
}
this.draw();
}
shimmer() {
if (this.size >= this.maxSize) {
this.isReverse = true;
} else if (this.size <= this.minSize) {
this.isReverse = false;
}
if (this.isReverse) {
this.size -= this.speed;
} else {
this.size += this.speed;
}
}
}
class PixelCanvas extends HTMLElement {
static register(tag = "pixel-canvas") {
if ("customElements" in window) {
customElements.define(tag, this);
}
}
static css = `
:host {
display: grid;
inline-size: 100%;
block-size: 100%;
overflow: hidden;
}
`;
get colors() {
return this.dataset.colors?.split(",") || ["#f8fafc", "#f1f5f9", "#cbd5e1"];
}
get gap() {
const value = this.dataset.gap || 5;
const min = 4;
const max = 50;
if (value <= min) {
return min;
} else if (value >= max) {
return max;
} else {
return parseInt(value);
}
}
get speed() {
const value = this.dataset.speed || 35;
const min = 0;
const max = 100;
const throttle = 0.001;
if (value <= min || this.reducedMotion) {
return min;
} else if (value >= max) {
return max * throttle;
} else {
return parseInt(value) * throttle;
}
}
get noFocus() {
return this.hasAttribute("data-no-focus");
}
connectedCallback() {
const canvas = document.createElement("canvas");
const sheet = new CSSStyleSheet();
this._parent = this.parentNode;
this.shadowroot = this.attachShadow({ mode: "open" });
sheet.replaceSync(PixelCanvas.css);
this.shadowroot.adoptedStyleSheets = [sheet];
this.shadowroot.append(canvas);
this.canvas = this.shadowroot.querySelector("canvas");
this.ctx = this.canvas.getContext("2d");
this.timeInterval = 1000 / 60;
this.timePrevious = performance.now();
this.reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
this.init();
this.resizeObserver = new ResizeObserver(() => this.init());
this.resizeObserver.observe(this);
this._parent.addEventListener("mouseenter", this);
this._parent.addEventListener("mouseleave", this);
if (!this.noFocus) {
this._parent.addEventListener("focusin", this);
this._parent.addEventListener("focusout", this);
}
}
disconnectedCallback() {
this.resizeObserver.disconnect();
this._parent.removeEventListener("mouseenter", this);
this._parent.removeEventListener("mouseleave", this);
if (!this.noFocus) {
this._parent.removeEventListener("focusin", this);
this._parent.removeEventListener("focusout", this);
}
delete this._parent;
}
handleEvent(event) {
this[`on${event.type}`](event);
}
onmouseenter() {
this.handleAnimation("appear");
}
onmouseleave() {
this.handleAnimation("disappear");
}
onfocusin(e) {
if (e.currentTarget.contains(e.relatedTarget)) return;
this.handleAnimation("appear");
}
onfocusout(e) {
if (e.currentTarget.contains(e.relatedTarget)) return;
this.handleAnimation("disappear");
}
handleAnimation(name) {
cancelAnimationFrame(this.animation);
this.animation = this.animate(name);
}
init() {
const rect = this.getBoundingClientRect();
const width = Math.floor(rect.width);
const height = Math.floor(rect.height);
this.pixels = [];
this.canvas.width = width;
this.canvas.height = height;
this.canvas.style.width = `${width}px`;
this.canvas.style.height = `${height}px`;
this.createPixels();
}
getDistanceToCanvasCenter(x, y) {
const dx = x - this.canvas.width / 2;
const dy = y - this.canvas.height / 2;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance;
}
createPixels() {
for (let x = 0; x < this.canvas.width; x += this.gap) {
for (let y = 0; y < this.canvas.height; y += this.gap) {
const color = this.colors[Math.floor(Math.random() * this.colors.length)];
const delay = this.reducedMotion ? 0 : this.getDistanceToCanvasCenter(x, y);
this.pixels.push(new Pixel(this.canvas, this.ctx, x, y, color, this.speed, delay));
}
}
}
animate(fnName) {
this.animation = requestAnimationFrame(() => this.animate(fnName));
const timeNow = performance.now();
const timePassed = timeNow - this.timePrevious;
if (timePassed < this.timeInterval) return;
this.timePrevious = timeNow - (timePassed % this.timeInterval);
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
for (let i = 0; i < this.pixels.length; i++) {
this.pixels[i][fnName]();
}
if (this.pixels.every((pixel) => pixel.isIdle)) {
cancelAnimationFrame(this.animation);
}
}
}
PixelCanvas.register();