Skip to content

🎥 Make videos programmatically with React

draw border uniformly 🫶

JonnyBurger9a5329cf0a29
diff --git a/packages/web-renderer/src/drawing/draw-border.ts b/packages/web-renderer/src/drawing/draw-border.ts
index 0daad6d10a..6c230fda31 100644
--- a/packages/web-renderer/src/drawing/draw-border.ts
+++ b/packages/web-renderer/src/drawing/draw-border.ts
@@ -235,6 +235,150 @@ const drawCorner = ({
}
};
+const drawUniformBorder = ({
+ ctx,
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ borderWidth,
+ borderColor,
+ borderStyle,
+}: {
+ ctx: OffscreenCanvasRenderingContext2D;
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ borderRadius: BorderRadiusCorners;
+ borderWidth: number;
+ borderColor: string;
+ borderStyle: string;
+}) => {
+ ctx.beginPath();
+ ctx.strokeStyle = borderColor;
+ ctx.lineWidth = borderWidth;
+ ctx.setLineDash(getLineDashPattern(borderStyle, borderWidth));
+
+ const halfWidth = borderWidth / 2;
+ const borderX = x + halfWidth;
+ const borderY = y + halfWidth;
+ const borderW = width - borderWidth;
+ const borderH = height - borderWidth;
+
+ // Adjust border radius for the border width
+ const adjustedBorderRadius = {
+ topLeft: {
+ horizontal: Math.max(0, borderRadius.topLeft.horizontal - halfWidth),
+ vertical: Math.max(0, borderRadius.topLeft.vertical - halfWidth),
+ },
+ topRight: {
+ horizontal: Math.max(0, borderRadius.topRight.horizontal - halfWidth),
+ vertical: Math.max(0, borderRadius.topRight.vertical - halfWidth),
+ },
+ bottomRight: {
+ horizontal: Math.max(0, borderRadius.bottomRight.horizontal - halfWidth),
+ vertical: Math.max(0, borderRadius.bottomRight.vertical - halfWidth),
+ },
+ bottomLeft: {
+ horizontal: Math.max(0, borderRadius.bottomLeft.horizontal - halfWidth),
+ vertical: Math.max(0, borderRadius.bottomLeft.vertical - halfWidth),
+ },
+ };
+
+ // Draw continuous path with border radius
+ ctx.moveTo(borderX + adjustedBorderRadius.topLeft.horizontal, borderY);
+
+ // Top edge
+ ctx.lineTo(
+ borderX + borderW - adjustedBorderRadius.topRight.horizontal,
+ borderY,
+ );
+
+ // Top-right corner
+ if (
+ adjustedBorderRadius.topRight.horizontal > 0 ||
+ adjustedBorderRadius.topRight.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + borderW - adjustedBorderRadius.topRight.horizontal,
+ borderY + adjustedBorderRadius.topRight.vertical,
+ adjustedBorderRadius.topRight.horizontal,
+ adjustedBorderRadius.topRight.vertical,
+ 0,
+ -Math.PI / 2,
+ 0,
+ );
+ }
+
+ // Right edge
+ ctx.lineTo(
+ borderX + borderW,
+ borderY + borderH - adjustedBorderRadius.bottomRight.vertical,
+ );
+
+ // Bottom-right corner
+ if (
+ adjustedBorderRadius.bottomRight.horizontal > 0 ||
+ adjustedBorderRadius.bottomRight.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + borderW - adjustedBorderRadius.bottomRight.horizontal,
+ borderY + borderH - adjustedBorderRadius.bottomRight.vertical,
+ adjustedBorderRadius.bottomRight.horizontal,
+ adjustedBorderRadius.bottomRight.vertical,
+ 0,
+ 0,
+ Math.PI / 2,
+ );
+ }
+
+ // Bottom edge
+ ctx.lineTo(
+ borderX + adjustedBorderRadius.bottomLeft.horizontal,
+ borderY + borderH,
+ );
+
+ // Bottom-left corner
+ if (
+ adjustedBorderRadius.bottomLeft.horizontal > 0 ||
+ adjustedBorderRadius.bottomLeft.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + adjustedBorderRadius.bottomLeft.horizontal,
+ borderY + borderH - adjustedBorderRadius.bottomLeft.vertical,
+ adjustedBorderRadius.bottomLeft.horizontal,
+ adjustedBorderRadius.bottomLeft.vertical,
+ 0,
+ Math.PI / 2,
+ Math.PI,
+ );
+ }
+
+ // Left edge
+ ctx.lineTo(borderX, borderY + adjustedBorderRadius.topLeft.vertical);
+
+ // Top-left corner
+ if (
+ adjustedBorderRadius.topLeft.horizontal > 0 ||
+ adjustedBorderRadius.topLeft.vertical > 0
+ ) {
+ ctx.ellipse(
+ borderX + adjustedBorderRadius.topLeft.horizontal,
+ borderY + adjustedBorderRadius.topLeft.vertical,
+ adjustedBorderRadius.topLeft.horizontal,
+ adjustedBorderRadius.topLeft.vertical,
+ 0,
+ Math.PI,
+ (Math.PI * 3) / 2,
+ );
+ }
+
+ ctx.closePath();
+ ctx.stroke();
+};
+
export const drawBorder = ({
ctx,
x,
@@ -270,107 +414,135 @@ export const drawBorder = ({
const originalLineWidth = ctx.lineWidth;
const originalLineDash = ctx.getLineDash();
- // Draw corners first (they go underneath the straight edges)
- drawCorner({
- ctx,
- corner: 'topLeft',
- x,
- y,
- width,
- height,
- borderRadius,
- topBorder: borders.top,
- rightBorder: borders.right,
- bottomBorder: borders.bottom,
- leftBorder: borders.left,
- });
-
- drawCorner({
- ctx,
- corner: 'topRight',
- x,
- y,
- width,
- height,
- borderRadius,
- topBorder: borders.top,
- rightBorder: borders.right,
- bottomBorder: borders.bottom,
- leftBorder: borders.left,
- });
-
- drawCorner({
- ctx,
- corner: 'bottomRight',
- x,
- y,
- width,
- height,
- borderRadius,
- topBorder: borders.top,
- rightBorder: borders.right,
- bottomBorder: borders.bottom,
- leftBorder: borders.left,
- });
-
- drawCorner({
- ctx,
- corner: 'bottomLeft',
- x,
- y,
- width,
- height,
- borderRadius,
- topBorder: borders.top,
- rightBorder: borders.right,
- bottomBorder: borders.bottom,
- leftBorder: borders.left,
- });
-
- // Draw each border side
- drawBorderSide({
- ctx,
- side: 'top',
- x,
- y,
- width,
- height,
- borderRadius,
- borderProperties: borders.top,
- });
-
- drawBorderSide({
- ctx,
- side: 'right',
- x,
- y,
- width,
- height,
- borderRadius,
- borderProperties: borders.right,
- });
-
- drawBorderSide({
- ctx,
- side: 'bottom',
- x,
- y,
- width,
- height,
- borderRadius,
- borderProperties: borders.bottom,
- });
-
- drawBorderSide({
- ctx,
- side: 'left',
- x,
- y,
- width,
- height,
- borderRadius,
- borderProperties: borders.left,
- });
+ // Check if all borders are uniform (same width, color, and style)
+ const allSidesEqual =
+ borders.top.width === borders.right.width &&
+ borders.top.width === borders.bottom.width &&
+ borders.top.width === borders.left.width &&
+ borders.top.color === borders.right.color &&
+ borders.top.color === borders.bottom.color &&
+ borders.top.color === borders.left.color &&
+ borders.top.style === borders.right.style &&
+ borders.top.style === borders.bottom.style &&
+ borders.top.style === borders.left.style &&
+ borders.top.width > 0;
+
+ if (allSidesEqual) {
+ // Draw as a single continuous border for continuous dashing
+ drawUniformBorder({
+ ctx,
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ borderWidth: borders.top.width,
+ borderColor: borders.top.color,
+ borderStyle: borders.top.style,
+ });
+ } else {
+ // Draw corners first (they go underneath the straight edges)
+ drawCorner({
+ ctx,
+ corner: 'topLeft',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ topBorder: borders.top,
+ rightBorder: borders.right,
+ bottomBorder: borders.bottom,
+ leftBorder: borders.left,
+ });
+
+ drawCorner({
+ ctx,
+ corner: 'topRight',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ topBorder: borders.top,
+ rightBorder: borders.right,
+ bottomBorder: borders.bottom,
+ leftBorder: borders.left,
+ });
+
+ drawCorner({
+ ctx,
+ corner: 'bottomRight',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ topBorder: borders.top,
+ rightBorder: borders.right,
+ bottomBorder: borders.bottom,
+ leftBorder: borders.left,
+ });
+
+ drawCorner({
+ ctx,
+ corner: 'bottomLeft',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ topBorder: borders.top,
+ rightBorder: borders.right,
+ bottomBorder: borders.bottom,
+ leftBorder: borders.left,
+ });
+
+ // Draw each border side
+ drawBorderSide({
+ ctx,
+ side: 'top',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ borderProperties: borders.top,
+ });
+
+ drawBorderSide({
+ ctx,
+ side: 'right',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ borderProperties: borders.right,
+ });
+
+ drawBorderSide({
+ ctx,
+ side: 'bottom',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ borderProperties: borders.bottom,
+ });
+
+ drawBorderSide({
+ ctx,
+ side: 'left',
+ x,
+ y,
+ width,
+ height,
+ borderRadius,
+ borderProperties: borders.left,
+ });
+ }
// Restore original canvas state
ctx.strokeStyle = originalStrokeStyle;
diff --git a/packages/web-renderer/src/test/Root.tsx b/packages/web-renderer/src/test/Root.tsx
index a904b595d6..9f51df0e2a 100644
--- a/packages/web-renderer/src/test/Root.tsx
+++ b/packages/web-renderer/src/test/Root.tsx
@@ -73,6 +73,7 @@ export const Root: React.FC = () => {
<Composition {...borderRadiusPercentage} />
<Composition {...borderRadiusNone} />
<Composition {...borderRadiusClamped} />
+ <Composition {...borderIndividualSides} />
</Folder>
<Folder name="Text">
<Composition {...textFixture} />
diff --git a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-chromium-darwin.png b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-chromium-darwin.png
index add421bfd6..5d0dae4624 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-chromium-darwin.png and b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-chromium-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-firefox-darwin.png b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-firefox-darwin.png
index 314f6b523d..8b4283581c 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-firefox-darwin.png and b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-firefox-darwin.png differ
diff --git a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-webkit-darwin.png b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-webkit-darwin.png
index 720b855677..dca2b68717 100644
Binary files a/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-webkit-darwin.png and b/packages/web-renderer/src/test/__screenshots__/border.test.tsx/border-webkit-darwin.png differ