Skip to content

Commit 24af200

Browse files
committed
fix: background image
1 parent 8e97d94 commit 24af200

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

app/components/BackgroundAnimation.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,28 @@ export function BackgroundAnimation() {
6868
let currentBlobs = startBlobs
6969
let targetBlobs: ReturnType<typeof createBlobs> = []
7070

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+
7186
function start() {
7287
if (timeout) {
7388
clearTimeout(timeout)
7489
}
7590
if (rafId) {
7691
cancelAnimationFrame(rafId)
7792
}
78-
canvas!.width = document.documentElement.clientWidth
79-
canvas!.height = document.documentElement.clientHeight
8093

8194
startBlobs = JSON.parse(JSON.stringify(currentBlobs))
8295
targetBlobs = createBlobs()
@@ -162,8 +175,9 @@ export function BackgroundAnimation() {
162175
}
163176
}
164177

178+
resizeHandler()
165179
start()
166-
window.addEventListener('resize', start)
180+
window.addEventListener('resize', resizeHandler)
167181

168182
return () => {
169183
if (rafId) {
@@ -172,7 +186,7 @@ export function BackgroundAnimation() {
172186
if (timeout) {
173187
clearTimeout(timeout)
174188
}
175-
window.removeEventListener('resize', start)
189+
window.removeEventListener('resize', resizeHandler)
176190
}
177191
}
178192
}, [prefersReducedMotion])

0 commit comments

Comments
 (0)