@@ -2,6 +2,10 @@ import * as ThemeColors from "./theme-colors";
22import * as SlowTimer from "../states/slow-timer" ;
33import Config from "../config" ;
44import { isSafeNumber } from "@monkeytype/util/numbers" ;
5+ import { requestDebouncedAnimationFrame } from "../utils/debounced-animation-frame" ;
6+
7+ const html = document . querySelector ( "html" ) as HTMLElement ;
8+ const body = document . body ;
59
610type Particle = {
711 x : number ;
@@ -14,7 +18,7 @@ type Particle = {
1418
1519type CTX = {
1620 particles : Particle [ ] ;
17- caret ?: JQuery ;
21+ caret ?: HTMLElement ;
1822 canvas ?: HTMLCanvasElement ;
1923 context2d ?: CanvasRenderingContext2D ;
2024 rendering : boolean ;
@@ -118,7 +122,7 @@ function updateParticle(particle: Particle): void {
118122}
119123
120124export function init ( ) : void {
121- ctx . caret = $ ( "#caret" ) ;
125+ ctx . caret = document . querySelector ( "#caret" ) as HTMLElement ;
122126 ctx . canvas = createCanvas ( ) ;
123127 ctx . context2d = ctx . canvas . getContext ( "2d" ) as CanvasRenderingContext2D ;
124128}
@@ -155,7 +159,7 @@ function render(): void {
155159 }
156160 ctx . particles = keep ;
157161
158- if ( ctx . particles . length && ! SlowTimer . get ( ) ) {
162+ if ( ctx . particles . length ) {
159163 requestAnimationFrame ( render ) ;
160164 } else {
161165 ctx . context2d . clearRect ( 0 , 0 , ctx . canvas . width , ctx . canvas . height ) ;
@@ -168,14 +172,13 @@ export function reset(immediate = false): void {
168172 delete ctx . resetTimeOut ;
169173
170174 clearTimeout ( ctx . resetTimeOut ) ;
171- const body = $ ( document . body ) ;
172- body . css ( "transition" , "all .25s, transform 0.8s" ) ;
173- body . css ( "transform" , `translate(0,0)` ) ;
175+ body . style . transition = "all .25s, transform 0.8s" ;
176+ body . style . transform = `translate(0,0)` ;
174177 setTimeout (
175178 ( ) => {
176- body . css ( " transition" , "all .25s, transform .05s" ) ;
177- $ ( " html" ) . css ( " overflow" , "inherit" ) ;
178- $ ( " html" ) . css ( "overflow-y" , "scroll" ) ;
179+ body . style . transition = "all .25s, transform .05s" ;
180+ html . style . overflow = "inherit" ;
181+ html . style . overflowY = "scroll" ;
179182 } ,
180183 immediate ? 0 : 1000 ,
181184 ) ;
@@ -201,47 +204,46 @@ function randomColor(): string {
201204export async function addPower ( good = true , extra = false ) : Promise < void > {
202205 if ( Config . monkeyPowerLevel === "off" || SlowTimer . get ( ) ) return ;
203206
204- if ( Config . blindMode ) good = true ;
205-
206- // Shake
207- if ( [ "3" , "4" ] . includes ( Config . monkeyPowerLevel ) ) {
208- $ ( "html" ) . css ( "overflow" , "hidden" ) ;
209- const shake = [
210- Math . round ( shakeAmount - Math . random ( ) * shakeAmount ) ,
211- Math . round ( shakeAmount - Math . random ( ) * shakeAmount ) ,
207+ requestDebouncedAnimationFrame ( "monkey-power.addPower" , async ( ) => {
208+ if ( Config . blindMode ) good = true ;
209+
210+ // Shake
211+ if ( [ "3" , "4" ] . includes ( Config . monkeyPowerLevel ) ) {
212+ html . style . overflow = "hidden" ;
213+ const shake = [
214+ Math . round ( shakeAmount - Math . random ( ) * shakeAmount ) ,
215+ Math . round ( shakeAmount - Math . random ( ) * shakeAmount ) ,
216+ ] ;
217+ body . style . transform = `translate(${ shake [ 0 ] } px, ${ shake [ 1 ] } px)` ;
218+ if ( isSafeNumber ( ctx . resetTimeOut ) ) clearTimeout ( ctx . resetTimeOut ) ;
219+ ctx . resetTimeOut = setTimeout ( reset , 2000 ) as unknown as number ;
220+ }
221+
222+ // Sparks
223+ const offset = ctx . caret ?. getBoundingClientRect ( ) ;
224+ const coords = [
225+ offset ?. left ?? 0 ,
226+ ( offset ?. top ?? 0 ) + ( ctx . caret ?. offsetHeight ?? 0 ) / 2 ,
212227 ] ;
213- $ ( document . body ) . css (
214- "transform" ,
215- `translate(${ shake [ 0 ] } px, ${ shake [ 1 ] } px)` ,
216- ) ;
217- if ( isSafeNumber ( ctx . resetTimeOut ) ) clearTimeout ( ctx . resetTimeOut ) ;
218- ctx . resetTimeOut = setTimeout ( reset , 2000 ) as unknown as number ;
219- }
220228
221- // Sparks
222- const offset = ctx . caret ?. offset ( ) ;
223- const coords = [
224- offset ?. left ?? 0 ,
225- ( offset ?. top ?? 0 ) + ( ctx . caret ?. height ( ) ?? 0 ) ,
226- ] ;
227-
228- for (
229- let i = Math . round (
230- ( particleCreateCount [ 0 ] + Math . random ( ) * particleCreateCount [ 1 ] ) *
231- ( extra ? 2 : 1 ) ,
232- ) ;
233- i > 0 ;
234- i --
235- ) {
236- const color = [ "2" , "4" ] . includes ( Config . monkeyPowerLevel )
237- ? randomColor ( )
238- : good
239- ? await ThemeColors . get ( "caret" )
240- : await ThemeColors . get ( "error" ) ;
241- ctx . particles . push (
242- createParticle ( ...( coords as [ x : number , y : number ] ) , color ) ,
243- ) ;
244- }
245-
246- startRender ( ) ;
229+ for (
230+ let i = Math . round (
231+ ( particleCreateCount [ 0 ] + Math . random ( ) * particleCreateCount [ 1 ] ) *
232+ ( extra ? 2 : 1 ) ,
233+ ) ;
234+ i > 0 ;
235+ i --
236+ ) {
237+ const color = [ "2" , "4" ] . includes ( Config . monkeyPowerLevel )
238+ ? randomColor ( )
239+ : good
240+ ? await ThemeColors . get ( "caret" )
241+ : await ThemeColors . get ( "error" ) ;
242+ ctx . particles . push (
243+ createParticle ( ...( coords as [ x : number , y : number ] ) , color ) ,
244+ ) ;
245+ }
246+
247+ startRender ( ) ;
248+ } ) ;
247249}
0 commit comments