A React hook that calculates the x and y scale of an object based on its parent width and height. It can be particularly useful when you want to display an image or video in a container with a specific aspect ratio using WebGL or canvas.
Imagine you wanna display a 16/9 aspect ratio image that covers a 1/2 aspect ratio container.
import { useObjectFit } from '@darkroom.engineering/hamo'
function MyComponent() {
/* parentWidth, parentHeight, childWidth, childHeight, objectFit */
const [x, y] = useObjectFit(1, 2, 16, 9, 'cover')
}
parentWidth
(number, optional): The width of the parent container. Default is1
.parentHeight
(number, optional): The height of the parent container. Default is1
.childWidth
(number, optional): The width of the child element. Default is1
.childHeight
(number, optional): The height of the child element. Default is1
.objectFit
(string, optional): The object-fit property of the child element. Default iscover
.
An array containing the width and height of the child element.