RiveComponent Scaling causes Pixilation.. Help
my riveComponent which scales when the top parent two level up triggers a scale event but the resolution is pixilated on the scale up? Not sure what i’m missing or if there is a cleaner approach to this current approach. Claude produced the code below which helped resolve the scaling event issue. Surprised at how many useEffects are utilized but its clear that the rive element is not updating to the scaling event correctly since all the rive elements are vector?
-newbie to react and next.js
'use client'
import { useRive, Layout, Fit, Alignment } from '@rive-app/react-canvas'
import { useState, useEffect, useRef } from 'react'
import { RivePostcardViewerProps } from '@/app/_types/postcard'
import { cn } from '@/app/_lib/utils'
export const RivePostcardViewer = ({
riveBlob,
stateMachine = 'bubbles',
className,
isScaled = false,
}: RivePostcardViewerProps) => {
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const [blobUrl, setBlobUrl] = useState<string>('')
const isMounted = useRef(true)
const containerRef = useRef<HTMLDivElement>(null)
// Log component lifecycle
useEffect(() => {
console.log('RivePostcardViewer - mounted')
return () => {
console.log('RivePostcardViewer - unmounted')
}
}, [])
// Create blob URL from Rive blob
useEffect(() => {
if (riveBlob) {
const url = URL.createObjectURL(riveBlob)
setBlobUrl(url)
setIsLoading(false)
return () => {
URL.revokeObjectURL(url)
}
}
}, [riveBlob])
// Initialize Rive with the correct layout based on scaling
const { RiveComponent, rive } = useRive({
src: blobUrl,
stateMachines: stateMachine ? [stateMachine] : undefined,
autoplay: true,
layout: new Layout({
fit: isScaled ? Fit.Fill : Fit.Fill,
alignment: Alignment.Center,
}),
onLoad: () => {
console.log('Rive animation loaded successfully')
rive?.resizeDrawingSurfaceToCanvas()
setIsLoading(false)
},
onLoadError: (err) => {
if (isMounted.current) {
console.error('Rive load error:', err)
setError(err instanceof Error ? err.message : 'Failed to load animation')
setIsLoading(false)
}
}
})
// Direct DOM manipulation fallback
useEffect(() => {
if (!rive) return
// this is what resolved the pixilation.
window.addEventListener('resize', () => {
rive?.resizeDrawingSurfaceToCanvas()
}, false)
// Additional fallback: directly manipulate canvas elements
const canvasCheck = setInterval(() => {
const canvasElements = document.querySelectorAll('canvas')
if (canvasElements.length > 0) {
console.log('Found canvas elements:', canvasElements.length)
canvasElements.forEach(canvas => {
const parent = canvas.parentElement
if (parent) {
// Try to apply scaling directly
if (isScaled) {
canvas.style.objectFit = 'fill'
canvas.style.maxWidth = '100%'
canvas.style.maxHeight = '100%'
} else {
canvas.style.objectFit = 'fill'
canvas.style.width = '100%'
canvas.style.height = '100%'
}
}
})
clearInterval(canvasCheck)
}
}, 100)
return () => {
clearInterval(canvasCheck)
}
}, [rive, isScaled])
if (isLoading) {
return (
<div className={cn("flex items-center justify-center w-full h-full", className)}>
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900" />
</div>
)
}
if (error) {
return (
<div className={cn("flex items-center justify-center w-full h-full", className)}>
<p className="text-red-500">{error}</p>
</div>
)
}
return (
<div
ref={containerRef}
className={cn(
"absolute inset-0 w-full h-full rive-component-wrapper",
className
)}
>
<RiveComponent />
</div>
)
}