@remotion/web-renderer 4.0.394 → 4.0.395
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/compose.d.ts +3 -1
- package/dist/compose.js +5 -3
- package/dist/drawing/draw-border.js +307 -55
- package/dist/drawing/draw-element-to-canvas.d.ts +3 -1
- package/dist/drawing/draw-element-to-canvas.js +16 -5
- package/dist/drawing/text/draw-text.js +6 -8
- package/dist/drawing/text/get-base-height.d.ts +1 -0
- package/dist/drawing/text/get-base-height.js +13 -0
- package/dist/drawing/text/handle-text-node.d.ts +3 -1
- package/dist/drawing/text/handle-text-node.js +2 -1
- package/dist/drawing/transform-in-3d.js +7 -1
- package/dist/drawing/turn-svg-into-drawable.js +7 -0
- package/dist/esm/index.mjs +494 -85
- package/dist/get-biggest-bounding-client-rect.js +4 -1
- package/dist/render-media-on-web.d.ts +1 -0
- package/dist/render-media-on-web.js +33 -15
- package/dist/render-still-on-web.d.ts +1 -0
- package/dist/render-still-on-web.js +25 -7
- package/dist/send-telemetry-event.js +1 -1
- package/dist/take-screenshot.d.ts +5 -2
- package/dist/take-screenshot.js +4 -4
- package/package.json +7 -6
package/dist/compose.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import type { LogLevel } from 'remotion';
|
|
2
|
+
export declare const compose: ({ element, context, offsetLeft, offsetTop, logLevel, }: {
|
|
2
3
|
element: HTMLElement | SVGElement;
|
|
3
4
|
context: OffscreenCanvasRenderingContext2D;
|
|
4
5
|
offsetLeft: number;
|
|
5
6
|
offsetTop: number;
|
|
7
|
+
logLevel: LogLevel;
|
|
6
8
|
}) => Promise<void>;
|
package/dist/compose.js
CHANGED
|
@@ -2,7 +2,7 @@ import { drawDomElement } from './drawing/draw-dom-element';
|
|
|
2
2
|
import { drawElementToCanvas } from './drawing/draw-element-to-canvas';
|
|
3
3
|
import { handleTextNode } from './drawing/text/handle-text-node';
|
|
4
4
|
import { skipToNextNonDescendant } from './walk-tree';
|
|
5
|
-
const walkOverNode = ({ node, context, offsetLeft, offsetTop, }) => {
|
|
5
|
+
const walkOverNode = ({ node, context, offsetLeft, offsetTop, logLevel, }) => {
|
|
6
6
|
if (node instanceof HTMLElement || node instanceof SVGElement) {
|
|
7
7
|
return drawElementToCanvas({
|
|
8
8
|
element: node,
|
|
@@ -10,14 +10,15 @@ const walkOverNode = ({ node, context, offsetLeft, offsetTop, }) => {
|
|
|
10
10
|
draw: drawDomElement(node),
|
|
11
11
|
offsetLeft,
|
|
12
12
|
offsetTop,
|
|
13
|
+
logLevel,
|
|
13
14
|
});
|
|
14
15
|
}
|
|
15
16
|
if (node instanceof Text) {
|
|
16
|
-
return handleTextNode({ node, context, offsetLeft, offsetTop });
|
|
17
|
+
return handleTextNode({ node, context, offsetLeft, offsetTop, logLevel });
|
|
17
18
|
}
|
|
18
19
|
throw new Error('Unknown node type');
|
|
19
20
|
};
|
|
20
|
-
export const compose = async ({ element, context, offsetLeft, offsetTop, }) => {
|
|
21
|
+
export const compose = async ({ element, context, offsetLeft, offsetTop, logLevel, }) => {
|
|
21
22
|
const treeWalker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, (node) => {
|
|
22
23
|
if (node instanceof Element) {
|
|
23
24
|
// SVG does have children, but we process SVG elements in its
|
|
@@ -38,6 +39,7 @@ export const compose = async ({ element, context, offsetLeft, offsetTop, }) => {
|
|
|
38
39
|
context,
|
|
39
40
|
offsetLeft,
|
|
40
41
|
offsetTop,
|
|
42
|
+
logLevel,
|
|
41
43
|
});
|
|
42
44
|
if (val === 'skip-children') {
|
|
43
45
|
if (!skipToNextNonDescendant(treeWalker)) {
|
|
@@ -1,88 +1,205 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
const parseBorderWidth = (value) => {
|
|
2
|
+
return parseFloat(value) || 0;
|
|
3
|
+
};
|
|
4
|
+
const getBorderSideProperties = (computedStyle) => {
|
|
5
|
+
// Parse individual border properties for each side
|
|
6
|
+
// This handles both shorthand (border: "1px solid red") and longhand (border-top-width: "1px") properties
|
|
7
|
+
return {
|
|
8
|
+
top: {
|
|
9
|
+
width: parseBorderWidth(computedStyle.borderTopWidth),
|
|
10
|
+
color: computedStyle.borderTopColor || computedStyle.borderColor || 'black',
|
|
11
|
+
style: computedStyle.borderTopStyle || computedStyle.borderStyle || 'solid',
|
|
12
|
+
},
|
|
13
|
+
right: {
|
|
14
|
+
width: parseBorderWidth(computedStyle.borderRightWidth),
|
|
15
|
+
color: computedStyle.borderRightColor || computedStyle.borderColor || 'black',
|
|
16
|
+
style: computedStyle.borderRightStyle || computedStyle.borderStyle || 'solid',
|
|
17
|
+
},
|
|
18
|
+
bottom: {
|
|
19
|
+
width: parseBorderWidth(computedStyle.borderBottomWidth),
|
|
20
|
+
color: computedStyle.borderBottomColor || computedStyle.borderColor || 'black',
|
|
21
|
+
style: computedStyle.borderBottomStyle || computedStyle.borderStyle || 'solid',
|
|
22
|
+
},
|
|
23
|
+
left: {
|
|
24
|
+
width: parseBorderWidth(computedStyle.borderLeftWidth),
|
|
25
|
+
color: computedStyle.borderLeftColor || computedStyle.borderColor || 'black',
|
|
26
|
+
style: computedStyle.borderLeftStyle || computedStyle.borderStyle || 'solid',
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const getLineDashPattern = (style, width) => {
|
|
31
|
+
if (style === 'dashed') {
|
|
32
|
+
return [width * 2, width];
|
|
33
|
+
}
|
|
34
|
+
if (style === 'dotted') {
|
|
35
|
+
return [width, width];
|
|
36
|
+
}
|
|
37
|
+
return [];
|
|
38
|
+
};
|
|
39
|
+
const drawBorderSide = ({ ctx, side, x, y, width, height, borderRadius, borderProperties, }) => {
|
|
40
|
+
const { width: borderWidth, color, style } = borderProperties;
|
|
41
|
+
if (borderWidth <= 0 || style === 'none' || style === 'hidden') {
|
|
17
42
|
return;
|
|
18
43
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
ctx.
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
44
|
+
ctx.beginPath();
|
|
45
|
+
ctx.strokeStyle = color;
|
|
46
|
+
ctx.lineWidth = borderWidth;
|
|
47
|
+
ctx.setLineDash(getLineDashPattern(style, borderWidth));
|
|
48
|
+
const halfWidth = borderWidth / 2;
|
|
49
|
+
if (side === 'top') {
|
|
50
|
+
// Start point (accounting for left border and top-left radius)
|
|
51
|
+
const startX = x + borderRadius.topLeft.horizontal;
|
|
52
|
+
const startY = y + halfWidth;
|
|
53
|
+
// End point (accounting for top-right radius)
|
|
54
|
+
const endX = x + width - borderRadius.topRight.horizontal;
|
|
55
|
+
const endY = y + halfWidth;
|
|
56
|
+
ctx.moveTo(startX, startY);
|
|
57
|
+
ctx.lineTo(endX, endY);
|
|
58
|
+
}
|
|
59
|
+
else if (side === 'right') {
|
|
60
|
+
// Start point (accounting for top border and top-right radius)
|
|
61
|
+
const startX = x + width - halfWidth;
|
|
62
|
+
const startY = y + borderRadius.topRight.vertical;
|
|
63
|
+
// End point (accounting for bottom-right radius)
|
|
64
|
+
const endX = x + width - halfWidth;
|
|
65
|
+
const endY = y + height - borderRadius.bottomRight.vertical;
|
|
66
|
+
ctx.moveTo(startX, startY);
|
|
67
|
+
ctx.lineTo(endX, endY);
|
|
68
|
+
}
|
|
69
|
+
else if (side === 'bottom') {
|
|
70
|
+
// Start point (accounting for bottom-left radius)
|
|
71
|
+
const startX = x + borderRadius.bottomLeft.horizontal;
|
|
72
|
+
const startY = y + height - halfWidth;
|
|
73
|
+
// End point (accounting for right border and bottom-right radius)
|
|
74
|
+
const endX = x + width - borderRadius.bottomRight.horizontal;
|
|
75
|
+
const endY = y + height - halfWidth;
|
|
76
|
+
ctx.moveTo(startX, startY);
|
|
77
|
+
ctx.lineTo(endX, endY);
|
|
78
|
+
}
|
|
79
|
+
else if (side === 'left') {
|
|
80
|
+
// Start point (accounting for top-left radius)
|
|
81
|
+
const startX = x + halfWidth;
|
|
82
|
+
const startY = y + borderRadius.topLeft.vertical;
|
|
83
|
+
// End point (accounting for bottom border and bottom-left radius)
|
|
84
|
+
const endX = x + halfWidth;
|
|
85
|
+
const endY = y + height - borderRadius.bottomLeft.vertical;
|
|
86
|
+
ctx.moveTo(startX, startY);
|
|
87
|
+
ctx.lineTo(endX, endY);
|
|
27
88
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
89
|
+
ctx.stroke();
|
|
90
|
+
};
|
|
91
|
+
const drawCorner = ({ ctx, corner, x, y, width, height, borderRadius, topBorder, rightBorder, bottomBorder, leftBorder, }) => {
|
|
92
|
+
const radius = borderRadius[corner];
|
|
93
|
+
if (radius.horizontal <= 0 && radius.vertical <= 0) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
let border1;
|
|
97
|
+
let border2;
|
|
98
|
+
let centerX;
|
|
99
|
+
let centerY;
|
|
100
|
+
let startAngle;
|
|
101
|
+
let endAngle;
|
|
102
|
+
if (corner === 'topLeft') {
|
|
103
|
+
border1 = leftBorder;
|
|
104
|
+
border2 = topBorder;
|
|
105
|
+
centerX = x + radius.horizontal;
|
|
106
|
+
centerY = y + radius.vertical;
|
|
107
|
+
startAngle = Math.PI;
|
|
108
|
+
endAngle = (Math.PI * 3) / 2;
|
|
109
|
+
}
|
|
110
|
+
else if (corner === 'topRight') {
|
|
111
|
+
border1 = topBorder;
|
|
112
|
+
border2 = rightBorder;
|
|
113
|
+
centerX = x + width - radius.horizontal;
|
|
114
|
+
centerY = y + radius.vertical;
|
|
115
|
+
startAngle = -Math.PI / 2;
|
|
116
|
+
endAngle = 0;
|
|
117
|
+
}
|
|
118
|
+
else if (corner === 'bottomRight') {
|
|
119
|
+
border1 = rightBorder;
|
|
120
|
+
border2 = bottomBorder;
|
|
121
|
+
centerX = x + width - radius.horizontal;
|
|
122
|
+
centerY = y + height - radius.vertical;
|
|
123
|
+
startAngle = 0;
|
|
124
|
+
endAngle = Math.PI / 2;
|
|
32
125
|
}
|
|
33
126
|
else {
|
|
34
|
-
|
|
127
|
+
// bottomLeft
|
|
128
|
+
border1 = bottomBorder;
|
|
129
|
+
border2 = leftBorder;
|
|
130
|
+
centerX = x + radius.horizontal;
|
|
131
|
+
centerY = y + height - radius.vertical;
|
|
132
|
+
startAngle = Math.PI / 2;
|
|
133
|
+
endAngle = Math.PI;
|
|
134
|
+
}
|
|
135
|
+
// Draw corner arc - use the average of the two adjacent borders
|
|
136
|
+
// In a more sophisticated implementation, we could blend the two borders
|
|
137
|
+
const avgWidth = (border1.width + border2.width) / 2;
|
|
138
|
+
const useColor = border1.width >= border2.width ? border1.color : border2.color;
|
|
139
|
+
const useStyle = border1.width >= border2.width ? border1.style : border2.style;
|
|
140
|
+
if (avgWidth > 0 && useStyle !== 'none' && useStyle !== 'hidden') {
|
|
141
|
+
ctx.beginPath();
|
|
142
|
+
ctx.strokeStyle = useColor;
|
|
143
|
+
ctx.lineWidth = avgWidth;
|
|
144
|
+
ctx.setLineDash(getLineDashPattern(useStyle, avgWidth));
|
|
145
|
+
// Adjust radius for the border width
|
|
146
|
+
const adjustedRadiusH = Math.max(0, radius.horizontal - avgWidth / 2);
|
|
147
|
+
const adjustedRadiusV = Math.max(0, radius.vertical - avgWidth / 2);
|
|
148
|
+
ctx.ellipse(centerX, centerY, adjustedRadiusH, adjustedRadiusV, 0, startAngle, endAngle);
|
|
149
|
+
ctx.stroke();
|
|
35
150
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const maxBorderWidth = Math.max(borderTop, borderRight, borderBottom, borderLeft);
|
|
39
|
-
// Create path for border (inset by half the border width to draw inside)
|
|
151
|
+
};
|
|
152
|
+
const drawUniformBorder = ({ ctx, x, y, width, height, borderRadius, borderWidth, borderColor, borderStyle, }) => {
|
|
40
153
|
ctx.beginPath();
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
154
|
+
ctx.strokeStyle = borderColor;
|
|
155
|
+
ctx.lineWidth = borderWidth;
|
|
156
|
+
ctx.setLineDash(getLineDashPattern(borderStyle, borderWidth));
|
|
157
|
+
const halfWidth = borderWidth / 2;
|
|
158
|
+
const borderX = x + halfWidth;
|
|
159
|
+
const borderY = y + halfWidth;
|
|
160
|
+
const borderW = width - borderWidth;
|
|
161
|
+
const borderH = height - borderWidth;
|
|
162
|
+
// Adjust border radius for the border width
|
|
46
163
|
const adjustedBorderRadius = {
|
|
47
164
|
topLeft: {
|
|
48
|
-
horizontal: Math.max(0, borderRadius.topLeft.horizontal -
|
|
49
|
-
vertical: Math.max(0, borderRadius.topLeft.vertical -
|
|
165
|
+
horizontal: Math.max(0, borderRadius.topLeft.horizontal - halfWidth),
|
|
166
|
+
vertical: Math.max(0, borderRadius.topLeft.vertical - halfWidth),
|
|
50
167
|
},
|
|
51
168
|
topRight: {
|
|
52
|
-
horizontal: Math.max(0, borderRadius.topRight.horizontal -
|
|
53
|
-
vertical: Math.max(0, borderRadius.topRight.vertical -
|
|
169
|
+
horizontal: Math.max(0, borderRadius.topRight.horizontal - halfWidth),
|
|
170
|
+
vertical: Math.max(0, borderRadius.topRight.vertical - halfWidth),
|
|
54
171
|
},
|
|
55
172
|
bottomRight: {
|
|
56
|
-
horizontal: Math.max(0, borderRadius.bottomRight.horizontal -
|
|
57
|
-
vertical: Math.max(0, borderRadius.bottomRight.vertical -
|
|
173
|
+
horizontal: Math.max(0, borderRadius.bottomRight.horizontal - halfWidth),
|
|
174
|
+
vertical: Math.max(0, borderRadius.bottomRight.vertical - halfWidth),
|
|
58
175
|
},
|
|
59
176
|
bottomLeft: {
|
|
60
|
-
horizontal: Math.max(0, borderRadius.bottomLeft.horizontal -
|
|
61
|
-
vertical: Math.max(0, borderRadius.bottomLeft.vertical -
|
|
177
|
+
horizontal: Math.max(0, borderRadius.bottomLeft.horizontal - halfWidth),
|
|
178
|
+
vertical: Math.max(0, borderRadius.bottomLeft.vertical - halfWidth),
|
|
62
179
|
},
|
|
63
180
|
};
|
|
64
|
-
// Draw path with border radius
|
|
181
|
+
// Draw continuous path with border radius
|
|
65
182
|
ctx.moveTo(borderX + adjustedBorderRadius.topLeft.horizontal, borderY);
|
|
66
183
|
// Top edge
|
|
67
|
-
ctx.lineTo(borderX +
|
|
184
|
+
ctx.lineTo(borderX + borderW - adjustedBorderRadius.topRight.horizontal, borderY);
|
|
68
185
|
// Top-right corner
|
|
69
186
|
if (adjustedBorderRadius.topRight.horizontal > 0 ||
|
|
70
187
|
adjustedBorderRadius.topRight.vertical > 0) {
|
|
71
|
-
ctx.ellipse(borderX +
|
|
188
|
+
ctx.ellipse(borderX + borderW - adjustedBorderRadius.topRight.horizontal, borderY + adjustedBorderRadius.topRight.vertical, adjustedBorderRadius.topRight.horizontal, adjustedBorderRadius.topRight.vertical, 0, -Math.PI / 2, 0);
|
|
72
189
|
}
|
|
73
190
|
// Right edge
|
|
74
|
-
ctx.lineTo(borderX +
|
|
191
|
+
ctx.lineTo(borderX + borderW, borderY + borderH - adjustedBorderRadius.bottomRight.vertical);
|
|
75
192
|
// Bottom-right corner
|
|
76
193
|
if (adjustedBorderRadius.bottomRight.horizontal > 0 ||
|
|
77
194
|
adjustedBorderRadius.bottomRight.vertical > 0) {
|
|
78
|
-
ctx.ellipse(borderX +
|
|
195
|
+
ctx.ellipse(borderX + borderW - adjustedBorderRadius.bottomRight.horizontal, borderY + borderH - adjustedBorderRadius.bottomRight.vertical, adjustedBorderRadius.bottomRight.horizontal, adjustedBorderRadius.bottomRight.vertical, 0, 0, Math.PI / 2);
|
|
79
196
|
}
|
|
80
197
|
// Bottom edge
|
|
81
|
-
ctx.lineTo(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY +
|
|
198
|
+
ctx.lineTo(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY + borderH);
|
|
82
199
|
// Bottom-left corner
|
|
83
200
|
if (adjustedBorderRadius.bottomLeft.horizontal > 0 ||
|
|
84
201
|
adjustedBorderRadius.bottomLeft.vertical > 0) {
|
|
85
|
-
ctx.ellipse(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY +
|
|
202
|
+
ctx.ellipse(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY + borderH - adjustedBorderRadius.bottomLeft.vertical, adjustedBorderRadius.bottomLeft.horizontal, adjustedBorderRadius.bottomLeft.vertical, 0, Math.PI / 2, Math.PI);
|
|
86
203
|
}
|
|
87
204
|
// Left edge
|
|
88
205
|
ctx.lineTo(borderX, borderY + adjustedBorderRadius.topLeft.vertical);
|
|
@@ -92,9 +209,144 @@ export const drawBorder = ({ ctx, x, y, width, height, borderRadius, computedSty
|
|
|
92
209
|
ctx.ellipse(borderX + adjustedBorderRadius.topLeft.horizontal, borderY + adjustedBorderRadius.topLeft.vertical, adjustedBorderRadius.topLeft.horizontal, adjustedBorderRadius.topLeft.vertical, 0, Math.PI, (Math.PI * 3) / 2);
|
|
93
210
|
}
|
|
94
211
|
ctx.closePath();
|
|
95
|
-
ctx.lineWidth = maxBorderWidth;
|
|
96
212
|
ctx.stroke();
|
|
97
|
-
|
|
213
|
+
};
|
|
214
|
+
export const drawBorder = ({ ctx, x, y, width, height, borderRadius, computedStyle, }) => {
|
|
215
|
+
const borders = getBorderSideProperties(computedStyle);
|
|
216
|
+
// Check if we have any visible border
|
|
217
|
+
const hasBorder = borders.top.width > 0 ||
|
|
218
|
+
borders.right.width > 0 ||
|
|
219
|
+
borders.bottom.width > 0 ||
|
|
220
|
+
borders.left.width > 0;
|
|
221
|
+
if (!hasBorder) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
// Save original canvas state
|
|
225
|
+
const originalStrokeStyle = ctx.strokeStyle;
|
|
226
|
+
const originalLineWidth = ctx.lineWidth;
|
|
227
|
+
const originalLineDash = ctx.getLineDash();
|
|
228
|
+
// Check if all borders are uniform (same width, color, and style)
|
|
229
|
+
const allSidesEqual = borders.top.width === borders.right.width &&
|
|
230
|
+
borders.top.width === borders.bottom.width &&
|
|
231
|
+
borders.top.width === borders.left.width &&
|
|
232
|
+
borders.top.color === borders.right.color &&
|
|
233
|
+
borders.top.color === borders.bottom.color &&
|
|
234
|
+
borders.top.color === borders.left.color &&
|
|
235
|
+
borders.top.style === borders.right.style &&
|
|
236
|
+
borders.top.style === borders.bottom.style &&
|
|
237
|
+
borders.top.style === borders.left.style &&
|
|
238
|
+
borders.top.width > 0;
|
|
239
|
+
if (allSidesEqual) {
|
|
240
|
+
// Draw as a single continuous border for continuous dashing
|
|
241
|
+
drawUniformBorder({
|
|
242
|
+
ctx,
|
|
243
|
+
x,
|
|
244
|
+
y,
|
|
245
|
+
width,
|
|
246
|
+
height,
|
|
247
|
+
borderRadius,
|
|
248
|
+
borderWidth: borders.top.width,
|
|
249
|
+
borderColor: borders.top.color,
|
|
250
|
+
borderStyle: borders.top.style,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
// Draw corners first (they go underneath the straight edges)
|
|
255
|
+
drawCorner({
|
|
256
|
+
ctx,
|
|
257
|
+
corner: 'topLeft',
|
|
258
|
+
x,
|
|
259
|
+
y,
|
|
260
|
+
width,
|
|
261
|
+
height,
|
|
262
|
+
borderRadius,
|
|
263
|
+
topBorder: borders.top,
|
|
264
|
+
rightBorder: borders.right,
|
|
265
|
+
bottomBorder: borders.bottom,
|
|
266
|
+
leftBorder: borders.left,
|
|
267
|
+
});
|
|
268
|
+
drawCorner({
|
|
269
|
+
ctx,
|
|
270
|
+
corner: 'topRight',
|
|
271
|
+
x,
|
|
272
|
+
y,
|
|
273
|
+
width,
|
|
274
|
+
height,
|
|
275
|
+
borderRadius,
|
|
276
|
+
topBorder: borders.top,
|
|
277
|
+
rightBorder: borders.right,
|
|
278
|
+
bottomBorder: borders.bottom,
|
|
279
|
+
leftBorder: borders.left,
|
|
280
|
+
});
|
|
281
|
+
drawCorner({
|
|
282
|
+
ctx,
|
|
283
|
+
corner: 'bottomRight',
|
|
284
|
+
x,
|
|
285
|
+
y,
|
|
286
|
+
width,
|
|
287
|
+
height,
|
|
288
|
+
borderRadius,
|
|
289
|
+
topBorder: borders.top,
|
|
290
|
+
rightBorder: borders.right,
|
|
291
|
+
bottomBorder: borders.bottom,
|
|
292
|
+
leftBorder: borders.left,
|
|
293
|
+
});
|
|
294
|
+
drawCorner({
|
|
295
|
+
ctx,
|
|
296
|
+
corner: 'bottomLeft',
|
|
297
|
+
x,
|
|
298
|
+
y,
|
|
299
|
+
width,
|
|
300
|
+
height,
|
|
301
|
+
borderRadius,
|
|
302
|
+
topBorder: borders.top,
|
|
303
|
+
rightBorder: borders.right,
|
|
304
|
+
bottomBorder: borders.bottom,
|
|
305
|
+
leftBorder: borders.left,
|
|
306
|
+
});
|
|
307
|
+
// Draw each border side
|
|
308
|
+
drawBorderSide({
|
|
309
|
+
ctx,
|
|
310
|
+
side: 'top',
|
|
311
|
+
x,
|
|
312
|
+
y,
|
|
313
|
+
width,
|
|
314
|
+
height,
|
|
315
|
+
borderRadius,
|
|
316
|
+
borderProperties: borders.top,
|
|
317
|
+
});
|
|
318
|
+
drawBorderSide({
|
|
319
|
+
ctx,
|
|
320
|
+
side: 'right',
|
|
321
|
+
x,
|
|
322
|
+
y,
|
|
323
|
+
width,
|
|
324
|
+
height,
|
|
325
|
+
borderRadius,
|
|
326
|
+
borderProperties: borders.right,
|
|
327
|
+
});
|
|
328
|
+
drawBorderSide({
|
|
329
|
+
ctx,
|
|
330
|
+
side: 'bottom',
|
|
331
|
+
x,
|
|
332
|
+
y,
|
|
333
|
+
width,
|
|
334
|
+
height,
|
|
335
|
+
borderRadius,
|
|
336
|
+
borderProperties: borders.bottom,
|
|
337
|
+
});
|
|
338
|
+
drawBorderSide({
|
|
339
|
+
ctx,
|
|
340
|
+
side: 'left',
|
|
341
|
+
x,
|
|
342
|
+
y,
|
|
343
|
+
width,
|
|
344
|
+
height,
|
|
345
|
+
borderRadius,
|
|
346
|
+
borderProperties: borders.left,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
// Restore original canvas state
|
|
98
350
|
ctx.strokeStyle = originalStrokeStyle;
|
|
99
351
|
ctx.lineWidth = originalLineWidth;
|
|
100
352
|
ctx.setLineDash(originalLineDash);
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import type { LogLevel } from 'remotion';
|
|
1
2
|
import type { DrawFn } from './drawn-fn';
|
|
2
3
|
export type DrawElementToCanvasReturnValue = 'continue' | 'skip-children';
|
|
3
|
-
export declare const drawElementToCanvas: ({ element, context, draw, offsetLeft, offsetTop, }: {
|
|
4
|
+
export declare const drawElementToCanvas: ({ element, context, draw, offsetLeft, offsetTop, logLevel, }: {
|
|
4
5
|
element: HTMLElement | SVGElement;
|
|
5
6
|
context: OffscreenCanvasRenderingContext2D;
|
|
6
7
|
draw: DrawFn;
|
|
7
8
|
offsetLeft: number;
|
|
8
9
|
offsetTop: number;
|
|
10
|
+
logLevel: LogLevel;
|
|
9
11
|
}) => Promise<DrawElementToCanvasReturnValue>;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { Internals } from 'remotion';
|
|
1
2
|
import { compose } from '../compose';
|
|
3
|
+
import { getBiggestBoundingClientRect } from '../get-biggest-bounding-client-rect';
|
|
2
4
|
import { calculateTransforms } from './calculate-transforms';
|
|
3
5
|
import { drawElement } from './draw-element';
|
|
4
6
|
import { transformIn3d } from './transform-in-3d';
|
|
5
|
-
export const drawElementToCanvas = async ({ element, context, draw, offsetLeft, offsetTop, }) => {
|
|
7
|
+
export const drawElementToCanvas = async ({ element, context, draw, offsetLeft, offsetTop, logLevel, }) => {
|
|
6
8
|
const { totalMatrix, reset, dimensions, opacity, computedStyle } = calculateTransforms(element);
|
|
7
9
|
if (opacity === 0) {
|
|
8
10
|
reset();
|
|
@@ -13,10 +15,12 @@ export const drawElementToCanvas = async ({ element, context, draw, offsetLeft,
|
|
|
13
15
|
return 'continue';
|
|
14
16
|
}
|
|
15
17
|
if (!totalMatrix.is2D) {
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
18
|
+
const biggestBoundingClientRect = getBiggestBoundingClientRect(element);
|
|
19
|
+
const canvasOffsetLeft = Math.min(biggestBoundingClientRect.left, 0);
|
|
20
|
+
const canvasOffsetTop = Math.min(biggestBoundingClientRect.top, 0);
|
|
21
|
+
const tempCanvasWidth = Math.max(biggestBoundingClientRect.width, biggestBoundingClientRect.right);
|
|
22
|
+
const tempCanvasHeight = Math.max(biggestBoundingClientRect.height, biggestBoundingClientRect.bottom);
|
|
23
|
+
const start = Date.now();
|
|
20
24
|
const tempCanvas = new OffscreenCanvas(tempCanvasWidth, tempCanvasHeight);
|
|
21
25
|
const context2 = tempCanvas.getContext('2d');
|
|
22
26
|
if (!context2) {
|
|
@@ -27,7 +31,9 @@ export const drawElementToCanvas = async ({ element, context, draw, offsetLeft,
|
|
|
27
31
|
context: context2,
|
|
28
32
|
offsetLeft: canvasOffsetLeft,
|
|
29
33
|
offsetTop: canvasOffsetTop,
|
|
34
|
+
logLevel,
|
|
30
35
|
});
|
|
36
|
+
const afterCompose = Date.now();
|
|
31
37
|
const transformed = transformIn3d({
|
|
32
38
|
canvasWidth: tempCanvasWidth,
|
|
33
39
|
canvasHeight: tempCanvasHeight,
|
|
@@ -37,7 +43,12 @@ export const drawElementToCanvas = async ({ element, context, draw, offsetLeft,
|
|
|
37
43
|
offsetTop: canvasOffsetTop,
|
|
38
44
|
});
|
|
39
45
|
context.drawImage(transformed, 0, 0);
|
|
46
|
+
const afterDraw = Date.now();
|
|
40
47
|
reset();
|
|
48
|
+
Internals.Log.trace({
|
|
49
|
+
logLevel,
|
|
50
|
+
tag: '@remotion/web-renderer',
|
|
51
|
+
}, `Transforming element in 3D - canvas size: ${tempCanvasWidth}x${tempCanvasHeight} - compose: ${afterCompose - start}ms - draw: ${afterDraw - afterCompose}ms`);
|
|
41
52
|
return 'skip-children';
|
|
42
53
|
}
|
|
43
54
|
await drawElement({
|
|
@@ -4,7 +4,7 @@ import { findLineBreaks } from './find-line-breaks.text';
|
|
|
4
4
|
import { getCollapsedText } from './get-collapsed-text';
|
|
5
5
|
export const drawText = (span) => {
|
|
6
6
|
const drawFn = ({ dimensions: rect, computedStyle, contextToDraw }) => {
|
|
7
|
-
const { fontFamily, fontSize, fontWeight, color,
|
|
7
|
+
const { fontFamily, fontSize, fontWeight, color, direction, writingMode, letterSpacing, textTransform, } = computedStyle;
|
|
8
8
|
const isVertical = writingMode !== 'horizontal-tb';
|
|
9
9
|
if (isVertical) {
|
|
10
10
|
// TODO: Only warn once per render.
|
|
@@ -15,16 +15,13 @@ export const drawText = (span) => {
|
|
|
15
15
|
return;
|
|
16
16
|
}
|
|
17
17
|
contextToDraw.save();
|
|
18
|
-
|
|
18
|
+
const fontSizePx = parseFloat(fontSize);
|
|
19
|
+
contextToDraw.font = `${fontWeight} ${fontSizePx}px ${fontFamily}`;
|
|
19
20
|
contextToDraw.fillStyle = color;
|
|
20
21
|
contextToDraw.letterSpacing = letterSpacing;
|
|
21
|
-
const fontSizePx = parseFloat(fontSize);
|
|
22
|
-
// TODO: This is not necessarily correct, need to create text and measure to know for sure
|
|
23
|
-
const lineHeightPx = lineHeight === 'normal' ? 1.2 * fontSizePx : parseFloat(lineHeight);
|
|
24
|
-
const baselineOffset = (lineHeightPx - fontSizePx) / 2;
|
|
25
22
|
const isRTL = direction === 'rtl';
|
|
26
23
|
contextToDraw.textAlign = isRTL ? 'right' : 'left';
|
|
27
|
-
contextToDraw.textBaseline = '
|
|
24
|
+
contextToDraw.textBaseline = 'alphabetic';
|
|
28
25
|
const originalText = span.textContent;
|
|
29
26
|
const collapsedText = getCollapsedText(span);
|
|
30
27
|
const transformedText = applyTextTransform(collapsedText, textTransform);
|
|
@@ -33,8 +30,9 @@ export const drawText = (span) => {
|
|
|
33
30
|
const xPosition = isRTL ? rect.right : rect.left;
|
|
34
31
|
const lines = findLineBreaks(span, isRTL);
|
|
35
32
|
let offsetTop = 0;
|
|
33
|
+
const { fontBoundingBoxAscent } = contextToDraw.measureText(lines[0].text);
|
|
36
34
|
for (const line of lines) {
|
|
37
|
-
contextToDraw.fillText(line.text, xPosition + line.offsetHorizontal, rect.top +
|
|
35
|
+
contextToDraw.fillText(line.text, xPosition + line.offsetHorizontal, rect.top + offsetTop + fontBoundingBoxAscent);
|
|
38
36
|
offsetTop += line.offsetTop;
|
|
39
37
|
}
|
|
40
38
|
span.textContent = originalText;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getOpticalHeight: (span: HTMLSpanElement) => number;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// A text with e.g. font-size: 12px does not always get rendered with 12px
|
|
2
|
+
// With "Arial", it gets rendered with 13.5px height, with SF Pro, it gets
|
|
3
|
+
// rendered with 14px height. I therefore conclude there is no logic
|
|
4
|
+
// and we need to measure it. I rendered the string "1", "a" and "qË" and
|
|
5
|
+
// they all have the same height, so it doesn't matter if certain characters
|
|
6
|
+
// go low or high.
|
|
7
|
+
export const getOpticalHeight = (span) => {
|
|
8
|
+
const originalText = span.textContent;
|
|
9
|
+
span.textContent = '1';
|
|
10
|
+
const { height } = span.getBoundingClientRect();
|
|
11
|
+
span.textContent = originalText;
|
|
12
|
+
return height;
|
|
13
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { LogLevel } from 'remotion';
|
|
1
2
|
import type { DrawElementToCanvasReturnValue } from '../draw-element-to-canvas';
|
|
2
|
-
export declare const handleTextNode: ({ node, context, offsetLeft, offsetTop, }: {
|
|
3
|
+
export declare const handleTextNode: ({ node, context, offsetLeft, offsetTop, logLevel, }: {
|
|
3
4
|
node: Text;
|
|
4
5
|
context: OffscreenCanvasRenderingContext2D;
|
|
5
6
|
offsetLeft: number;
|
|
6
7
|
offsetTop: number;
|
|
8
|
+
logLevel: LogLevel;
|
|
7
9
|
}) => Promise<DrawElementToCanvasReturnValue>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { drawElementToCanvas } from '../draw-element-to-canvas';
|
|
2
2
|
import { drawText } from './draw-text';
|
|
3
|
-
export const handleTextNode = async ({ node, context, offsetLeft, offsetTop, }) => {
|
|
3
|
+
export const handleTextNode = async ({ node, context, offsetLeft, offsetTop, logLevel, }) => {
|
|
4
4
|
const span = document.createElement('span');
|
|
5
5
|
const parent = node.parentNode;
|
|
6
6
|
if (!parent) {
|
|
@@ -14,6 +14,7 @@ export const handleTextNode = async ({ node, context, offsetLeft, offsetTop, })
|
|
|
14
14
|
draw: drawText(span),
|
|
15
15
|
offsetLeft,
|
|
16
16
|
offsetTop,
|
|
17
|
+
logLevel,
|
|
17
18
|
});
|
|
18
19
|
// Undo the layout manipulation
|
|
19
20
|
parent.insertBefore(node, span);
|
|
@@ -22,6 +22,12 @@ const createHelperCanvas = ({ canvasWidth, canvasHeight, }) => {
|
|
|
22
22
|
helperCanvas.gl.clear(helperCanvas.gl.COLOR_BUFFER_BIT);
|
|
23
23
|
return helperCanvas;
|
|
24
24
|
}
|
|
25
|
+
if (helperCanvas) {
|
|
26
|
+
helperCanvas.gl.deleteProgram(helperCanvas.program);
|
|
27
|
+
helperCanvas.gl.deleteShader(helperCanvas.vertexShader);
|
|
28
|
+
helperCanvas.gl.deleteShader(helperCanvas.fragmentShader);
|
|
29
|
+
helperCanvas = null;
|
|
30
|
+
}
|
|
25
31
|
const canvas = new OffscreenCanvas(canvasWidth, canvasHeight);
|
|
26
32
|
const gl = canvas.getContext('webgl');
|
|
27
33
|
if (!gl) {
|
|
@@ -67,7 +73,7 @@ const createHelperCanvas = ({ canvasWidth, canvasHeight, }) => {
|
|
|
67
73
|
// Enable blending for transparency
|
|
68
74
|
gl.enable(gl.BLEND);
|
|
69
75
|
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
70
|
-
helperCanvas = { canvas, gl, program };
|
|
76
|
+
helperCanvas = { canvas, gl, program, vertexShader, fragmentShader };
|
|
71
77
|
return helperCanvas;
|
|
72
78
|
};
|
|
73
79
|
export const transformIn3d = ({ canvasWidth, canvasHeight, matrix, sourceCanvas, offsetLeft, offsetTop, }) => {
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
export const turnSvgIntoDrawable = (svg) => {
|
|
2
|
+
const { fill, color } = getComputedStyle(svg);
|
|
2
3
|
const originalTransform = svg.style.transform;
|
|
3
4
|
const originalTransformOrigin = svg.style.transformOrigin;
|
|
4
5
|
const originalMarginLeft = svg.style.marginLeft;
|
|
5
6
|
const originalMarginRight = svg.style.marginRight;
|
|
6
7
|
const originalMarginTop = svg.style.marginTop;
|
|
7
8
|
const originalMarginBottom = svg.style.marginBottom;
|
|
9
|
+
const originalFill = svg.style.fill;
|
|
10
|
+
const originalColor = svg.style.color;
|
|
8
11
|
svg.style.transform = 'none';
|
|
9
12
|
svg.style.transformOrigin = '';
|
|
10
13
|
// Margins were already included in the positioning calculation,
|
|
@@ -13,6 +16,8 @@ export const turnSvgIntoDrawable = (svg) => {
|
|
|
13
16
|
svg.style.marginRight = '0';
|
|
14
17
|
svg.style.marginTop = '0';
|
|
15
18
|
svg.style.marginBottom = '0';
|
|
19
|
+
svg.style.fill = fill;
|
|
20
|
+
svg.style.color = color;
|
|
16
21
|
const svgData = new XMLSerializer().serializeToString(svg);
|
|
17
22
|
svg.style.marginLeft = originalMarginLeft;
|
|
18
23
|
svg.style.marginRight = originalMarginRight;
|
|
@@ -20,6 +25,8 @@ export const turnSvgIntoDrawable = (svg) => {
|
|
|
20
25
|
svg.style.marginBottom = originalMarginBottom;
|
|
21
26
|
svg.style.transform = originalTransform;
|
|
22
27
|
svg.style.transformOrigin = originalTransformOrigin;
|
|
28
|
+
svg.style.fill = originalFill;
|
|
29
|
+
svg.style.color = originalColor;
|
|
23
30
|
return new Promise((resolve, reject) => {
|
|
24
31
|
const image = new Image();
|
|
25
32
|
const url = `data:image/svg+xml;base64,${btoa(svgData)}`;
|