Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize aspect-ratio / padding to fix video overflow #641

Merged
merged 8 commits into from
Sep 8, 2022
20 changes: 18 additions & 2 deletions packages/js/src/utils/videoElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,27 @@ const setVideoMediaTrack = ({
}) => {
element.srcObject = new MediaStream([track])

const debounce = <F extends (...args: any[]) => any>(
func: F,
timeout = 250
) => {
let timer!: ReturnType<typeof setTimeout>

return (...args: Parameters<F>): Promise<ReturnType<F>> =>
new Promise((resolve) => {
if (timer) clearTimeout(timer)
timer = setTimeout(() => resolve(func(...args)), timeout)
})
}

// debounce to avoid multiple calls
const debounceGetDimensionsFromResize = debounce(getDimensionsFromResize, 150)
edolix marked this conversation as resolved.
Show resolved Hide resolved

const rsObserver = new ResizeObserver((entries) => {
entries.forEach((entry) => {
// newer api but less supported
if (entry.contentBoxSize?.length > 0) {
getDimensionsFromResize({
debounceGetDimensionsFromResize({
width: entry.contentBoxSize[0].inlineSize,
height: entry.contentBoxSize[0].blockSize,
})
Expand All @@ -184,7 +200,7 @@ const setVideoMediaTrack = ({
entry.contentRect.width !== 0 &&
entry.contentRect.height !== 0
) {
getDimensionsFromResize({
debounceGetDimensionsFromResize({
width: entry.contentRect.width,
height: entry.contentRect.height,
})
Expand Down