@remotion/web-renderer 4.0.397 → 4.0.399

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/dist/artifact.d.ts +2 -2
  2. package/dist/calculate-transforms.d.ts +0 -2
  3. package/dist/calculate-transforms.js +0 -17
  4. package/dist/composable.d.ts +8 -2
  5. package/dist/compose-canvas.js +4 -28
  6. package/dist/compose.d.ts +2 -1
  7. package/dist/compose.js +34 -35
  8. package/dist/drawing/border-radius.d.ts +3 -1
  9. package/dist/drawing/border-radius.js +27 -6
  10. package/dist/drawing/calculate-object-fit.d.ts +40 -0
  11. package/dist/drawing/calculate-object-fit.js +208 -0
  12. package/dist/drawing/draw-background.d.ts +13 -0
  13. package/dist/drawing/draw-background.js +62 -0
  14. package/dist/drawing/draw-box-shadow.d.ts +1 -1
  15. package/dist/drawing/draw-box-shadow.js +1 -1
  16. package/dist/drawing/draw-dom-element.js +78 -10
  17. package/dist/drawing/draw-element-to-canvas.d.ts +1 -2
  18. package/dist/drawing/draw-element-to-canvas.js +36 -8
  19. package/dist/drawing/draw-element.d.ts +4 -1
  20. package/dist/drawing/draw-element.js +21 -35
  21. package/dist/drawing/fit-svg-into-its-dimensions.d.ts +12 -0
  22. package/dist/drawing/fit-svg-into-its-dimensions.js +35 -0
  23. package/dist/drawing/get-clipped-background.d.ts +8 -0
  24. package/dist/drawing/get-clipped-background.js +14 -0
  25. package/dist/drawing/get-padding-box.d.ts +1 -0
  26. package/dist/drawing/get-padding-box.js +30 -0
  27. package/dist/drawing/get-pretransform-rect.js +13 -0
  28. package/dist/drawing/go-rects-intersect.d.ts +1 -0
  29. package/dist/drawing/go-rects-intersect.js +6 -0
  30. package/dist/drawing/handle-3d-transform.d.ts +4 -2
  31. package/dist/drawing/handle-3d-transform.js +4 -3
  32. package/dist/drawing/handle-mask.js +2 -0
  33. package/dist/drawing/overflow.d.ts +3 -1
  34. package/dist/drawing/overflow.js +3 -1
  35. package/dist/drawing/parse-linear-gradient.d.ts +3 -1
  36. package/dist/drawing/parse-linear-gradient.js +3 -3
  37. package/dist/drawing/precompose.js +1 -0
  38. package/dist/drawing/process-node.js +5 -6
  39. package/dist/drawing/text/draw-text.d.ts +2 -1
  40. package/dist/drawing/text/draw-text.js +9 -3
  41. package/dist/drawing/text/get-base-height.d.ts +1 -0
  42. package/dist/drawing/text/get-base-height.js +13 -0
  43. package/dist/drawing/text/handle-text-node.d.ts +2 -1
  44. package/dist/drawing/text/handle-text-node.js +2 -2
  45. package/dist/drawing/transform-in-3d.d.ts +3 -2
  46. package/dist/drawing/transform-in-3d.js +106 -87
  47. package/dist/esm/index.mjs +687 -172
  48. package/dist/find-capturable-elements.d.ts +1 -1
  49. package/dist/find-capturable-elements.js +22 -20
  50. package/dist/index.d.ts +4 -2
  51. package/dist/internal-state.d.ts +19 -0
  52. package/dist/internal-state.js +9 -0
  53. package/dist/mediabunny-mappings.d.ts +3 -3
  54. package/dist/render-media-on-web.d.ts +4 -4
  55. package/dist/render-media-on-web.js +8 -3
  56. package/dist/render-still-on-web.d.ts +2 -2
  57. package/dist/render-still-on-web.js +1 -0
  58. package/dist/send-telemetry-event.js +2 -2
  59. package/dist/take-screenshot.js +1 -0
  60. package/dist/tree-walker-cleanup-after-children.d.ts +5 -0
  61. package/dist/tree-walker-cleanup-after-children.js +33 -0
  62. package/package.json +9 -8
  63. package/dist/border-radius.d.ts +0 -31
  64. package/dist/border-radius.js +0 -152
  65. package/dist/drawing/canvas-offset-from-rect.d.ts +0 -8
  66. package/dist/drawing/canvas-offset-from-rect.js +0 -12
  67. package/dist/drawing/compose-canvas.d.ts +0 -1
  68. package/dist/drawing/compose-canvas.js +0 -36
  69. package/dist/drawing/compose-svg.d.ts +0 -1
  70. package/dist/drawing/compose-svg.js +0 -34
  71. package/dist/drawing/compose.d.ts +0 -5
  72. package/dist/drawing/compose.js +0 -6
  73. package/dist/drawing/get-bounding-box-including-shadow.d.ts +0 -1
  74. package/dist/drawing/get-bounding-box-including-shadow.js +0 -6
  75. package/dist/drawing/get-computed-style-cache.d.ts +0 -0
  76. package/dist/drawing/get-computed-style-cache.js +0 -1
  77. package/dist/find-canvas-elements.d.ts +0 -1
  78. package/dist/find-canvas-elements.js +0 -13
  79. package/dist/opacity.d.ts +0 -4
  80. package/dist/opacity.js +0 -7
  81. package/dist/transform.d.ts +0 -4
  82. package/dist/transform.js +0 -6
