Skip to content

Commit

Permalink
fix: additional layout configuration that react needs
Browse files Browse the repository at this point in the history
  • Loading branch information
HayesGordon committed Oct 30, 2024
1 parent bdd4c84 commit 83fa663
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/hooks/useRive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import React, {
ComponentProps,
RefCallback,
} from 'react';
import { Rive, EventType } from '@rive-app/canvas';
import { Rive, EventType, Fit } from '@rive-app/canvas';
import { UseRiveParameters, UseRiveOptions, RiveState } from '../types';
import useResizeCanvas from './useResizeCanvas';
import useDevicePixelRatio from './useDevicePixelRatio';
import { getOptions } from '../utils';
import useIntersectionObserver from './useIntersectionObserver';

Expand Down Expand Up @@ -74,16 +75,27 @@ export default function useRive(
const isParamsLoaded = Boolean(riveParams);
const options = getOptions(opts);

const devicePixelRatio = useDevicePixelRatio();

/**
* When the canvas/parent container resize, reset the Rive layout to match the
* new (0, 0, canvas.width, canvas.height) bounds in the render loop
*/
const onCanvasHasResized = useCallback(() => {
if (rive) {
if (rive.layout.fit === Fit.Layout) {
if (canvasElem) {
// TODO (Gordon): expose these are properties on JS runtime
(rive as any)._devicePixelRatioUsed = devicePixelRatio;
(rive as any).artboard.width = canvasElem?.width / devicePixelRatio;
(rive as any).artboard.height = canvasElem?.height / devicePixelRatio;
}
}

rive.startRendering();
rive.resizeToCanvas();
}
}, [rive]);
}, [rive, devicePixelRatio]);

// Watch the canvas parent container resize and size the canvas to match
useResizeCanvas({
Expand Down

0 comments on commit 83fa663

Please sign in to comment.