@@ -42,22 +42,22 @@ class Canvas {
42
42
}
43
43
44
44
lowPerf = false ;
45
+ toggle = false ;
45
46
46
47
loop = ( ) => {
47
48
const now = Date . now ( ) ;
48
- const delta = now - this . lastDelta ;
49
- if ( getScrollPos ( now ) . y > this . stage . height || delta < 33 ) {
49
+ const delta = 33 ;
50
+ const renderDelta = ( now - this . lastDelta ) * 2 ;
51
+ if ( window . scrolling || getScrollPos ( now ) . y > this . stage . height ) {
52
+ this . lastDelta = now ;
50
53
requestAnimationFrame ( this . loop ) ;
51
54
return ;
52
55
}
53
56
54
- if ( delta > 2000 ) {
57
+ if ( renderDelta > 4000 ) {
55
58
this . lowPerf = true ;
56
- this . dots = this . dots . filter ( x => Math . random ( ) > 0.5 ) ;
57
59
}
58
60
59
- this . ctx . clearRect ( 0 , 0 , this . stage . width , this . stage . height ) ;
60
-
61
61
let distX ;
62
62
let distY ;
63
63
let power = 0.2 ;
@@ -67,53 +67,62 @@ class Canvas {
67
67
let middle ;
68
68
let top ;
69
69
70
- if ( this . wave ) {
71
- const radius = this . wave . waveWidth ;
70
+ this . toggle = ! this . toggle ;
71
+ if ( this . toggle ) {
72
+ if ( this . wave ) {
73
+ const radius = this . wave . waveWidth ;
72
74
73
- this . wave . update ( delta ) ;
75
+ this . wave . update ( delta ) ;
74
76
75
- startRadius = this . wave . waveRadius - radius / 2 - radius ;
76
- endRadius = this . wave . waveRadius + radius / 2 - radius ;
77
- middle = startRadius + ( endRadius - startRadius ) / 2 ;
78
- top = - ( middle - startRadius ) * ( middle - endRadius ) ;
77
+ startRadius = this . wave . waveRadius - radius / 2 - radius ;
78
+ endRadius = this . wave . waveRadius + radius / 2 - radius ;
79
+ middle = startRadius + ( endRadius - startRadius ) / 2 ;
80
+ top = - ( middle - startRadius ) * ( middle - endRadius ) ;
79
81
80
- if ( startRadius > this . stage . width + 200 ) {
81
- this . wave = null ;
82
+ if ( startRadius > this . stage . width + 200 ) {
83
+ this . wave = null ;
84
+ }
82
85
}
83
- }
84
-
85
- for ( let i = 0 ; i < this . dots . length ; i ++ ) {
86
- distX = Math . abs ( this . dots [ i ] . x - this . cubeX ) ;
87
- distY = Math . abs ( this . dots [ i ] . y - this . cubeY ) ;
88
- const distance = Math . sqrt ( distX * distX + distY * distY ) ;
89
-
90
- this . dots [ i ] . setAlpha ( Math . max ( 0.2 , ( 1 - distance / 300 ) * 2 ) ) ;
91
- this . dots [ i ] . setSize ( Math . max ( 1.5 , ( 1 - distance / 300 ) * 4 ) ) ;
92
- this . dots [ i ] . update ( delta ) ;
93
86
94
- if ( this . wave ) {
95
- const dirX = this . dots [ i ] . x - this . wave . x ;
96
- const dirY = this . dots [ i ] . y - this . wave . y ;
97
- const waveDistance = Math . sqrt ( dirX * dirX + dirY * dirY ) ;
87
+ for ( let i = 0 ; i < this . dots . length ; i ++ ) {
88
+ distX = Math . abs ( this . dots [ i ] . x - this . cubeX ) ;
89
+ distY = Math . abs ( this . dots [ i ] . y - this . cubeY ) ;
90
+ const distance = Math . sqrt ( distX * distX + distY * distY ) ;
98
91
99
- if ( waveDistance < middle ) {
100
- this . dots [ i ] . setColor ( this . wave . color ) ;
92
+ this . dots [ i ] . setAlpha ( Math . max ( 0.2 , ( 1 - distance / 300 ) * 2 ) ) ;
93
+ this . dots [ i ] . setSize ( Math . max ( 1.5 , ( 1 - distance / 300 ) * 4 ) ) ;
94
+ this . dots [ i ] . update ( delta ) ;
95
+
96
+ if ( this . wave ) {
97
+ const dirX = this . dots [ i ] . x - this . wave . x ;
98
+ const dirY = this . dots [ i ] . y - this . wave . y ;
99
+ const waveDistance = Math . sqrt ( dirX * dirX + dirY * dirY ) ;
100
+
101
+ if ( waveDistance < middle ) {
102
+ this . dots [ i ] . setColor ( this . wave . color ) ;
103
+ }
104
+ if ( waveDistance > startRadius && waveDistance < endRadius ) {
105
+ power = Math . max (
106
+ 0 ,
107
+ - ( ( waveDistance - startRadius ) * ( waveDistance - endRadius ) ) / top
108
+ ) ;
109
+
110
+ this . dots [ i ] . x += ( power - 0.5 ) * ( dirX / waveDistance ) * 5 ;
111
+ this . dots [ i ] . y += ( power - 0.5 ) * ( dirY / waveDistance ) * 5 ;
112
+
113
+ this . dots [ i ] . alpha *= ( power + 1 ) ** 4 ;
114
+ this . dots [ i ] . size *= power * 0.5 + 1 ;
115
+ }
101
116
}
102
- if ( waveDistance > startRadius && waveDistance < endRadius ) {
103
- power = Math . max (
104
- 0 ,
105
- - ( ( waveDistance - startRadius ) * ( waveDistance - endRadius ) ) / top
106
- ) ;
107
-
108
- this . dots [ i ] . x += ( power - 0.5 ) * ( dirX / waveDistance ) * 5 ;
109
- this . dots [ i ] . y += ( power - 0.5 ) * ( dirY / waveDistance ) * 5 ;
117
+ }
118
+ } else {
119
+ if ( ! this . lowPerf ) {
120
+ this . ctx . clearRect ( 0 , 0 , this . stage . width , this . stage . height ) ;
110
121
111
- this . dots [ i ] . alpha *= ( power + 1 ) ** 4 ;
112
- this . dots [ i ] . size *= power * 0.5 + 1 ;
122
+ for ( let i = 0 ; i < this . dots . length ; i ++ ) {
123
+ this . dots [ i ] . draw ( this . ctx ) ;
113
124
}
114
125
}
115
-
116
- this . dots [ i ] . draw ( this . ctx ) ;
117
126
}
118
127
119
128
this . lastDelta = now ;
@@ -145,8 +154,8 @@ class Canvas {
145
154
const distance = Math . sqrt ( distX * distX + distY * distY ) ;
146
155
147
156
this . dots [ i ] . setSpeed (
148
- distX / distance / 40 * ( ( Math . random ( ) + 0.1 ) * 0.2 ) ,
149
- distY / distance / 40 * ( ( Math . random ( ) + 0.1 ) * 0.2 )
157
+ distX / distance / 20 * ( ( Math . random ( ) + 0.1 ) * 0.15 ) ,
158
+ distY / distance / 20 * ( ( Math . random ( ) + 0.1 ) * 0.15 )
150
159
) ;
151
160
}
152
161
this . calibrated = true ;
0 commit comments