🎥 Make videos programmatically with React
reasonable aspect ratio implementation
Jonny Burgerdaa8a7af35c6
diff --git a/packages/docs/docs/player.md b/packages/docs/docs/player.mdindex 66760f2444..921d7ecb3e 100644--- a/packages/docs/docs/player.md+++ b/packages/docs/docs/player.md@@ -261,5 +261,4 @@ Before we mark the player as stable, we are looking to improve in the following- Better props validation- Customize error UI- Volume slider-- Automatically adapt correct aspect ratio if passing styles like "width: 100%".- Fix the fullscreen icon - it should inverse when the player is already in fullscreen.diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsxindex 4630984ad9..c02521111f 100644--- a/packages/player/src/Player.tsx+++ b/packages/player/src/Player.tsx@@ -52,7 +52,6 @@ export const PlayerFn = <T,>(compositionHeight: height,compositionWidth: width,fps,- props,controls,style,loop,@@ -100,7 +99,7 @@ export const PlayerFn = <T,>(width,fps,id: 'player-comp',- props,+ props: inputProps,nonce: 777,},],@@ -115,7 +114,7 @@ export const PlayerFn = <T,>(unregisterAsset: () => undefined,assets: [],};- }, [component, props, durationInFrames, fps, height, width]);+ }, [component, durationInFrames, height, width, fps, inputProps]);return (<Internals.Timeline.TimelineContext.Provider value={timelineContextValue}>diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsxindex 6a00c3b235..3e90624607 100644--- a/packages/player/src/PlayerUI.tsx+++ b/packages/player/src/PlayerUI.tsx@@ -1,3 +1,4 @@+import {StandardLonghandProperties} from 'csstype';import React, {forwardRef,Suspense,@@ -17,6 +18,7 @@ import {Controls} from './PlayerControls';import {useHoverState} from './use-hover-state';import {usePlayback} from './use-playback';import {usePlayer} from './use-player';+import {calculatePlayerSize} from './utils/calculate-player-size';import {IS_NODE} from './utils/is-node';import {useElementSize} from './utils/use-element-size';@@ -47,8 +49,6 @@ const PlayerUI: React.ForwardRefRenderFunction<usePlayback({loop});const player = usePlayer();- console.log({inputProps});-useEffect(() => {if (hasPausedToResume && !player.playing) {setHasPausedToResume(false);@@ -116,11 +116,16 @@ const PlayerUI: React.ForwardRefRenderFunction<return {position: 'relative',overflow: 'hidden',- width: config.width,- height: config.height,+ ...calculatePlayerSize({+ compositionHeight: config.height,+ compositionWidth: config.width,+ currentSize: canvasSize,+ height: style?.height as StandardLonghandProperties['width'],+ width: style?.width as StandardLonghandProperties['height'],+ }),...style,};- }, [config, style]);+ }, [canvasSize, config, style]);const layout = useMemo(() => {if (!config || !canvasSize) {diff --git a/packages/player/src/utils/calculate-player-size.ts b/packages/player/src/utils/calculate-player-size.tsnew file mode 100644index 0000000000..e65833d5e8--- /dev/null+++ b/packages/player/src/utils/calculate-player-size.ts@@ -0,0 +1,44 @@+import {StandardLonghandProperties} from 'csstype';+import {Size} from './use-element-size';++export const calculatePlayerSize = ({+ currentSize,+ width,+ height,+ compositionWidth,+ compositionHeight,+}: {+ currentSize: Size | null;+ width: StandardLonghandProperties['width'] | undefined;+ height: StandardLonghandProperties['height'] | undefined;+ compositionWidth: number;+ compositionHeight: number;+}): React.CSSProperties => {+ const aspectRatio = compositionWidth / compositionHeight;++ if (!currentSize) {+ return {+ width: compositionWidth,+ height: compositionHeight,+ };+ }++ // If has width specified, but no height, specify a default height that satisfies the aspect ratio.+ if (width !== undefined && height === undefined) {+ return {+ height: currentSize.width / aspectRatio,+ };+ }++ // Opposite: If has height specified, evaluate the height and specify a default width.+ if (height !== undefined && width === undefined) {+ return {+ width: currentSize.height * aspectRatio,+ };+ }++ return {+ width: compositionWidth,+ height: compositionHeight,+ };+};