-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
@remotion/web-renderer: Support object-fit property
#6206
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
Changes from 3 commits
07136d3
8aa6f86
0ceb1a3
ceb8b98
63cc418
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,260 @@ | ||||||||||||||
| export type ObjectFit = 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'; | ||||||||||||||
|
|
||||||||||||||
| export type ObjectFitResult = { | ||||||||||||||
| // Source rectangle (which part of the image to draw) | ||||||||||||||
| sourceX: number; | ||||||||||||||
| sourceY: number; | ||||||||||||||
| sourceWidth: number; | ||||||||||||||
| sourceHeight: number; | ||||||||||||||
| // Destination rectangle (where to draw on canvas) | ||||||||||||||
| destX: number; | ||||||||||||||
| destY: number; | ||||||||||||||
| destWidth: number; | ||||||||||||||
| destHeight: number; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| type ObjectFitParams = { | ||||||||||||||
| containerSize: {width: number; height: number; left: number; top: number}; | ||||||||||||||
| intrinsicSize: {width: number; height: number}; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * fill: Stretch the image to fill the container, ignoring aspect ratio | ||||||||||||||
| */ | ||||||||||||||
| const calculateFill = ({ | ||||||||||||||
| containerSize, | ||||||||||||||
| intrinsicSize, | ||||||||||||||
| }: ObjectFitParams): ObjectFitResult => { | ||||||||||||||
| return { | ||||||||||||||
| sourceX: 0, | ||||||||||||||
| sourceY: 0, | ||||||||||||||
| sourceWidth: intrinsicSize.width, | ||||||||||||||
| sourceHeight: intrinsicSize.height, | ||||||||||||||
| destX: containerSize.left, | ||||||||||||||
| destY: containerSize.top, | ||||||||||||||
| destWidth: containerSize.width, | ||||||||||||||
| destHeight: containerSize.height, | ||||||||||||||
| }; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * contain: Scale the image to fit inside the container while maintaining aspect ratio. | ||||||||||||||
| * This may result in letterboxing (empty space on sides or top/bottom). | ||||||||||||||
| */ | ||||||||||||||
| const calculateContain = ({ | ||||||||||||||
| containerSize, | ||||||||||||||
| intrinsicSize, | ||||||||||||||
| }: ObjectFitParams): ObjectFitResult => { | ||||||||||||||
|
||||||||||||||
| }: ObjectFitParams): ObjectFitResult => { | |
| }: ObjectFitParams): ObjectFitResult => { | |
| if (containerSize.height <= 0 || intrinsicSize.height <= 0) { | |
| // Fall back to fill behavior if heights are invalid to avoid division by zero | |
| return calculateFill({containerSize, intrinsicSize}); | |
| } |
Uh oh!
There was an error while loading. Please reload this page.