-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: change
onCanvasResize
to useResizeObserver
and provide…
… controls on the observer
- Loading branch information
Showing
4 changed files
with
35 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 24 additions & 8 deletions
32
lib/src/helpers/resize.ts → lib/src/hooks/useResizeObserver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,55 @@ | ||
/** | ||
* Listen for resize of the canvas itself instead of the whole window | ||
* Listen for changes to the size of an element. | ||
*/ | ||
export function onCanvasResize( | ||
canvas: HTMLCanvasElement, | ||
export function useResizeObserver( | ||
target: HTMLElement, | ||
callback: (args: { | ||
/** | ||
* canvas size in CSS pixels | ||
* size of the observed element in CSS pixels | ||
*/ | ||
size: { width: number; height: number }; | ||
/** | ||
* canvas size in device pixels | ||
* size of the observed element in device pixels | ||
*/ | ||
devicePixelSize: { width: number; height: number }; | ||
/** | ||
* untouched, native observer entries | ||
*/ | ||
entries: ResizeObserverEntry[]; | ||
}) => void, | ||
) { | ||
let size: ResizeObserverSize; | ||
let devicePixelSize: ResizeObserverSize; | ||
|
||
const observer = new ResizeObserver((entries) => { | ||
const entry = entries.find((entry) => entry.target === canvas)!; | ||
const entry = entries.find((entry) => entry.target === target)!; | ||
|
||
size = entry.contentBoxSize[0]; | ||
devicePixelSize = entry.devicePixelContentBoxSize?.[0] || { | ||
blockSize: Math.round(size.blockSize * window.devicePixelRatio), | ||
inlineSize: Math.round(size.inlineSize * window.devicePixelRatio), | ||
}; | ||
|
||
// resize after next paint, otherwise there are glitches if a render loop is active | ||
// call the callback after the next paint, otherwise there are glitches when resizing a canvas | ||
// with an active render loop | ||
setTimeout(() => { | ||
callback({ | ||
size: { width: size.inlineSize, height: size.blockSize }, | ||
devicePixelSize: { width: devicePixelSize.inlineSize, height: devicePixelSize.blockSize }, | ||
entries, | ||
}); | ||
}, 0); | ||
}); | ||
|
||
observer.observe(canvas); | ||
observer.observe(target); | ||
|
||
return { | ||
disconnect: observer.disconnect, | ||
observe: () => { | ||
observer.observe(target); | ||
}, | ||
unobserve: () => { | ||
observer.unobserve(target); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters