1
+ import { isEqual } from "lodash-es" ;
2
+
1
3
let mainCursor ;
2
4
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
+ } ;
4
11
5
12
const getStyle = ( el , attr ) => {
6
13
try {
@@ -49,7 +56,6 @@ class Cursor {
49
56
document . body . appendChild ( ( this . scr = document . createElement ( "style" ) ) ) ;
50
57
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}` ;
51
58
}
52
-
53
59
refresh ( ) {
54
60
this . scr . remove ( ) ;
55
61
this . cursor . classList . remove ( "active" ) ;
@@ -72,6 +78,7 @@ class Cursor {
72
78
y : e . clientY - 8 ,
73
79
} ;
74
80
this . cursor . classList . remove ( "hidden" ) ;
81
+ this . render ( ) ;
75
82
} ;
76
83
document . onmouseenter = ( ) => this . cursor . classList . remove ( "hidden" ) ;
77
84
document . onmouseleave = ( ) => this . cursor . classList . add ( "hidden" ) ;
@@ -81,13 +88,15 @@ class Cursor {
81
88
82
89
render ( ) {
83
90
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 ) ;
86
93
this . move ( this . pos . prev . x , this . pos . prev . y ) ;
87
94
} else {
88
95
this . pos . prev = this . pos . curr ;
89
96
}
90
- requestAnimationFrame ( ( ) => this . render ( ) ) ;
97
+ if ( ! isEqual ( this . pos . curr , this . pos . prev ) ) {
98
+ requestAnimationFrame ( ( ) => this . render ( ) ) ;
99
+ }
91
100
}
92
101
}
93
102
0 commit comments