@@ -68,15 +68,28 @@ export function BackgroundAnimation() {
68
68
let currentBlobs = startBlobs
69
69
let targetBlobs : ReturnType < typeof createBlobs > = [ ]
70
70
71
+ function resizeHandler ( ) {
72
+ // Create an offscreen canvas and copy the current content
73
+ const offscreen = document . createElement ( 'canvas' )
74
+ offscreen . width = canvas ! . width
75
+ offscreen . height = canvas ! . height
76
+ offscreen . getContext ( '2d' ) ! . drawImage ( canvas ! , 0 , 0 )
77
+
78
+ // Resize the main canvas
79
+ canvas ! . width = document . documentElement . clientWidth
80
+ canvas ! . height = document . documentElement . clientHeight
81
+
82
+ // Stretch and redraw the saved content to fill the new size
83
+ ctx . drawImage ( offscreen , 0 , 0 , canvas ! . width , canvas ! . height )
84
+ }
85
+
71
86
function start ( ) {
72
87
if ( timeout ) {
73
88
clearTimeout ( timeout )
74
89
}
75
90
if ( rafId ) {
76
91
cancelAnimationFrame ( rafId )
77
92
}
78
- canvas ! . width = document . documentElement . clientWidth
79
- canvas ! . height = document . documentElement . clientHeight
80
93
81
94
startBlobs = JSON . parse ( JSON . stringify ( currentBlobs ) )
82
95
targetBlobs = createBlobs ( )
@@ -162,8 +175,9 @@ export function BackgroundAnimation() {
162
175
}
163
176
}
164
177
178
+ resizeHandler ( )
165
179
start ( )
166
- window . addEventListener ( 'resize' , start )
180
+ window . addEventListener ( 'resize' , resizeHandler )
167
181
168
182
return ( ) => {
169
183
if ( rafId ) {
@@ -172,7 +186,7 @@ export function BackgroundAnimation() {
172
186
if ( timeout ) {
173
187
clearTimeout ( timeout )
174
188
}
175
- window . removeEventListener ( 'resize' , start )
189
+ window . removeEventListener ( 'resize' , resizeHandler )
176
190
}
177
191
}
178
192
} , [ prefersReducedMotion ] )
0 commit comments