@remotion/web-renderer 4.0.424 → 4.0.425
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/add-sample.js +20 -0
- package/dist/artifact.js +56 -0
- package/dist/audio.js +42 -0
- package/dist/can-use-webfs-target.js +19 -0
- package/dist/compose.js +85 -0
- package/dist/create-scaffold.js +104 -0
- package/dist/drawing/border-radius.js +151 -0
- package/dist/drawing/calculate-object-fit.js +208 -0
- package/dist/drawing/calculate-transforms.js +127 -0
- package/dist/drawing/clamp-rect-to-parent-bounds.js +18 -0
- package/dist/drawing/do-rects-intersect.js +6 -0
- package/dist/drawing/draw-background.js +62 -0
- package/dist/drawing/draw-border.js +353 -0
- package/dist/drawing/draw-box-shadow.js +103 -0
- package/dist/drawing/draw-dom-element.js +85 -0
- package/dist/drawing/draw-element.js +84 -0
- package/dist/drawing/draw-outline.js +93 -0
- package/dist/drawing/draw-rounded.js +34 -0
- package/dist/drawing/drawn-fn.js +1 -0
- package/dist/drawing/fit-svg-into-its-dimensions.js +35 -0
- package/dist/drawing/get-clipped-background.d.ts +8 -0
- package/dist/drawing/get-clipped-background.js +14 -0
- package/dist/drawing/get-padding-box.js +30 -0
- package/dist/drawing/get-pretransform-rect.js +49 -0
- package/dist/drawing/handle-3d-transform.js +26 -0
- package/dist/drawing/handle-mask.js +21 -0
- package/dist/drawing/has-transform.js +14 -0
- package/dist/drawing/mask-image.js +14 -0
- package/dist/drawing/opacity.js +7 -0
- package/dist/drawing/overflow.js +14 -0
- package/dist/drawing/parse-linear-gradient.js +260 -0
- package/dist/drawing/parse-transform-origin.js +7 -0
- package/dist/drawing/precompose.d.ts +11 -0
- package/dist/drawing/precompose.js +14 -0
- package/dist/drawing/process-node.js +122 -0
- package/dist/drawing/round-to-expand-rect.js +7 -0
- package/dist/drawing/text/apply-text-transform.js +12 -0
- package/dist/drawing/text/draw-text.js +53 -0
- package/dist/drawing/text/find-line-breaks.text.js +118 -0
- package/dist/drawing/text/get-collapsed-text.d.ts +1 -0
- package/dist/drawing/text/get-collapsed-text.js +46 -0
- package/dist/drawing/text/handle-text-node.js +24 -0
- package/dist/drawing/transform-in-3d.js +177 -0
- package/dist/drawing/transform-rect-with-matrix.js +19 -0
- package/dist/drawing/transform.js +10 -0
- package/dist/drawing/turn-svg-into-drawable.js +41 -0
- package/dist/frame-range.js +15 -0
- package/dist/get-audio-encoding-config.js +18 -0
- package/dist/get-biggest-bounding-client-rect.js +43 -0
- package/dist/index.js +2 -0
- package/dist/internal-state.js +36 -0
- package/dist/mediabunny-mappings.js +63 -0
- package/dist/output-target.js +1 -0
- package/dist/props-if-has-props.js +1 -0
- package/dist/render-media-on-web.js +304 -0
- package/dist/render-operations-queue.js +3 -0
- package/dist/render-still-on-web.js +110 -0
- package/dist/send-telemetry-event.js +22 -0
- package/dist/take-screenshot.js +30 -0
- package/dist/throttle-progress.js +43 -0
- package/dist/tree-walker-cleanup-after-children.js +33 -0
- package/dist/update-time.js +17 -0
- package/dist/validate-video-frame.js +34 -0
- package/dist/wait-for-ready.js +39 -0
- package/dist/walk-tree.js +14 -0
- package/dist/web-fs-target.js +41 -0
- package/dist/with-resolvers.js +9 -0
- package/package.json +9 -9
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { hasAnyTransformCssValue, hasTransformCssValue } from './has-transform';
|
|
2
|
+
import { getMaskImageValue, parseMaskImage } from './mask-image';
|
|
3
|
+
import { parseTransformOrigin } from './parse-transform-origin';
|
|
4
|
+
const getInternalTransformOrigin = (transform) => {
|
|
5
|
+
var _a;
|
|
6
|
+
const centerX = transform.boundingClientRect.width / 2;
|
|
7
|
+
const centerY = transform.boundingClientRect.height / 2;
|
|
8
|
+
const origin = (_a = parseTransformOrigin(transform.transformOrigin)) !== null && _a !== void 0 ? _a : {
|
|
9
|
+
x: centerX,
|
|
10
|
+
y: centerY,
|
|
11
|
+
};
|
|
12
|
+
return origin;
|
|
13
|
+
};
|
|
14
|
+
const getGlobalTransformOrigin = ({ transform }) => {
|
|
15
|
+
const { x: originX, y: originY } = getInternalTransformOrigin(transform);
|
|
16
|
+
return {
|
|
17
|
+
x: originX + transform.boundingClientRect.left,
|
|
18
|
+
y: originY + transform.boundingClientRect.top,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export const calculateTransforms = ({ element, rootElement, }) => {
|
|
22
|
+
// Compute the cumulative transform by traversing parent nodes
|
|
23
|
+
let parent = element;
|
|
24
|
+
const transforms = [];
|
|
25
|
+
const toReset = [];
|
|
26
|
+
let opacity = 1;
|
|
27
|
+
let elementComputedStyle = null;
|
|
28
|
+
let maskImageInfo = null;
|
|
29
|
+
while (parent) {
|
|
30
|
+
const computedStyle = getComputedStyle(parent);
|
|
31
|
+
if (parent === element) {
|
|
32
|
+
elementComputedStyle = computedStyle;
|
|
33
|
+
opacity = parseFloat(computedStyle.opacity);
|
|
34
|
+
const maskImageValue = getMaskImageValue(computedStyle);
|
|
35
|
+
maskImageInfo = maskImageValue ? parseMaskImage(maskImageValue) : null;
|
|
36
|
+
const originalMaskImage = parent.style.maskImage;
|
|
37
|
+
const originalWebkitMaskImage = parent.style.webkitMaskImage;
|
|
38
|
+
parent.style.maskImage = 'none';
|
|
39
|
+
parent.style.webkitMaskImage = 'none';
|
|
40
|
+
const parentRef = parent;
|
|
41
|
+
toReset.push(() => {
|
|
42
|
+
parentRef.style.maskImage = originalMaskImage;
|
|
43
|
+
parentRef.style.webkitMaskImage = originalWebkitMaskImage;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (hasAnyTransformCssValue(computedStyle) || parent === element) {
|
|
47
|
+
const toParse = hasTransformCssValue(computedStyle)
|
|
48
|
+
? computedStyle.transform
|
|
49
|
+
: undefined;
|
|
50
|
+
const matrix = new DOMMatrix(toParse);
|
|
51
|
+
const { transform, scale, rotate } = parent.style;
|
|
52
|
+
const additionalMatrices = [];
|
|
53
|
+
// The order of transformations is:
|
|
54
|
+
// 1. Translate --> We do not have to consider it since it changes getClientBoundingRect()
|
|
55
|
+
// 2. Rotate
|
|
56
|
+
// 3. Scale
|
|
57
|
+
// 4. CSS "transform"
|
|
58
|
+
if (rotate !== '' && rotate !== 'none') {
|
|
59
|
+
additionalMatrices.push(new DOMMatrix(`rotate(${rotate})`));
|
|
60
|
+
}
|
|
61
|
+
if (scale !== '' && scale !== 'none') {
|
|
62
|
+
additionalMatrices.push(new DOMMatrix(`scale(${scale})`));
|
|
63
|
+
}
|
|
64
|
+
additionalMatrices.push(matrix);
|
|
65
|
+
parent.style.transform = 'none';
|
|
66
|
+
parent.style.scale = 'none';
|
|
67
|
+
parent.style.rotate = 'none';
|
|
68
|
+
transforms.push({
|
|
69
|
+
element: parent,
|
|
70
|
+
transformOrigin: computedStyle.transformOrigin,
|
|
71
|
+
boundingClientRect: null,
|
|
72
|
+
matrices: additionalMatrices,
|
|
73
|
+
});
|
|
74
|
+
const parentRef = parent;
|
|
75
|
+
toReset.push(() => {
|
|
76
|
+
parentRef.style.transform = transform;
|
|
77
|
+
parentRef.style.scale = scale;
|
|
78
|
+
parentRef.style.rotate = rotate;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (parent === rootElement) {
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
parent = parent.parentElement;
|
|
85
|
+
}
|
|
86
|
+
for (const transform of transforms) {
|
|
87
|
+
transform.boundingClientRect = transform.element.getBoundingClientRect();
|
|
88
|
+
}
|
|
89
|
+
const dimensions = transforms[0].boundingClientRect;
|
|
90
|
+
const nativeTransformOrigin = getInternalTransformOrigin(transforms[0]);
|
|
91
|
+
const totalMatrix = new DOMMatrix();
|
|
92
|
+
for (const transform of transforms.slice().reverse()) {
|
|
93
|
+
for (const matrix of transform.matrices) {
|
|
94
|
+
const globalTransformOrigin = getGlobalTransformOrigin({
|
|
95
|
+
transform,
|
|
96
|
+
});
|
|
97
|
+
const transformMatrix = new DOMMatrix()
|
|
98
|
+
.translate(globalTransformOrigin.x, globalTransformOrigin.y)
|
|
99
|
+
.multiply(matrix)
|
|
100
|
+
.translate(-globalTransformOrigin.x, -globalTransformOrigin.y);
|
|
101
|
+
totalMatrix.multiplySelf(transformMatrix);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!elementComputedStyle) {
|
|
105
|
+
throw new Error('Element computed style not found');
|
|
106
|
+
}
|
|
107
|
+
const needs3DTransformViaWebGL = !totalMatrix.is2D;
|
|
108
|
+
const needsMaskImage = maskImageInfo !== null;
|
|
109
|
+
return {
|
|
110
|
+
dimensions,
|
|
111
|
+
totalMatrix,
|
|
112
|
+
reset: () => {
|
|
113
|
+
for (const reset of toReset) {
|
|
114
|
+
reset();
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
nativeTransformOrigin,
|
|
118
|
+
computedStyle: elementComputedStyle,
|
|
119
|
+
opacity,
|
|
120
|
+
maskImageInfo,
|
|
121
|
+
precompositing: {
|
|
122
|
+
needs3DTransformViaWebGL,
|
|
123
|
+
needsMaskImage: maskImageInfo,
|
|
124
|
+
needsPrecompositing: Boolean(needs3DTransformViaWebGL || needsMaskImage),
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { roundToExpandRect } from './round-to-expand-rect';
|
|
2
|
+
export const getNarrowerRect = ({ firstRect, secondRect, }) => {
|
|
3
|
+
const left = Math.max(firstRect.left, secondRect.left);
|
|
4
|
+
const top = Math.max(firstRect.top, secondRect.top);
|
|
5
|
+
const bottom = Math.min(firstRect.bottom, secondRect.bottom);
|
|
6
|
+
const right = Math.min(firstRect.right, secondRect.right);
|
|
7
|
+
return new DOMRect(left, top, right - left, bottom - top);
|
|
8
|
+
};
|
|
9
|
+
export const getWiderRectAndExpand = ({ firstRect, secondRect, }) => {
|
|
10
|
+
if (firstRect === null) {
|
|
11
|
+
return roundToExpandRect(secondRect);
|
|
12
|
+
}
|
|
13
|
+
const left = Math.min(firstRect.left, secondRect.left);
|
|
14
|
+
const top = Math.min(firstRect.top, secondRect.top);
|
|
15
|
+
const bottom = Math.max(firstRect.bottom, secondRect.bottom);
|
|
16
|
+
const right = Math.max(firstRect.right, secondRect.right);
|
|
17
|
+
return roundToExpandRect(new DOMRect(left, top, right - left, bottom - top));
|
|
18
|
+
};
|
|
@@ -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, offsetLeft: parentOffsetLeft, offsetTop: parentOffsetTop, }) => {
|
|
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: new DOMRect(boundingRect.left + parentOffsetLeft, boundingRect.top + parentOffsetTop, boundingRect.width, boundingRect.height),
|
|
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 - offsetTop, boundingRect.width, boundingRect.height);
|
|
59
|
+
contextToDraw.fillStyle = originalFillStyle;
|
|
60
|
+
}
|
|
61
|
+
finish();
|
|
62
|
+
};
|
|
@@ -0,0 +1,353 @@
|
|
|
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') {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
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);
|
|
88
|
+
}
|
|
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;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
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();
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const drawUniformBorder = ({ ctx, x, y, width, height, borderRadius, borderWidth, borderColor, borderStyle, }) => {
|
|
153
|
+
ctx.beginPath();
|
|
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
|
|
163
|
+
const adjustedBorderRadius = {
|
|
164
|
+
topLeft: {
|
|
165
|
+
horizontal: Math.max(0, borderRadius.topLeft.horizontal - halfWidth),
|
|
166
|
+
vertical: Math.max(0, borderRadius.topLeft.vertical - halfWidth),
|
|
167
|
+
},
|
|
168
|
+
topRight: {
|
|
169
|
+
horizontal: Math.max(0, borderRadius.topRight.horizontal - halfWidth),
|
|
170
|
+
vertical: Math.max(0, borderRadius.topRight.vertical - halfWidth),
|
|
171
|
+
},
|
|
172
|
+
bottomRight: {
|
|
173
|
+
horizontal: Math.max(0, borderRadius.bottomRight.horizontal - halfWidth),
|
|
174
|
+
vertical: Math.max(0, borderRadius.bottomRight.vertical - halfWidth),
|
|
175
|
+
},
|
|
176
|
+
bottomLeft: {
|
|
177
|
+
horizontal: Math.max(0, borderRadius.bottomLeft.horizontal - halfWidth),
|
|
178
|
+
vertical: Math.max(0, borderRadius.bottomLeft.vertical - halfWidth),
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
// Draw continuous path with border radius
|
|
182
|
+
ctx.moveTo(borderX + adjustedBorderRadius.topLeft.horizontal, borderY);
|
|
183
|
+
// Top edge
|
|
184
|
+
ctx.lineTo(borderX + borderW - adjustedBorderRadius.topRight.horizontal, borderY);
|
|
185
|
+
// Top-right corner
|
|
186
|
+
if (adjustedBorderRadius.topRight.horizontal > 0 ||
|
|
187
|
+
adjustedBorderRadius.topRight.vertical > 0) {
|
|
188
|
+
ctx.ellipse(borderX + borderW - adjustedBorderRadius.topRight.horizontal, borderY + adjustedBorderRadius.topRight.vertical, adjustedBorderRadius.topRight.horizontal, adjustedBorderRadius.topRight.vertical, 0, -Math.PI / 2, 0);
|
|
189
|
+
}
|
|
190
|
+
// Right edge
|
|
191
|
+
ctx.lineTo(borderX + borderW, borderY + borderH - adjustedBorderRadius.bottomRight.vertical);
|
|
192
|
+
// Bottom-right corner
|
|
193
|
+
if (adjustedBorderRadius.bottomRight.horizontal > 0 ||
|
|
194
|
+
adjustedBorderRadius.bottomRight.vertical > 0) {
|
|
195
|
+
ctx.ellipse(borderX + borderW - adjustedBorderRadius.bottomRight.horizontal, borderY + borderH - adjustedBorderRadius.bottomRight.vertical, adjustedBorderRadius.bottomRight.horizontal, adjustedBorderRadius.bottomRight.vertical, 0, 0, Math.PI / 2);
|
|
196
|
+
}
|
|
197
|
+
// Bottom edge
|
|
198
|
+
ctx.lineTo(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY + borderH);
|
|
199
|
+
// Bottom-left corner
|
|
200
|
+
if (adjustedBorderRadius.bottomLeft.horizontal > 0 ||
|
|
201
|
+
adjustedBorderRadius.bottomLeft.vertical > 0) {
|
|
202
|
+
ctx.ellipse(borderX + adjustedBorderRadius.bottomLeft.horizontal, borderY + borderH - adjustedBorderRadius.bottomLeft.vertical, adjustedBorderRadius.bottomLeft.horizontal, adjustedBorderRadius.bottomLeft.vertical, 0, Math.PI / 2, Math.PI);
|
|
203
|
+
}
|
|
204
|
+
// Left edge
|
|
205
|
+
ctx.lineTo(borderX, borderY + adjustedBorderRadius.topLeft.vertical);
|
|
206
|
+
// Top-left corner
|
|
207
|
+
if (adjustedBorderRadius.topLeft.horizontal > 0 ||
|
|
208
|
+
adjustedBorderRadius.topLeft.vertical > 0) {
|
|
209
|
+
ctx.ellipse(borderX + adjustedBorderRadius.topLeft.horizontal, borderY + adjustedBorderRadius.topLeft.vertical, adjustedBorderRadius.topLeft.horizontal, adjustedBorderRadius.topLeft.vertical, 0, Math.PI, (Math.PI * 3) / 2);
|
|
210
|
+
}
|
|
211
|
+
ctx.closePath();
|
|
212
|
+
ctx.stroke();
|
|
213
|
+
};
|
|
214
|
+
export const drawBorder = ({ ctx, rect, 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: rect.left,
|
|
244
|
+
y: rect.top,
|
|
245
|
+
width: rect.width,
|
|
246
|
+
height: rect.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: rect.left,
|
|
259
|
+
y: rect.top,
|
|
260
|
+
width: rect.width,
|
|
261
|
+
height: rect.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: rect.left,
|
|
272
|
+
y: rect.top,
|
|
273
|
+
width: rect.width,
|
|
274
|
+
height: rect.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: rect.left,
|
|
285
|
+
y: rect.top,
|
|
286
|
+
width: rect.width,
|
|
287
|
+
height: rect.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: rect.left,
|
|
298
|
+
y: rect.top,
|
|
299
|
+
width: rect.width,
|
|
300
|
+
height: rect.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: rect.left,
|
|
312
|
+
y: rect.top,
|
|
313
|
+
width: rect.width,
|
|
314
|
+
height: rect.height,
|
|
315
|
+
borderRadius,
|
|
316
|
+
borderProperties: borders.top,
|
|
317
|
+
});
|
|
318
|
+
drawBorderSide({
|
|
319
|
+
ctx,
|
|
320
|
+
side: 'right',
|
|
321
|
+
x: rect.left,
|
|
322
|
+
y: rect.top,
|
|
323
|
+
width: rect.width,
|
|
324
|
+
height: rect.height,
|
|
325
|
+
borderRadius,
|
|
326
|
+
borderProperties: borders.right,
|
|
327
|
+
});
|
|
328
|
+
drawBorderSide({
|
|
329
|
+
ctx,
|
|
330
|
+
side: 'bottom',
|
|
331
|
+
x: rect.left,
|
|
332
|
+
y: rect.top,
|
|
333
|
+
width: rect.width,
|
|
334
|
+
height: rect.height,
|
|
335
|
+
borderRadius,
|
|
336
|
+
borderProperties: borders.bottom,
|
|
337
|
+
});
|
|
338
|
+
drawBorderSide({
|
|
339
|
+
ctx,
|
|
340
|
+
side: 'left',
|
|
341
|
+
x: rect.left,
|
|
342
|
+
y: rect.top,
|
|
343
|
+
width: rect.width,
|
|
344
|
+
height: rect.height,
|
|
345
|
+
borderRadius,
|
|
346
|
+
borderProperties: borders.left,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
// Restore original canvas state
|
|
350
|
+
ctx.strokeStyle = originalStrokeStyle;
|
|
351
|
+
ctx.lineWidth = originalLineWidth;
|
|
352
|
+
ctx.setLineDash(originalLineDash);
|
|
353
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Internals } from 'remotion';
|
|
2
|
+
import { drawRoundedRectPath } from './draw-rounded';
|
|
3
|
+
export const parseBoxShadow = (boxShadowValue) => {
|
|
4
|
+
if (!boxShadowValue || boxShadowValue === 'none') {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
const shadows = [];
|
|
8
|
+
// Split by comma, but respect rgba() colors
|
|
9
|
+
const shadowStrings = boxShadowValue.split(/,(?![^(]*\))/);
|
|
10
|
+
for (const shadowStr of shadowStrings) {
|
|
11
|
+
const trimmed = shadowStr.trim();
|
|
12
|
+
if (!trimmed || trimmed === 'none') {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const shadow = {
|
|
16
|
+
offsetX: 0,
|
|
17
|
+
offsetY: 0,
|
|
18
|
+
blurRadius: 0,
|
|
19
|
+
color: 'rgba(0, 0, 0, 0.5)',
|
|
20
|
+
inset: false,
|
|
21
|
+
};
|
|
22
|
+
// Check for inset
|
|
23
|
+
shadow.inset = /\binset\b/i.test(trimmed);
|
|
24
|
+
// Remove 'inset' keyword
|
|
25
|
+
let remaining = trimmed.replace(/\binset\b/gi, '').trim();
|
|
26
|
+
// Extract color (can be rgb(), rgba(), hsl(), hsla(), hex, or named color)
|
|
27
|
+
const colorMatch = remaining.match(/(rgba?\([^)]+\)|hsla?\([^)]+\)|#[0-9a-f]{3,8}|[a-z]+)/i);
|
|
28
|
+
if (colorMatch) {
|
|
29
|
+
shadow.color = colorMatch[0];
|
|
30
|
+
remaining = remaining.replace(colorMatch[0], '').trim();
|
|
31
|
+
}
|
|
32
|
+
// Parse remaining numeric values (offset-x offset-y blur spread)
|
|
33
|
+
const numbers = remaining.match(/[+-]?\d*\.?\d+(?:px|em|rem|%)?/gi) || [];
|
|
34
|
+
const values = numbers.map((n) => parseFloat(n) || 0);
|
|
35
|
+
if (values.length >= 2) {
|
|
36
|
+
shadow.offsetX = values[0];
|
|
37
|
+
shadow.offsetY = values[1];
|
|
38
|
+
if (values.length >= 3) {
|
|
39
|
+
shadow.blurRadius = Math.max(0, values[2]); // Blur cannot be negative
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
shadows.push(shadow);
|
|
43
|
+
}
|
|
44
|
+
return shadows;
|
|
45
|
+
};
|
|
46
|
+
export const drawBorderRadius = ({ ctx, rect, borderRadius, computedStyle, logLevel, }) => {
|
|
47
|
+
const shadows = parseBoxShadow(computedStyle.boxShadow);
|
|
48
|
+
if (shadows.length === 0) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// Draw shadows from last to first (so first shadow appears on top)
|
|
52
|
+
for (let i = shadows.length - 1; i >= 0; i--) {
|
|
53
|
+
const shadow = shadows[i];
|
|
54
|
+
const newLeft = rect.left + Math.min(shadow.offsetX, 0) - shadow.blurRadius;
|
|
55
|
+
const newRight = rect.right + Math.max(shadow.offsetX, 0) + shadow.blurRadius;
|
|
56
|
+
const newTop = rect.top + Math.min(shadow.offsetY, 0) - shadow.blurRadius;
|
|
57
|
+
const newBottom = rect.bottom + Math.max(shadow.offsetY, 0) + shadow.blurRadius;
|
|
58
|
+
const newRect = new DOMRect(newLeft, newTop, newRight - newLeft, newBottom - newTop);
|
|
59
|
+
const leftOffset = rect.left - newLeft;
|
|
60
|
+
const topOffset = rect.top - newTop;
|
|
61
|
+
const newCanvas = new OffscreenCanvas(newRect.width, newRect.height);
|
|
62
|
+
const newCtx = newCanvas.getContext('2d');
|
|
63
|
+
if (!newCtx) {
|
|
64
|
+
throw new Error('Failed to get context');
|
|
65
|
+
}
|
|
66
|
+
if (shadow.inset) {
|
|
67
|
+
// TODO: Only warn once per render.
|
|
68
|
+
Internals.Log.warn({
|
|
69
|
+
logLevel,
|
|
70
|
+
tag: '@remotion/web-renderer',
|
|
71
|
+
}, 'Detected "box-shadow" with "inset". This is not yet supported in @remotion/web-renderer');
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
// Apply shadow properties to canvas
|
|
75
|
+
newCtx.shadowBlur = shadow.blurRadius;
|
|
76
|
+
newCtx.shadowColor = shadow.color;
|
|
77
|
+
newCtx.shadowOffsetX = shadow.offsetX;
|
|
78
|
+
newCtx.shadowOffsetY = shadow.offsetY;
|
|
79
|
+
newCtx.fillStyle = 'black';
|
|
80
|
+
drawRoundedRectPath({
|
|
81
|
+
ctx: newCtx,
|
|
82
|
+
x: leftOffset,
|
|
83
|
+
y: topOffset,
|
|
84
|
+
width: rect.width,
|
|
85
|
+
height: rect.height,
|
|
86
|
+
borderRadius,
|
|
87
|
+
});
|
|
88
|
+
newCtx.fill();
|
|
89
|
+
// Cut out the shape, leaving only shadow
|
|
90
|
+
newCtx.shadowColor = 'transparent';
|
|
91
|
+
newCtx.globalCompositeOperation = 'destination-out';
|
|
92
|
+
drawRoundedRectPath({
|
|
93
|
+
ctx: newCtx,
|
|
94
|
+
x: leftOffset,
|
|
95
|
+
y: topOffset,
|
|
96
|
+
width: rect.width,
|
|
97
|
+
height: rect.height,
|
|
98
|
+
borderRadius,
|
|
99
|
+
});
|
|
100
|
+
newCtx.fill();
|
|
101
|
+
ctx.drawImage(newCanvas, rect.left - leftOffset, rect.top - topOffset);
|
|
102
|
+
}
|
|
103
|
+
};
|