@@ -9,7 +9,7 @@ export declare const onlyArtifact: ({ assets, frameBuffer, }: {
9
9
  assets: TRenderAsset[];
10
10
  frameBuffer: Blob | OffscreenCanvas | null;
11
11
  }) => Promise<EmittedArtifact[]>;
12
- export type OnArtifact = (asset: EmittedArtifact) => void;
12
+ export type WebRendererOnArtifact = (asset: EmittedArtifact) => void;
13
13
  export type ArtifactsRef = React.RefObject<{
14
14
  collectAssets: () => TRenderAsset[];
15
15
  } | null>;
@@ -18,6 +18,6 @@ export declare const handleArtifacts: () => {
18
18
  imageData: Blob | OffscreenCanvas | null;
19
19
  frame: number;
20
20
  assets: TRenderAsset[];
21
- onArtifact: OnArtifact;
21
+ onArtifact: WebRendererOnArtifact;
22
22
  }) => Promise<void>;
23
23
  };
@@ -6,6 +6,4 @@ export declare const calculateTransforms: (element: HTMLElement | SVGSVGElement)
6
6
  x: number;
7
7
  y: number;
8
8
  };
9
- borderRadius: import("./drawing/border-radius").BorderRadiusCorners;
10
- opacity: number;
11
9
  };
@@ -1,4 +1,3 @@
1
- import { parseBorderRadius } from './drawing/border-radius';
2
1
  import { parseTransformOrigin } from './parse-transform-origin';
