@@ -28,6 +28,7 @@ import { WordGenError } from "../../utils/word-gen-error";
2828import { FunboxName , KeymapLayout , Layout } from "@monkeytype/schemas/configs" ;
2929import { Language , LanguageObject } from "@monkeytype/schemas/languages" ;
3030import { qs } from "../../utils/dom" ;
31+ import { convertRemToPixels } from "../../utils/numbers" ;
3132
3233export type FunboxFunctions = {
3334 getWord ?: ( wordset ?: Wordset , wordIndex ?: number ) => string ;
@@ -168,6 +169,12 @@ export class PolyglotWordset extends Wordset {
168169
169170let 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+
171178const 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 } ,
0 commit comments