update canvas dimensions to use clientWidth and Height as opposed to BoundingClient, to avoid getting scaled information

This commit is contained in:
Maxwell Talbot
2022-07-14 13:08:11 +01:00
parent 45aec2db1c
commit fd1c00a995
2 changed files with 12 additions and 14 deletions

View File

@@ -104,8 +104,12 @@ export default function useRive(
* @returns Dimensions object.
*/
function getCanvasDimensions() {
const { width, height } =
containerRef.current?.getBoundingClientRect() ?? new DOMRect(0, 0, 0, 0);
// getBoundingClientRect returns the scaled width and height
// this will result in double scaling
// https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements
const width = containerRef.current?.clientWidth ?? 0;
const height = containerRef.current?.clientHeight ?? 0;
if (rive && options.fitCanvasToArtboardHeight) {
const { maxY, maxX } = rive.bounds;

View File

@@ -136,10 +136,8 @@ describe('useRive', () => {
const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
containerSpy.getBoundingClientRect = jest.fn().mockImplementation(() => ({
width: 100,
height: 100,
}));
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
const { result } = renderHook(() => useRive(params));
@@ -176,10 +174,8 @@ describe('useRive', () => {
const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
containerSpy.getBoundingClientRect = jest.fn().mockImplementation(() => ({
width: 100,
height: 100,
}));
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
const { result } = renderHook(() => useRive(params, opts));
@@ -216,10 +212,8 @@ describe('useRive', () => {
const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
containerSpy.getBoundingClientRect = jest.fn().mockImplementation(() => ({
width: 100,
height: 100,
}));
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
const { result } = renderHook(() => useRive(params, opts));