Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/hooks/useResizeCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ export default function useResizeCanvas({
const { maxX, maxY } = artboardBounds ?? {};

const getContainerDimensions = useCallback(() => {
const width = containerRef.current?.clientWidth ?? 0;
const height = containerRef.current?.clientHeight ?? 0;
const boundingBox = containerRef.current?.getBoundingClientRect();
const width = Math.ceil(boundingBox?.width ?? 0);
const height = Math.ceil(boundingBox?.height ?? 0);
if (fitCanvasToArtboardHeight && artboardBounds) {
const { maxY, maxX } = artboardBounds;
return { width, height: width * (maxY / maxX) };
Expand Down
51 changes: 35 additions & 16 deletions test/useRive.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ jest.mock('@rive-app/canvas', () => ({
describe('useRive', () => {
let controlledRiveloadCb: () => void;
let baseRiveMock: Partial<rive.Rive>;
const mockBoundingBox = {
width: 100,
height: 100,
x: 0,
top: 0,
y: 0,
bottom: 100,
left: 0,
right: 100,
toJSON: () => '',
};

beforeEach(() => {
baseRiveMock = {
Expand Down Expand Up @@ -162,8 +173,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params));

Expand Down Expand Up @@ -200,8 +212,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() =>
useRive(params, { customDevicePixelRatio: 1 })
Expand Down Expand Up @@ -241,8 +254,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params, opts));

Expand Down Expand Up @@ -284,8 +298,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params, opts));

Expand Down Expand Up @@ -468,8 +483,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params));

Expand Down Expand Up @@ -500,16 +516,18 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params));

await act(async () => {
result.current.setCanvasRef(canvasSpy);
result.current.setContainerRef(containerSpy);
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(200);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(200);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue({...mockBoundingBox, width: 200, height: 200});
});
await waitFor(() => {
expect(result.current.canvas).toBe(canvasSpy);
Expand Down Expand Up @@ -559,8 +577,9 @@ describe('useRive', () => {
});

await act(async () => {
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(500);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(500);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue({...mockBoundingBox, width: 500, height: 500});
containerSpy.dispatchEvent(new Event('resize'));
});

Expand Down