Skip to content

Commit 4f2a169

Browse files
committed
refactor: simplify logo interactions
1 parent 32b10bf commit 4f2a169

1 file changed

Lines changed: 25 additions & 52 deletions

File tree

website/src/components/HdiLogo.astro

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,33 @@
9696
});
9797
}
9898

99-
// Maps a mouse event to a key by dividing the pre into 19 character columns.
99+
// Maps a clientX coordinate to a key by dividing the pre into 19 character columns.
100100
// h: cols 0–5, d: cols 6–11, i: cols 12–17, col 18 is the trailing '|' (ignored).
101-
function keyFromMouse(e: MouseEvent): string | null {
101+
function keyFromPoint(clientX: number): string | null {
102102
const rect = logoPre.getBoundingClientRect();
103-
const col = Math.floor((e.clientX - rect.left) / (rect.width / 19));
103+
const col = Math.floor((clientX - rect.left) / (rect.width / 19));
104104
return col < 6 ? "h" : col < 12 ? "d" : col < 18 ? "i" : null;
105105
}
106106

107-
function keyFromTouch(touch: Touch): string | null {
108-
const rect = logoPre.getBoundingClientRect();
109-
const col = Math.floor((touch.clientX - rect.left) / (rect.width / 19));
110-
return col < 6 ? "h" : col < 12 ? "d" : col < 18 ? "i" : null;
107+
function releaseAll() {
108+
if (pressedKeys.size === 0) return;
109+
pressedKeys.clear();
110+
render();
111+
}
112+
113+
function pressTouches(e: TouchEvent) {
114+
e.preventDefault();
115+
pressedKeys.clear();
116+
for (const touch of e.touches) {
117+
const key = keyFromPoint(touch.clientX);
118+
if (key) pressedKeys.add(key);
119+
}
120+
render();
111121
}
112122

113123
// Mouse
114124
logoPre.addEventListener("mousemove", (e) => {
115-
const key = keyFromMouse(e);
125+
const key = keyFromPoint(e.clientX);
116126
if (key !== hoveredKey) {
117127
hoveredKey = key;
118128
applyClasses();
@@ -129,54 +139,19 @@
129139
applyClasses();
130140
});
131141
logoPre.addEventListener("mousedown", (e) => {
132-
const key = keyFromMouse(e);
142+
const key = keyFromPoint(e.clientX);
133143
if (key) {
134144
pressedKeys.add(key);
135145
render();
136146
}
137147
});
138-
document.addEventListener("mouseup", () => {
139-
if (pressedKeys.size === 0) return;
140-
pressedKeys.clear();
141-
render();
142-
});
148+
document.addEventListener("mouseup", releaseAll);
143149

144150
// Touch
145-
logoPre.addEventListener(
146-
"touchstart",
147-
(e) => {
148-
e.preventDefault();
149-
pressedKeys.clear();
150-
for (const touch of e.touches) {
151-
const key = keyFromTouch(touch);
152-
if (key) pressedKeys.add(key);
153-
}
154-
render();
155-
},
156-
{ passive: false },
157-
);
158-
logoPre.addEventListener(
159-
"touchmove",
160-
(e) => {
161-
e.preventDefault();
162-
pressedKeys.clear();
163-
for (const touch of e.touches) {
164-
const key = keyFromTouch(touch);
165-
if (key) pressedKeys.add(key);
166-
}
167-
render();
168-
},
169-
{ passive: false },
170-
);
171-
document.addEventListener("touchend", () => {
172-
if (pressedKeys.size === 0) return;
173-
pressedKeys.clear();
174-
render();
175-
});
176-
document.addEventListener("touchcancel", () => {
177-
pressedKeys.clear();
178-
render();
179-
});
151+
logoPre.addEventListener("touchstart", pressTouches, { passive: false });
152+
logoPre.addEventListener("touchmove", pressTouches, { passive: false });
153+
document.addEventListener("touchend", releaseAll);
154+
document.addEventListener("touchcancel", releaseAll);
180155

181156
// Keyboard — skip when focus is in a text input to avoid interfering with typing
182157
const KEYS = new Set(["h", "d", "i"]);
@@ -211,9 +186,7 @@
211186
.hero-logo-letter {
212187
color: var(--green);
213188
transition: all 150ms linear;
214-
&.hovered {
215-
color: var(--mauve);
216-
}
189+
&.hovered,
217190
&.pressed {
218191
color: var(--mauve);
219192
}

0 commit comments

Comments
 (0)