3
2
  const getInternalTransformOrigin = (transform) => {
4
3
  var _a;
@@ -22,18 +21,8 @@ export const calculateTransforms = (element) => {
22
21
  let parent = element;
23
22
  const transforms = [];
24
23
  const toReset = [];
25
- let borderRadius = '';
26
- let opacity = 1;
27
24
  while (parent) {
28
25
  const computedStyle = getComputedStyle(parent);
29
- if (parent === element) {
30
- borderRadius = computedStyle.borderRadius;
31
- }
32
- // Multiply opacity values from element and all parents
33
- const parentOpacity = computedStyle.opacity;
34
- if (parentOpacity && parentOpacity !== '') {
35
- opacity *= parseFloat(parentOpacity);
36
- }
37
26
  if ((computedStyle.transform && computedStyle.transform !== 'none') ||
38
27
  parent === element) {
39
28
  const toParse = computedStyle.transform === 'none' || computedStyle.transform === ''
@@ -81,11 +70,5 @@ export const calculateTransforms = (element) => {
81
70
  }
82
71
  },
83
72
  nativeTransformOrigin,
84
- borderRadius: parseBorderRadius({
85
- borderRadius,
86
- width: dimensions.width,
87
- height: dimensions.height,
88
- }),
89
- opacity,
90
73
  };
91
74
  };
@@ -1,4 +1,10 @@
1
1
  export type Composable = {
2
- type: 'element';
3
- element: HTMLElement | SVGElement;
2
+ type: 'canvas';
3
+ element: HTMLCanvasElement;
4
+ } | {
5
+ type: 'svg';
6
+ element: SVGSVGElement;
7
+ } | {
8
+ type: 'img';
9
+ element: HTMLImageElement;
4
10
  };
@@ -1,36 +1,12 @@
1
- import { setBorderRadius } from './border-radius';
2
1
  import { calculateTransforms } from './calculate-transforms';
3
- import { turnSvgIntoDrawable } from './drawing/compose-svg';
4
- import { setOpacity } from './drawing/opacity';
5
- import { setTransform } from './drawing/transform';
2
+ import { turnSvgIntoDrawable } from './compose-svg';
6
3
  export const composeCanvas = async (canvas, context) => {
7
- const { totalMatrix, reset, dimensions, borderRadius, opacity } = calculateTransforms(canvas);
8
- if (opacity === 0) {
9
- reset();
10
- return;
11
- }
4
+ const { totalMatrix, reset, dimensions } = calculateTransforms(canvas);
5
+ context.setTransform(totalMatrix);
12
6
  const drawable = canvas instanceof SVGSVGElement
13
7
  ? await turnSvgIntoDrawable(canvas)
14
8
  : canvas;
15
- const finishTransform = setTransform({
16
- ctx: context,
17
- transform: totalMatrix,
18
- });
19
- const finishBorderRadius = setBorderRadius({
20
- ctx: context,
21
- x: dimensions.left,
22
- y: dimensions.top,
23
- width: dimensions.width,
24
- height: dimensions.height,
25
- borderRadius,
26
- });
27
- const finishOpacity = setOpacity({
28
- ctx: context,
29
- opacity,
30
- });
31
9
  context.drawImage(drawable, dimensions.left, dimensions.top, dimensions.width, dimensions.height);
32
- finishOpacity();
33
- finishBorderRadius();
34
- finishTransform();
10
+ context.setTransform(new DOMMatrix());
35
11
  reset();
36
12
  };
package/dist/compose.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import type { LogLevel } from 'remotion';
2
2
  import type { InternalState } from './internal-state';
3
- export declare const compose: ({ element, context, logLevel, parentRect, internalState, }: {
3
+ export declare const compose: ({ element, context, logLevel, parentRect, internalState, onlyBackgroundClip, }: {
4
4
  element: HTMLElement | SVGElement;
5
5
  context: OffscreenCanvasRenderingContext2D;
6
6
  logLevel: LogLevel;
7
7
  parentRect: DOMRect;
8
8
  internalState: InternalState;
9
+ onlyBackgroundClip: boolean;
9
10
  }) => Promise<void>;
package/dist/compose.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { drawDomElement } from './drawing/draw-dom-element';
2
2
  import { processNode } from './drawing/process-node';
3
3
  import { handleTextNode } from './drawing/text/handle-text-node';
4
+ import { createTreeWalkerCleanupAfterChildren } from './tree-walker-cleanup-after-children';
4
5
  import { skipToNextNonDescendant } from './walk-tree';
5
- const walkOverNode = ({ node, context, logLevel, parentRect, internalState, rootElement, }) => {
6
+ const walkOverNode = ({ node, context, logLevel, parentRect, internalState, rootElement, onlyBackgroundClip, }) => {
6
7
  if (node instanceof HTMLElement || node instanceof SVGElement) {
7
8
  return processNode({
8
9
  element: node,
@@ -22,38 +23,41 @@ const walkOverNode = ({ node, context, logLevel, parentRect, internalState, root
22
23
  parentRect,
23
24
  internalState,
24
25
  rootElement,
26
+ onlyBackgroundClip,
25
27
  });
26
28
  }
27
29
  throw new Error('Unknown node type');
28
30
  };
29
- export const compose = async ({ element, context, logLevel, parentRect, internalState, }) => {
30
- const cleanupAfterChildren = [];
31
- const treeWalker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, (node) => {
32
- if (node instanceof Element) {
33
- // SVG does have children, but we process SVG elements in its
34
- // entirety
35
- if (node.parentElement instanceof SVGSVGElement) {
36
- return NodeFilter.FILTER_REJECT;
37
- }
38
- const computedStyle = getComputedStyle(node);
39
- return computedStyle.display === 'none'
40
- ? NodeFilter.FILTER_REJECT
41
- : NodeFilter.FILTER_ACCEPT;
42
- }
31
+ const getFilterFunction = (node) => {
32
+ if (!(node instanceof Element)) {
33
+ // Must be a text node!
43
34
  return NodeFilter.FILTER_ACCEPT;
44
- });
45
- while (true) {
46
- for (let i = 0; i < cleanupAfterChildren.length;) {
47
- const cleanup = cleanupAfterChildren[i];
48
- if (!(cleanup.element === treeWalker.currentNode ||
49
- cleanup.element.contains(treeWalker.currentNode))) {
50
- cleanup.cleanupFn();
51
- cleanupAfterChildren.splice(i, 1);
52
- }
53
- else {
54
- i++;
55
- }
35
+ }
36
+ // SVG does have children, but we process SVG elements in its
37
+ // entirety
38
+ if (node.parentElement instanceof SVGSVGElement) {
39
+ return NodeFilter.FILTER_REJECT;
40
+ }
41
+ const computedStyle = getComputedStyle(node);
42
+ if (computedStyle.display === 'none') {
43
+ return NodeFilter.FILTER_REJECT;
44
+ }
45
+ return NodeFilter.FILTER_ACCEPT;
46
+ };
47
+ export const compose = async ({ element, context, logLevel, parentRect, internalState, onlyBackgroundClip, }) => {
48
+ const treeWalker = document.createTreeWalker(element, onlyBackgroundClip
49
+ ? NodeFilter.SHOW_TEXT
50
+ : NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, getFilterFunction);
51
+ // Skip to the first text node
52
+ if (onlyBackgroundClip) {
53
+ treeWalker.nextNode();
54
+ if (!treeWalker.currentNode) {
55
+ return;
56
56
  }
57
+ }
58
+ const { checkCleanUpAtBeginningOfIteration, addCleanup, cleanupInTheEndOfTheIteration, } = createTreeWalkerCleanupAfterChildren(treeWalker);
59
+ while (true) {
60
+ checkCleanUpAtBeginningOfIteration();
57
61
  const val = await walkOverNode({
58
62
  node: treeWalker.currentNode,
59
63
  context,
@@ -61,6 +65,7 @@ export const compose = async ({ element, context, logLevel, parentRect, internal
61
65
  parentRect,
62
66
  internalState,
63
67
  rootElement: element,
68
+ onlyBackgroundClip,
64
69
  });
65
70
  if (val.type === 'skip-children') {
66
71
  if (!skipToNextNonDescendant(treeWalker)) {
@@ -69,18 +74,12 @@ export const compose = async ({ element, context, logLevel, parentRect, internal
69
74
  }
70
75
  else {
71
76
  if (val.cleanupAfterChildren) {
72
- // Last registered must be cleaned up first
73
- cleanupAfterChildren.unshift({
74
- element: treeWalker.currentNode,
75
- cleanupFn: val.cleanupAfterChildren,
76
- });
77
+ addCleanup(treeWalker.currentNode, val.cleanupAfterChildren);
77
78
  }
78
79
  if (!treeWalker.nextNode()) {
79
80
  break;
80
81
  }
81
82
  }
82
83
  }
83
- for (const cleanup of cleanupAfterChildren) {
84
- cleanup.cleanupFn();
85
- }
84
+ cleanupInTheEndOfTheIteration();
86
85
  };
@@ -21,9 +21,11 @@ export declare function parseBorderRadius({ borderRadius, width, height, }: {
21
21
  width: number;
22
22
  height: number;
23
23
  }): BorderRadiusCorners;
24
- export declare function setBorderRadius({ ctx, rect, borderRadius, forceClipEvenWhenZero, }: {
24
+ export declare function setBorderRadius({ ctx, rect, borderRadius, forceClipEvenWhenZero, computedStyle, backgroundClip, }: {
25
25
  ctx: OffscreenCanvasRenderingContext2D;
26
26
  rect: DOMRect;
27
27
  borderRadius: BorderRadiusCorners;
28
28
  forceClipEvenWhenZero: boolean;
29
+ computedStyle: CSSStyleDeclaration;
30
+ backgroundClip: string;
29
31
  }): () => void;
@@ -1,4 +1,5 @@
1
1
  import { drawRoundedRectPath } from './draw-rounded';
2
+ import { getBoxBasedOnBackgroundClip } from './get-padding-box';
2
3
  function parseValue({ value, reference, }) {
3
4
  value = value.trim();
4
5
  if (value.endsWith('%')) {
@@ -102,7 +103,7 @@ export function parseBorderRadius({ borderRadius, width, height, }) {
102
103
  height,
103
104
  });
104
105
  }
105
- export function setBorderRadius({ ctx, rect, borderRadius, forceClipEvenWhenZero = false, }) {
106
+ export function setBorderRadius({ ctx, rect, borderRadius, forceClipEvenWhenZero = false, computedStyle, backgroundClip, }) {
106
107
  if (borderRadius.topLeft.horizontal === 0 &&
107
108
  borderRadius.topLeft.vertical === 0 &&
108
109
  borderRadius.topRight.horizontal === 0 &&
@@ -115,13 +116,33 @@ export function setBorderRadius({ ctx, rect, borderRadius, forceClipEvenWhenZero
115
116
  return () => { };
116
117
  }
117
118
  ctx.save();
119
+ const boundingRect = getBoxBasedOnBackgroundClip(rect, computedStyle, backgroundClip);
120
+ // See background-clip tests for why this logic matters!
121
+ const actualBorderRadius = {
122
+ topLeft: {
123
+ horizontal: Math.max(0, borderRadius.topLeft.horizontal - (boundingRect.left - rect.left)),
124
+ vertical: Math.max(0, borderRadius.topLeft.vertical - (boundingRect.top - rect.top)),
125
+ },
126
+ topRight: {
127
+ horizontal: Math.max(0, borderRadius.topRight.horizontal - (rect.right - boundingRect.right)),
128
+ vertical: Math.max(0, borderRadius.topRight.vertical - (boundingRect.top - rect.top)),
129
+ },
130
+ bottomRight: {
131
+ horizontal: Math.max(0, borderRadius.bottomRight.horizontal - (rect.right - boundingRect.right)),
132
+ vertical: Math.max(0, borderRadius.bottomRight.vertical - (rect.bottom - boundingRect.bottom)),
133
+ },
134
+ bottomLeft: {
135
+ horizontal: Math.max(0, borderRadius.bottomLeft.horizontal - (boundingRect.left - rect.left)),
136
+ vertical: Math.max(0, borderRadius.bottomLeft.vertical - (rect.bottom - boundingRect.bottom)),
137
+ },
138
+ };
118
139
  drawRoundedRectPath({
119
140
  ctx,
120
- x: rect.left,
121
- y: rect.top,
122
- width: rect.width,
123
- height: rect.height,
124
- borderRadius,
141
+ x: boundingRect.left,
142
+ y: boundingRect.top,
143
+ width: boundingRect.width,
144
+ height: boundingRect.height,
145
+ borderRadius: actualBorderRadius,
125
146
  });
126
147
  ctx.clip();
127
148
  return () => {
@@ -0,0 +1,40 @@
1
+ export type ObjectFit = 'fill' | 'contain' | 'cover' | 'none' | 'scale-down';
2
+ export type ObjectFitResult = {
3
+ sourceX: number;
4
+ sourceY: number;
5
+ sourceWidth: number;
6
+ sourceHeight: number;
7
+ destX: number;
8
+ destY: number;
9
+ destWidth: number;
10
+ destHeight: number;
11
+ };
12
+ type ObjectFitParams = {
13
+ containerSize: {
14
+ width: number;
15
+ height: number;
16
+ left: number;
17
+ top: number;
18
+ };
19
+ intrinsicSize: {
20
+ width: number;
21
+ height: number;
22
+ };
23
+ };
24
+ /**
25
+ * Calculates how to draw an image based on object-fit CSS property.
26
+ *
27
+ * @param objectFit - The CSS object-fit value
28
+ * @param containerSize - The container dimensions (where the image should be drawn)
29
+ * @param intrinsicSize - The natural/intrinsic size of the image
30
+ * @returns Source and destination rectangles for drawImage
31
+ */
32
+ export declare const calculateObjectFit: ({ objectFit, containerSize, intrinsicSize, }: {
33
+ objectFit: ObjectFit;
34
+ } & ObjectFitParams) => ObjectFitResult;
35
+ /**
36
+ * Parse an object-fit CSS value string into our ObjectFit type.
37
+ * Returns 'fill' as the default if the value is not recognized.
38
+ */
39
+ export declare const parseObjectFit: (value: string | null | undefined) => ObjectFit;
40
+ export {};
@@ -0,0 +1,208 @@
1
+ /**
2
+ * fill: Stretch the image to fill the container, ignoring aspect ratio
3
+ */
4
+ const calculateFill = ({ containerSize, intrinsicSize, }) => {
5
+ return {
6
+ sourceX: 0,
7
+ sourceY: 0,
8
+ sourceWidth: intrinsicSize.width,
9
+ sourceHeight: intrinsicSize.height,
10
+ destX: containerSize.left,
11
+ destY: containerSize.top,
12
+ destWidth: containerSize.width,
13
+ destHeight: containerSize.height,
14
+ };
15
+ };
16
+ /**
17
+ * contain: Scale the image to fit inside the container while maintaining aspect ratio.
18
+ * This may result in letterboxing (empty space on sides or top/bottom).
19
+ */
20
+ const calculateContain = ({ containerSize, intrinsicSize, }) => {
21
+ const containerAspect = containerSize.width / containerSize.height;
22
+ const imageAspect = intrinsicSize.width / intrinsicSize.height;
23
+ let destWidth;
24
+ let destHeight;
25
+ if (imageAspect > containerAspect) {
26
+ // Image is wider than container (relative to their heights)
27
+ // Fit by width, letterbox top/bottom
28
+ destWidth = containerSize.width;
29
+ destHeight = containerSize.width / imageAspect;
30
+ }
31
+ else {
32
+ // Image is taller than container (relative to their widths)
33
+ // Fit by height, letterbox left/right
34
+ destHeight = containerSize.height;
35
+ destWidth = containerSize.height * imageAspect;
36
+ }
37
+ // Center the image in the container
38
+ const destX = containerSize.left + (containerSize.width - destWidth) / 2;
39
+ const destY = containerSize.top + (containerSize.height - destHeight) / 2;
40
+ return {
41
+ sourceX: 0,
42
+ sourceY: 0,
43
+ sourceWidth: intrinsicSize.width,
44
+ sourceHeight: intrinsicSize.height,
45
+ destX,
46
+ destY,
47
+ destWidth,
48
+ destHeight,
49
+ };
50
+ };
51
+ /**
52
+ * cover: Scale the image to cover the container while maintaining aspect ratio.
53
+ * Parts of the image may be cropped.
54
+ */
55
+ const calculateCover = ({ containerSize, intrinsicSize, }) => {
56
+ // Guard against zero or non-positive heights to avoid division by zero and NaN/Infinity.
57
+ if (containerSize.height <= 0 || intrinsicSize.height <= 0) {
58
+ return {
59
+ sourceX: 0,
60
+ sourceY: 0,
61
+ sourceWidth: 0,
62
+ sourceHeight: 0,
63
+ destX: containerSize.left,
64
+ destY: containerSize.top,
65
+ destWidth: 0,
66
+ destHeight: 0,
67
+ };
68
+ }
69
+ const containerAspect = containerSize.width / containerSize.height;
70
+ const imageAspect = intrinsicSize.width / intrinsicSize.height;
71
+ let sourceX = 0;
72
+ let sourceY = 0;
73
+ let sourceWidth = intrinsicSize.width;
74
+ let sourceHeight = intrinsicSize.height;
75
+ if (imageAspect > containerAspect) {
76
+ // Image is wider than container - crop horizontally
77
+ // Scale by height, then crop width
78
+ sourceWidth = intrinsicSize.height * containerAspect;
79
+ sourceX = (intrinsicSize.width - sourceWidth) / 2;
80
+ }
81
+ else {
82
+ // Image is taller than container - crop vertically
83
+ // Scale by width, then crop height
84
+ sourceHeight = intrinsicSize.width / containerAspect;
85
+ sourceY = (intrinsicSize.height - sourceHeight) / 2;
86
+ }
87
+ return {
88
+ sourceX,
89
+ sourceY,
90
+ sourceWidth,
91
+ sourceHeight,
92
+ destX: containerSize.left,
93
+ destY: containerSize.top,
94
+ destWidth: containerSize.width,
95
+ destHeight: containerSize.height,
96
+ };
97
+ };
98
+ /**
99
+ * none: Draw the image at its natural size, centered in the container.
100
+ * Clips to the container bounds if the image overflows.
101
+ */
102
+ const calculateNone = ({ containerSize, intrinsicSize, }) => {
103
+ // Calculate centered position (can be negative if image is larger than container)
104
+ const centeredX = containerSize.left + (containerSize.width - intrinsicSize.width) / 2;
105
+ const centeredY = containerSize.top + (containerSize.height - intrinsicSize.height) / 2;
106
+ // Calculate clipping bounds
107
+ let sourceX = 0;
108
+ let sourceY = 0;
109
+ let sourceWidth = intrinsicSize.width;
110
+ let sourceHeight = intrinsicSize.height;
111
+ let destX = centeredX;
112
+ let destY = centeredY;
113
+ let destWidth = intrinsicSize.width;
114
+ let destHeight = intrinsicSize.height;
115
+ // Clip left edge
116
+ if (destX < containerSize.left) {
117
+ const clipAmount = containerSize.left - destX;
118
+ sourceX = clipAmount;
119
+ sourceWidth -= clipAmount;
120
+ destX = containerSize.left;
121
+ destWidth -= clipAmount;
122
+ }
123
+ // Clip top edge
124
+ if (destY < containerSize.top) {
125
+ const clipAmount = containerSize.top - destY;
126
+ sourceY = clipAmount;
127
+ sourceHeight -= clipAmount;
128
+ destY = containerSize.top;
129
+ destHeight -= clipAmount;
130
+ }
131
+ // Clip right edge
132
+ const containerRight = containerSize.left + containerSize.width;
133
+ if (destX + destWidth > containerRight) {
134
+ const clipAmount = destX + destWidth - containerRight;
135
+ sourceWidth -= clipAmount;
136
+ destWidth -= clipAmount;
137
+ }
138
+ // Clip bottom edge
139
+ const containerBottom = containerSize.top + containerSize.height;
140
+ if (destY + destHeight > containerBottom) {
141
+ const clipAmount = destY + destHeight - containerBottom;
142
+ sourceHeight -= clipAmount;
143
+ destHeight -= clipAmount;
144
+ }
145
+ return {
146
+ sourceX,
147
+ sourceY,
148
+ sourceWidth,
149
+ sourceHeight,
150
+ destX,
151
+ destY,
152
+ destWidth,
153
+ destHeight,
154
+ };
155
+ };
156
+ /**
157
+ * Calculates how to draw an image based on object-fit CSS property.
158
+ *
159
+ * @param objectFit - The CSS object-fit value
160
+ * @param containerSize - The container dimensions (where the image should be drawn)
161
+ * @param intrinsicSize - The natural/intrinsic size of the image
162
+ * @returns Source and destination rectangles for drawImage
163
+ */
164
+ export const calculateObjectFit = ({ objectFit, containerSize, intrinsicSize, }) => {
165
+ switch (objectFit) {
166
+ case 'fill':
167
+ return calculateFill({ containerSize, intrinsicSize });
168
+ case 'contain':
169
+ return calculateContain({ containerSize, intrinsicSize });
170
+ case 'cover':
171
+ return calculateCover({ containerSize, intrinsicSize });
172
+ case 'none':
173
+ return calculateNone({ containerSize, intrinsicSize });
174
+ case 'scale-down': {
175
+ // scale-down behaves like contain or none, whichever results in a smaller image
176
+ const containResult = calculateContain({ containerSize, intrinsicSize });
177
+ const noneResult = calculateNone({ containerSize, intrinsicSize });
178
+ // Compare the rendered size - use whichever is smaller
179
+ const containArea = containResult.destWidth * containResult.destHeight;
180
+ const noneArea = noneResult.destWidth * noneResult.destHeight;
181
+ return containArea < noneArea ? containResult : noneResult;
182
+ }
183
+ default: {
184
+ const exhaustiveCheck = objectFit;
185
+ throw new Error(`Unknown object-fit value: ${exhaustiveCheck}`);
186
+ }
187
+ }
188
+ };
189
+ /**
190
+ * Parse an object-fit CSS value string into our ObjectFit type.
191
+ * Returns 'fill' as the default if the value is not recognized.
192
+ */
193
+ export const parseObjectFit = (value) => {
194
+ if (!value) {
195
+ return 'fill';
196
+ }
197
+ const normalized = value.trim().toLowerCase();
198
+ switch (normalized) {
199
+ case 'fill':
200
+ case 'contain':
201
+ case 'cover':
202
+ case 'none':
203
+ case 'scale-down':
204
+ return normalized;
205
+ default:
206
+ return 'fill';
207
+ }
208
+ };
@@ -0,0 +1,13 @@
1
+ import type { LogLevel } from 'remotion';
2
+ import type { InternalState } from '../internal-state';
3
+ export declare const drawBackground: ({ backgroundImage, context, rect, backgroundColor, backgroundClip, element, logLevel, internalState, computedStyle, }: {
4
+ backgroundImage: string;
5
+ context: OffscreenCanvasRenderingContext2D;
6
+ rect: DOMRect;
7
+ backgroundColor: string;
8
+ backgroundClip: string;
9
+ element: HTMLElement | SVGElement;
10
+ logLevel: LogLevel;
11
+ internalState: InternalState;
12
+ computedStyle: CSSStyleDeclaration;
13
+ }) => Promise<void>;
@@ -0,0 +1,62 @@
1
+ import { getClippedBackground } from './get-clipped-background';
2
+ import { getBoxBasedOnBackgroundClip } from './get-padding-box';
3
+ import { createCanvasGradient, parseLinearGradient, } from './parse-linear-gradient';
4
+ export const drawBackground = async ({ backgroundImage, context, rect, backgroundColor, backgroundClip, element, logLevel, internalState, computedStyle, }) => {
5
+ let contextToDraw = context;
6
+ const originalCompositeOperation = context.globalCompositeOperation;
7
+ let offsetLeft = 0;
8
+ let offsetTop = 0;
9
+ const finish = () => {
10
+ context.globalCompositeOperation = originalCompositeOperation;
11
+ if (context !== contextToDraw) {
12
+ context.drawImage(contextToDraw.canvas, offsetLeft, offsetTop, contextToDraw.canvas.width, contextToDraw.canvas.height);
13
+ }
14
+ };
15
+ const boundingRect = getBoxBasedOnBackgroundClip(rect, computedStyle, backgroundClip);
16
+ if (backgroundClip.includes('text')) {
17
+ offsetLeft = boundingRect.left;
18
+ offsetTop = boundingRect.top;
19
+ const originalBackgroundClip = element.style.backgroundClip;
20
+ const originalWebkitBackgroundClip = element.style.webkitBackgroundClip;
21
+ element.style.backgroundClip = 'initial';
22
+ element.style.webkitBackgroundClip = 'initial';
23
+ const drawn = await getClippedBackground({
24
+ element,
25
+ boundingRect,
26
+ logLevel,
27
+ internalState,
28
+ });
29
+ element.style.backgroundClip = originalBackgroundClip;
30
+ element.style.webkitBackgroundClip = originalWebkitBackgroundClip;
31
+ contextToDraw = drawn;
32
+ contextToDraw.globalCompositeOperation = 'source-in';
33
+ }
34
+ if (backgroundImage && backgroundImage !== 'none') {
35
+ const gradientInfo = parseLinearGradient(backgroundImage);
36
+ if (gradientInfo) {
37
+ const gradient = createCanvasGradient({
38
+ ctx: contextToDraw,
39
+ rect: boundingRect,
40
+ gradientInfo,
41
+ offsetLeft,
42
+ offsetTop,
43
+ });
44
+ const originalFillStyle = contextToDraw.fillStyle;
45
+ contextToDraw.fillStyle = gradient;
46
+ contextToDraw.fillRect(boundingRect.left - offsetLeft, boundingRect.top - offsetTop, boundingRect.width, boundingRect.height);
47
+ contextToDraw.fillStyle = originalFillStyle;
48
+ return finish();
49
+ }
50
+ }
51
+ // Fallback to solid background color if no gradient was drawn
52
+ if (backgroundColor &&
53
+ backgroundColor !== 'transparent' &&
54
+ !(backgroundColor.startsWith('rgba') &&
55
+ (backgroundColor.endsWith(', 0)') || backgroundColor.endsWith(',0')))) {
56
+ const originalFillStyle = contextToDraw.fillStyle;
57
+ contextToDraw.fillStyle = backgroundColor;
58
+ contextToDraw.fillRect(boundingRect.left - offsetLeft, boundingRect.top - offsetLeft, boundingRect.width, boundingRect.height);
59
+ contextToDraw.fillStyle = originalFillStyle;
60
+ }
61
+ finish();
62
+ };
@@ -8,7 +8,7 @@ interface BoxShadow {
8
8
  inset: boolean;
9
9
  }
10
10
  export declare const parseBoxShadow: (boxShadowValue: string) => BoxShadow[];
11
- export declare const setBoxShadow: ({ ctx, rect, borderRadius, computedStyle, logLevel, }: {
11
+ export declare const drawBorderRadius: ({ ctx, rect, borderRadius, computedStyle, logLevel, }: {
12
12
  ctx: OffscreenCanvasRenderingContext2D;
13
13
  rect: DOMRect;
14
14
  borderRadius: BorderRadiusCorners;
@@ -43,7 +43,7 @@ export const parseBoxShadow = (boxShadowValue) => {
43
43
  }
44
44
  return shadows;
45
45
  };
46
- export const setBoxShadow = ({ ctx, rect, borderRadius, computedStyle, logLevel, }) => {
46
+ export const drawBorderRadius = ({ ctx, rect, borderRadius, computedStyle, logLevel, }) => {
47
47
  const shadows = parseBoxShadow(computedStyle.boxShadow);
48
48
  if (shadows.length === 0) {
49
49
  return;