Skip to content

Commit 49b4c65

Browse files
committed
update for dynamic radius size
1 parent 268cd2e commit 49b4c65

2 files changed

Lines changed: 67 additions & 13 deletions

File tree

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { WordGenError } from "../../utils/word-gen-error";
2828
import { FunboxName, KeymapLayout, Layout } from "@monkeytype/schemas/configs";
2929
import { Language, LanguageObject } from "@monkeytype/schemas/languages";
3030
import { qs } from "../../utils/dom";
31+
import { convertRemToPixels } from "../../utils/numbers";
3132

3233
export type FunboxFunctions = {
3334
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
@@ -168,6 +169,12 @@ export class PolyglotWordset extends Wordset {
168169

169170
let tunnelVisionAnimationFrame: number | null = null;
170171

172+
function getTunnelVisionRadiusPx(): number {
173+
const fontSizePx = convertRemToPixels(Config.fontSize);
174+
175+
return Math.max(48, Math.min(220, fontSizePx * 4.5));
176+
}
177+
171178
const list: Partial<Record<FunboxName, FunboxFunctions>> = {
172179
"58008": {
173180
getWord(): string {
@@ -684,25 +691,37 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
684691
tunnel_vision: {
685692
applyGlobalCSS(): void {
686693
const words = qs("#words");
687-
if (!words) return;
694+
const wordsWrapper = qs("#wordsWrapper");
695+
if (!words || !wordsWrapper) return;
688696

689697
const updateCaretPos = (): void => {
690698
const caretElem = qs("#caret");
691699
if (caretElem !== null) {
692-
const caretStyle = caretElem.getStyle();
693-
const left = caretStyle.left || "0px";
694-
const top = caretStyle.top || "0px";
695-
const marginLeft = caretStyle.marginLeft || "0px";
696-
const marginTop = caretStyle.marginTop || "0px";
697-
698-
words.native.style.setProperty(
700+
const wordsRect = words.native.getBoundingClientRect();
701+
const wordsWrapperRect = wordsWrapper.native.getBoundingClientRect();
702+
const caretRect = caretElem.native.getBoundingClientRect();
703+
const caretLeft =
704+
caretRect.left - wordsRect.left + caretRect.width / 2;
705+
const caretTop = caretRect.top - wordsRect.top + caretRect.height / 2;
706+
const wrapperCaretLeft =
707+
caretRect.left - wordsWrapperRect.left + caretRect.width / 2;
708+
const wrapperCaretTop =
709+
caretRect.top - wordsWrapperRect.top + caretRect.height / 2;
710+
const radius = `${getTunnelVisionRadiusPx()}px`;
711+
712+
words.native.style.setProperty("--caret-left", `${caretLeft}px`);
713+
words.native.style.setProperty("--caret-top", `${caretTop}px`);
714+
words.native.style.setProperty("--tunnel-radius", radius);
715+
716+
wordsWrapper.native.style.setProperty(
699717
"--caret-left",
700-
`calc(${left} + ${marginLeft})`,
718+
`${wrapperCaretLeft}px`,
701719
);
702-
words.native.style.setProperty(
720+
wordsWrapper.native.style.setProperty(
703721
"--caret-top",
704-
`calc(${top} + ${marginTop})`,
722+
`${wrapperCaretTop}px`,
705723
);
724+
wordsWrapper.native.style.setProperty("--tunnel-radius", radius);
706725
}
707726
tunnelVisionAnimationFrame = requestAnimationFrame(updateCaretPos);
708727
};
@@ -718,9 +737,16 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
718737
tunnelVisionAnimationFrame = null;
719738
}
720739
const words = qs("#words");
740+
const wordsWrapper = qs("#wordsWrapper");
721741
if (words) {
722742
words.native.style.removeProperty("--caret-left");
723743
words.native.style.removeProperty("--caret-top");
744+
words.native.style.removeProperty("--tunnel-radius");
745+
}
746+
if (wordsWrapper) {
747+
wordsWrapper.native.style.removeProperty("--caret-left");
748+
wordsWrapper.native.style.removeProperty("--caret-top");
749+
wordsWrapper.native.style.removeProperty("--tunnel-radius");
724750
}
725751
},
726752
},
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
#words {
22
mask-image: radial-gradient(
3-
circle 150px at var(--caret-left) var(--caret-top),
3+
circle var(--tunnel-radius, 150px) at var(--caret-left) var(--caret-top),
44
black 0%,
55
black 50%,
66
transparent 100%
77
);
88
-webkit-mask-image: radial-gradient(
9-
circle 150px at var(--caret-left) var(--caret-top),
9+
circle var(--tunnel-radius, 150px) at var(--caret-left) var(--caret-top),
1010
black 0%,
1111
black 50%,
1212
transparent 100%
1313
);
1414
mask-repeat: no-repeat;
1515
-webkit-mask-repeat: no-repeat;
1616
}
17+
18+
body.fb-tunnel-vision #wordsWrapper.tape {
19+
isolation: isolate;
20+
}
21+
22+
body.fb-tunnel-vision #wordsWrapper.tape #words {
23+
mask-image: none;
24+
-webkit-mask-image: none;
25+
}
26+
27+
body.fb-tunnel-vision #wordsWrapper.tape::after {
28+
content: "";
29+
position: absolute;
30+
inset: 0;
31+
z-index: 1;
32+
pointer-events: none;
33+
background: radial-gradient(
34+
circle var(--tunnel-radius, 150px) at var(--caret-left) var(--caret-top),
35+
transparent 0%,
36+
transparent 50%,
37+
var(--bg-color) 100%
38+
);
39+
}
40+
41+
body.fb-tunnel-vision #wordsWrapper.tape #caret,
42+
body.fb-tunnel-vision #wordsWrapper.tape #paceCaret {
43+
z-index: 2;
44+
}

0 commit comments

Comments
 (0)