@remotion/web-renderer 4.0.384 → 4.0.385
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.
- package/dist/audio.d.ts +5 -1
- package/dist/audio.js +12 -19
- package/dist/border-radius.d.ts +31 -0
- package/dist/border-radius.js +152 -0
- package/dist/calculate-transforms.d.ts +2 -0
- package/dist/calculate-transforms.js +17 -0
- package/dist/composable.d.ts +2 -8
- package/dist/compose-canvas.js +28 -4
- package/dist/compose.d.ts +1 -6
- package/dist/compose.js +16 -11
- package/dist/drawing/border-radius.d.ts +31 -0
- package/dist/drawing/border-radius.js +152 -0
- package/dist/drawing/calculate-transforms.d.ts +10 -0
- package/dist/drawing/calculate-transforms.js +81 -0
- package/dist/drawing/compose-canvas.d.ts +1 -0
- package/dist/drawing/compose-canvas.js +36 -0
- package/dist/drawing/compose-svg.d.ts +1 -0
- package/dist/drawing/compose-svg.js +34 -0
- package/dist/drawing/compose.d.ts +5 -0
- package/dist/drawing/compose.js +6 -0
- package/dist/drawing/draw-border.d.ts +10 -0
- package/dist/drawing/draw-border.js +101 -0
- package/dist/drawing/draw-element-to-canvas.d.ts +4 -0
- package/dist/drawing/draw-element-to-canvas.js +72 -0
- package/dist/drawing/get-computed-style-cache.d.ts +0 -0
- package/dist/drawing/get-computed-style-cache.js +1 -0
- package/dist/drawing/opacity.d.ts +4 -0
- package/dist/drawing/opacity.js +7 -0
- package/dist/drawing/parse-transform-origin.d.ts +4 -0
- package/dist/drawing/parse-transform-origin.js +7 -0
- package/dist/drawing/transform.d.ts +4 -0
- package/dist/drawing/transform.js +6 -0
- package/dist/drawing/turn-svg-into-drawable.d.ts +1 -0
- package/dist/drawing/turn-svg-into-drawable.js +34 -0
- package/dist/esm/index.mjs +363 -79
- package/dist/find-capturable-elements.d.ts +1 -1
- package/dist/find-capturable-elements.js +20 -22
- package/dist/opacity.d.ts +4 -0
- package/dist/opacity.js +7 -0
- package/dist/render-media-on-web.js +14 -5
- package/dist/take-screenshot.js +7 -8
- package/dist/transform.d.ts +4 -0
- package/dist/transform.js +6 -0
- package/package.json +5 -5
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { setBorderRadius } from './border-radius';
|
|
2
|
+
import { calculateTransforms } from './calculate-transforms';
|
|
3
|
+
import { turnSvgIntoDrawable } from './compose-svg';
|
|
4
|
+
import { setOpacity } from './opacity';
|
|
5
|
+
import { setTransform } from './transform';
|
|
6
|
+
export const drawElementToCanvas = async (canvas, context) => {
|
|
7
|
+
const { totalMatrix, reset, dimensions, borderRadius, opacity } = calculateTransforms(canvas);
|
|
8
|
+
if (opacity === 0) {
|
|
9
|
+
reset();
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const drawable = canvas instanceof SVGSVGElement
|
|
13
|
+
? await turnSvgIntoDrawable(canvas)
|
|
14
|
+
: 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
|
+
context.drawImage(drawable, dimensions.left, dimensions.top, dimensions.width, dimensions.height);
|
|
32
|
+
finishOpacity();
|
|
33
|
+
finishBorderRadius();
|
|
34
|
+
finishTransform();
|
|
35
|
+
reset();
|
|
36
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const turnSvgIntoDrawable: (svg: SVGSVGElement) => Promise<HTMLImageElement>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const turnSvgIntoDrawable = (svg) => {
|
|
2
|
+
const originalTransform = svg.style.transform;
|
|
3
|
+
const originalTransformOrigin = svg.style.transformOrigin;
|
|
4
|
+
const originalMarginLeft = svg.style.marginLeft;
|
|
5
|
+
const originalMarginRight = svg.style.marginRight;
|
|
6
|
+
const originalMarginTop = svg.style.marginTop;
|
|
7
|
+
const originalMarginBottom = svg.style.marginBottom;
|
|
8
|
+
svg.style.transform = 'none';
|
|
9
|
+
svg.style.transformOrigin = '';
|
|
10
|
+
// Margins were already included in the positioning calculation,
|
|
11
|
+
// so we need to remove them to avoid double counting.
|
|
12
|
+
svg.style.marginLeft = '0';
|
|
13
|
+
svg.style.marginRight = '0';
|
|
14
|
+
svg.style.marginTop = '0';
|
|
15
|
+
svg.style.marginBottom = '0';
|
|
16
|
+
const svgData = new XMLSerializer().serializeToString(svg);
|
|
17
|
+
svg.style.marginLeft = originalMarginLeft;
|
|
18
|
+
svg.style.marginRight = originalMarginRight;
|
|
19
|
+
svg.style.marginTop = originalMarginTop;
|
|
20
|
+
svg.style.marginBottom = originalMarginBottom;
|
|
21
|
+
svg.style.transform = originalTransform;
|
|
22
|
+
svg.style.transformOrigin = originalTransformOrigin;
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const image = new Image();
|
|
25
|
+
const url = `data:image/svg+xml;base64,${btoa(svgData)}`;
|
|
26
|
+
image.onload = function () {
|
|
27
|
+
resolve(image);
|
|
28
|
+
};
|
|
29
|
+
image.onerror = () => {
|
|
30
|
+
reject(new Error('Failed to convert SVG to image'));
|
|
31
|
+
};
|
|
32
|
+
image.src = url;
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BorderRadiusCorners } from './border-radius';
|
|
2
|
+
export declare const drawBorder: ({ ctx, x, y, width, height, borderRadius, computedStyle, }: {
|
|
3
|
+
ctx: OffscreenCanvasRenderingContext2D;
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
borderRadius: BorderRadiusCorners;
|
|
9
|
+
computedStyle: CSSStyleDeclaration;
|
|
10
|
+
}) => void;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export const drawBorder = ({ ctx, x, y, width, height, borderRadius, computedStyle, }) => {
|
|
2
|
+
const { borderStyle, borderColor, borderWidth: computedBorderWidth, } = computedStyle;
|
|
3
|
+
// Parse border width (can be shorthand like "1px 2px 3px 4px")
|
|
4
|
+
const borderWidths = computedBorderWidth
|
|
5
|
+
.split(/\s+/)
|
|
6
|
+
.map((w) => parseFloat(w));
|
|
7
|
+
const borderTop = borderWidths[0] || 0;
|
|
8
|
+
const borderRight = borderWidths[1] || borderTop;
|
|
9
|
+
const borderBottom = borderWidths[2] || borderTop;
|
|
10
|
+
const borderLeft = borderWidths[3] || borderRight;
|
|
11
|
+
// Check if we have a visible border
|
|
12
|
+
const hasBorder = borderStyle &&
|
|
13
|
+
borderStyle !== 'none' &&
|
|
14
|
+
borderStyle !== 'hidden' &&
|
|
15
|
+
(borderTop > 0 || borderRight > 0 || borderBottom > 0 || borderLeft > 0);
|
|
16
|
+
if (!hasBorder) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const originalStrokeStyle = ctx.strokeStyle;
|
|
20
|
+
const originalLineWidth = ctx.lineWidth;
|
|
21
|
+
const originalLineDash = ctx.getLineDash();
|
|
22
|
+
ctx.strokeStyle = borderColor;
|
|
23
|
+
// Set line dash based on border style
|
|
24
|
+
if (borderStyle === 'dashed') {
|
|
25
|
+
const max = Math.max(borderTop, borderRight, borderBottom, borderLeft);
|
|
26
|
+
ctx.setLineDash([max * 2, max]);
|
|
27
|
+
}
|
|
28
|
+
else if (borderStyle === 'dotted') {
|
|
29
|
+
ctx.setLineDash([
|
|
30
|
+
Math.max(borderTop, borderRight, borderBottom, borderLeft),
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
ctx.setLineDash([]);
|
|
35
|
+
}
|
|
36
|
+
// For simplicity, use the maximum border width if they differ
|
|
37
|
+
// A full implementation would draw each side separately
|
|
38
|
+
const maxBorderWidth = Math.max(borderTop, borderRight, borderBottom, borderLeft);
|
|
39
|
+
// Create path for border (inset by half the border width to draw inside)
|
|
40
|
+
ctx.beginPath();
|
|
41
|
+
const borderX = x + maxBorderWidth / 2;
|
|
42
|
+
const borderY = y + maxBorderWidth / 2;
|
|
43
|
+
const borderWidth = width - maxBorderWidth;
|
|
44
|
+
const borderHeight = height - maxBorderWidth;
|
|
45
|
+
// Account for border radius, adjusted for the border width
|
|
46
|
+
const adjustedBorderRadius = {
|
|
47
|
+
topLeft: {
|
|
48
|
+
horizontal: Math.max(0, borderRadius.topLeft.horizontal - maxBorderWidth / 2),
|
|
49
|
+
vertical: Math.max(0, borderRadius.topLeft.vertical - maxBorderWidth / 2),
|
|
50
|
+
},
|
|
51
|
+
topRight: {
|
|
52
|
+
horizontal: Math.max(0, borderRadius.topRight.horizontal - maxBorderWidth / 2),
|
|
53
|
+
vertical: Math.max(0, borderRadius.topRight.vertical - maxBorderWidth / 2),
|
|
54
|
+
},
|
|
55
|
+
bottomRight: {
|
|
56
|
+
horizontal: Math.max(0, borderRadius.bottomRight.horizontal - maxBorderWidth / 2),
|
|
57
|
+
vertical: Math.max(0, borderRadius.bottomRight.vertical - maxBorderWidth / 2),
|
|
58
|
+
},
|
|
59
|
+
bottomLeft: {
|
|
60
|
+
horizontal: Math.max(0, borderRadius.bottomLeft.horizontal - maxBorderWidth / 2),
|
|
61
|
+
vertical: Math.max(0, borderRadius.bottomLeft.vertical - maxBorderWidth / 2),
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
// Draw path with border radius
|
|
65
|
+
ctx.moveTo(borderX + adjustedBorderRadius.topLeft.horizontal, borderY);
|
|
66
|
+
// Top edge
|
|
67
|
+
ctx.lineTo(borderX + borderWidth - adjustedBorderRadius.topRight.horizontal, borderY);
|
|
68
|
+
// Top-right corner
|
|
69
|
+
if (adjustedBorderRadius.topRight.horizontal > 0 ||
|
|
70
|
+
adjustedBorderRadius.topRight.vertical > 0) {
|
|
71
|
+
ctx.ellipse(borderX + borderWidth - adjustedBorderRadius.topRight.horizontal, borderY + adjustedBorderRadius.topRight.vertical, adjustedBorderRadius.topRight.horizontal, adjustedBorderRadius.topRight.vertical, 0, -Math.PI / 2, 0);
|
|
72
|
+
}
|
|
73
|
+
// Right edge
|
|
74
|
+
ctx.lineTo(borderX + borderWidth, borderY + borderHeight - adjustedBorderRadius.bottomRight.vertical);
|
|
75
|
+
// Bottom-right corner
|
|
76
|
+
if (adjustedBorderRadius.bottomRight.horizontal > 0 ||
|
|
77
|
+
adjustedBorderRadius.bottomRight.vertical > 0) {
|
|
78
|
+
ctx.ellipse(borderX + borderWidth - adjustedBorderRadius.bottomRight.horizontal, borderY + borderHeight - adjustedBorderRadius.bottomRight.vertical, adjustedBorderRadius.bottomRight.horizontal, adjustedBorderRadius.bottomRight.vertical, 0, 0, Math.PI / 2);
|
|
79
|
+
}
|
|
80
|
+
// Bottom edge
|
|
81
|
+
ctx.lineTo(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY + borderHeight);
|
|
82
|
+
// Bottom-left corner
|
|
83
|
+
if (adjustedBorderRadius.bottomLeft.horizontal > 0 ||
|
|
84
|
+
adjustedBorderRadius.bottomLeft.vertical > 0) {
|
|
85
|
+
ctx.ellipse(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY + borderHeight - adjustedBorderRadius.bottomLeft.vertical, adjustedBorderRadius.bottomLeft.horizontal, adjustedBorderRadius.bottomLeft.vertical, 0, Math.PI / 2, Math.PI);
|
|
86
|
+
}
|
|
87
|
+
// Left edge
|
|
88
|
+
ctx.lineTo(borderX, borderY + adjustedBorderRadius.topLeft.vertical);
|
|
89
|
+
// Top-left corner
|
|
90
|
+
if (adjustedBorderRadius.topLeft.horizontal > 0 ||
|
|
91
|
+
adjustedBorderRadius.topLeft.vertical > 0) {
|
|
92
|
+
ctx.ellipse(borderX + adjustedBorderRadius.topLeft.horizontal, borderY + adjustedBorderRadius.topLeft.vertical, adjustedBorderRadius.topLeft.horizontal, adjustedBorderRadius.topLeft.vertical, 0, Math.PI, (Math.PI * 3) / 2);
|
|
93
|
+
}
|
|
94
|
+
ctx.closePath();
|
|
95
|
+
ctx.lineWidth = maxBorderWidth;
|
|
96
|
+
ctx.stroke();
|
|
97
|
+
// Restore original values
|
|
98
|
+
ctx.strokeStyle = originalStrokeStyle;
|
|
99
|
+
ctx.lineWidth = originalLineWidth;
|
|
100
|
+
ctx.setLineDash(originalLineDash);
|
|
101
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { parseBorderRadius, setBorderRadius } from './border-radius';
|
|
2
|
+
import { calculateTransforms } from './calculate-transforms';
|
|
3
|
+
import { drawBorder } from './draw-border';
|
|
4
|
+
import { setOpacity } from './opacity';
|
|
5
|
+
import { setTransform } from './transform';
|
|
6
|
+
import { turnSvgIntoDrawable } from './turn-svg-into-drawable';
|
|
7
|
+
export const drawElementToCanvas = async ({ element, context, }) => {
|
|
8
|
+
const { totalMatrix, reset, dimensions, opacity } = calculateTransforms(element);
|
|
9
|
+
if (opacity === 0) {
|
|
10
|
+
reset();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (dimensions.width <= 0 || dimensions.height <= 0) {
|
|
14
|
+
reset();
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const computedStyle = getComputedStyle(element);
|
|
18
|
+
const background = computedStyle.backgroundColor;
|
|
19
|
+
const borderRadius = parseBorderRadius({
|
|
20
|
+
borderRadius: computedStyle.borderRadius,
|
|
21
|
+
width: dimensions.width,
|
|
22
|
+
height: dimensions.height,
|
|
23
|
+
});
|
|
24
|
+
const finishTransform = setTransform({
|
|
25
|
+
ctx: context,
|
|
26
|
+
transform: totalMatrix,
|
|
27
|
+
});
|
|
28
|
+
const finishBorderRadius = setBorderRadius({
|
|
29
|
+
ctx: context,
|
|
30
|
+
x: dimensions.left,
|
|
31
|
+
y: dimensions.top,
|
|
32
|
+
width: dimensions.width,
|
|
33
|
+
height: dimensions.height,
|
|
34
|
+
borderRadius,
|
|
35
|
+
});
|
|
36
|
+
const finishOpacity = setOpacity({
|
|
37
|
+
ctx: context,
|
|
38
|
+
opacity,
|
|
39
|
+
});
|
|
40
|
+
const drawable = element instanceof SVGSVGElement
|
|
41
|
+
? await turnSvgIntoDrawable(element)
|
|
42
|
+
: element instanceof HTMLImageElement
|
|
43
|
+
? element
|
|
44
|
+
: element instanceof HTMLCanvasElement
|
|
45
|
+
? element
|
|
46
|
+
: null;
|
|
47
|
+
if (background &&
|
|
48
|
+
background !== 'transparent' &&
|
|
49
|
+
!(background.startsWith('rgba') &&
|
|
50
|
+
(background.endsWith(', 0)') || background.endsWith(',0')))) {
|
|
51
|
+
const originalFillStyle = context.fillStyle;
|
|
52
|
+
context.fillStyle = background;
|
|
53
|
+
context.fillRect(dimensions.left, dimensions.top, dimensions.width, dimensions.height);
|
|
54
|
+
context.fillStyle = originalFillStyle;
|
|
55
|
+
}
|
|
56
|
+
if (drawable) {
|
|
57
|
+
context.drawImage(drawable, dimensions.left, dimensions.top, dimensions.width, dimensions.height);
|
|
58
|
+
}
|
|
59
|
+
drawBorder({
|
|
60
|
+
ctx: context,
|
|
61
|
+
x: dimensions.left,
|
|
62
|
+
y: dimensions.top,
|
|
63
|
+
width: dimensions.width,
|
|
64
|
+
height: dimensions.height,
|
|
65
|
+
borderRadius,
|
|
66
|
+
computedStyle,
|
|
67
|
+
});
|
|
68
|
+
finishOpacity();
|
|
69
|
+
finishBorderRadius();
|
|
70
|
+
finishTransform();
|
|
71
|
+
reset();
|
|
72
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const turnSvgIntoDrawable: (svg: SVGSVGElement) => Promise<HTMLImageElement>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const turnSvgIntoDrawable = (svg) => {
|
|
2
|
+
const originalTransform = svg.style.transform;
|
|
3
|
+
const originalTransformOrigin = svg.style.transformOrigin;
|
|
4
|
+
const originalMarginLeft = svg.style.marginLeft;
|
|
5
|
+
const originalMarginRight = svg.style.marginRight;
|
|
6
|
+
const originalMarginTop = svg.style.marginTop;
|
|
7
|
+
const originalMarginBottom = svg.style.marginBottom;
|
|
8
|
+
svg.style.transform = 'none';
|
|
9
|
+
svg.style.transformOrigin = '';
|
|
10
|
+
// Margins were already included in the positioning calculation,
|
|
11
|
+
// so we need to remove them to avoid double counting.
|
|
12
|
+
svg.style.marginLeft = '0';
|
|
13
|
+
svg.style.marginRight = '0';
|
|
14
|
+
svg.style.marginTop = '0';
|
|
15
|
+
svg.style.marginBottom = '0';
|
|
16
|
+
const svgData = new XMLSerializer().serializeToString(svg);
|
|
17
|
+
svg.style.marginLeft = originalMarginLeft;
|
|
18
|
+
svg.style.marginRight = originalMarginRight;
|
|
19
|
+
svg.style.marginTop = originalMarginTop;
|
|
20
|
+
svg.style.marginBottom = originalMarginBottom;
|
|
21
|
+
svg.style.transform = originalTransform;
|
|
22
|
+
svg.style.transformOrigin = originalTransformOrigin;
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const image = new Image();
|
|
25
|
+
const url = `data:image/svg+xml;base64,${btoa(svgData)}`;
|
|
26
|
+
image.onload = function () {
|
|
27
|
+
resolve(image);
|
|
28
|
+
};
|
|
29
|
+
image.onerror = () => {
|
|
30
|
+
reject(new Error('Failed to convert SVG to image'));
|
|
31
|
+
};
|
|
32
|
+
image.src = url;
|
|
33
|
+
});
|
|
34
|
+
};
|