1+ import { isEqual } from "lodash-es" ;
2+
13let mainCursor ;
24
3- Math . lerp = ( a , b , n ) => ( 1 - n ) * a + n * b ;
5+ const lerp = ( a , b , n ) => {
6+ if ( Math . round ( a ) === b ) {
7+ return b ;
8+ }
9+ return ( 1 - n ) * a + n * b ;
10+ } ;
411
512const getStyle = ( el , attr ) => {
613 try {
@@ -49,7 +56,6 @@ class Cursor {
4956 document . body . appendChild ( ( this . scr = document . createElement ( "style" ) ) ) ;
5057 this . scr . innerHTML = `* {cursor: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8' width='10px' height='10px'><circle cx='4' cy='4' r='4' fill='white' /></svg>") 4 4, auto !important}` ;
5158 }
52-
5359 refresh ( ) {
5460 this . scr . remove ( ) ;
5561 this . cursor . classList . remove ( "active" ) ;
@@ -72,6 +78,7 @@ class Cursor {
7278 y : e . clientY - 8 ,
7379 } ;
7480 this . cursor . classList . remove ( "hidden" ) ;
81+ this . render ( ) ;
7582 } ;
7683 document . onmouseenter = ( ) => this . cursor . classList . remove ( "hidden" ) ;
7784 document . onmouseleave = ( ) => this . cursor . classList . add ( "hidden" ) ;
@@ -81,13 +88,15 @@ class Cursor {
8188
8289 render ( ) {
8390 if ( this . pos . prev ) {
84- this . pos . prev . x = Math . lerp ( this . pos . prev . x , this . pos . curr . x , 0.35 ) ;
85- this . pos . prev . y = Math . lerp ( this . pos . prev . y , this . pos . curr . y , 0.35 ) ;
91+ this . pos . prev . x = lerp ( this . pos . prev . x , this . pos . curr . x , 0.35 ) ;
92+ this . pos . prev . y = lerp ( this . pos . prev . y , this . pos . curr . y , 0.35 ) ;
8693 this . move ( this . pos . prev . x , this . pos . prev . y ) ;
8794 } else {
8895 this . pos . prev = this . pos . curr ;
8996 }
90- requestAnimationFrame ( ( ) => this . render ( ) ) ;
97+ if ( ! isEqual ( this . pos . curr , this . pos . prev ) ) {
98+ requestAnimationFrame ( ( ) => this . render ( ) ) ;
99+ }
91100 }
92101}
93102
0 commit comments