Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

fix: resolve SVG border artifacts during emphasis animations

Augustus Otue7491c88da3f
diff --git a/apps/web/src/components/editor/Preview.tsx b/apps/web/src/components/editor/Preview.tsx
index a5933e2b..bf8a5c38 100644
--- a/apps/web/src/components/editor/Preview.tsx
+++ b/apps/web/src/components/editor/Preview.tsx
@@ -3329,32 +3329,13 @@ export const Preview: React.FC = () => {
(sc) => sc.trackId === track.id,
);
for (const shapeClip of trackShapeClips) {
- const offscreen = new OffscreenCanvas(
+ renderShapeClipToCanvas(
+ ctx,
+ shapeClip,
canvas.width,
canvas.height,
+ currentPlayhead,
);
- const offCtx = offscreen.getContext("2d");
- if (offCtx) {
- renderShapeClipToCanvas(
- offCtx as unknown as CanvasRenderingContext2D,
- shapeClip,
- canvas.width,
- canvas.height,
- currentPlayhead,
- );
- const bitmap = await createImageBitmap(offscreen);
- tempBitmaps.push(bitmap);
- gpuLayers.push({
- bitmap,
- transform: {
- ...DEFAULT_TRANSFORM,
- opacity: 1,
- scale: { x: 1, y: 1 },
- position: { x: 0, y: 0 },
- anchor: { x: 0, y: 0 },
- },
- });
- }
}
} else if (track.type === "text") {
const trackTextClips = activeTextClips.filter(
@@ -3944,7 +3925,6 @@ export const Preview: React.FC = () => {
const canvasRect = canvas.getBoundingClientRect();
const { transform } = selectedShapeClip;
- const shapeSize = 200;
const canvasWidth = settings.width;
const canvasHeight = settings.height;
@@ -3969,8 +3949,28 @@ export const Preview: React.FC = () => {
const displayScale = actualWidth / canvasWidth;
- const shapeWidth = shapeSize * transform.scale.x * displayScale;
- const shapeHeight = shapeSize * transform.scale.y * displayScale;
+ let baseWidth: number;
+ let baseHeight: number;
+
+ if (selectedShapeClip.type === "svg") {
+ const svgClip = selectedShapeClip as SVGClip;
+ const svgWidth = svgClip.viewBox?.width || 200;
+ const svgHeight = svgClip.viewBox?.height || 200;
+ const svgAspect = svgWidth / svgHeight;
+ if (svgAspect > 1) {
+ baseWidth = canvasWidth;
+ baseHeight = canvasWidth / svgAspect;
+ } else {
+ baseHeight = canvasHeight;
+ baseWidth = canvasHeight * svgAspect;
+ }
+ } else {
+ baseWidth = 200;
+ baseHeight = 200;
+ }
+
+ const shapeWidth = baseWidth * transform.scale.x * displayScale;
+ const shapeHeight = baseHeight * transform.scale.y * displayScale;
const posX = transform.position.x * canvasWidth * displayScale;
const posY = transform.position.y * canvasHeight * displayScale;
@@ -4003,9 +4003,6 @@ export const Preview: React.FC = () => {
const canvasRect = canvas.getBoundingClientRect();
const { transform } = clip;
- // Approximation of the displayed clip size in canvas-coordinate units,
- // consistent with the value used in shapeClipBounds for the resize overlay.
- const shapeSize = 200;
const canvasWidth = settings.width;
const canvasHeight = settings.height;
@@ -4030,8 +4027,28 @@ export const Preview: React.FC = () => {
const displayScale = actualWidth / canvasWidth;
- const shapeWidth = shapeSize * transform.scale.x * displayScale;
- const shapeHeight = shapeSize * transform.scale.y * displayScale;
+ let baseWidth: number;
+ let baseHeight: number;
+
+ if (clip.type === "svg") {
+ const svgClip = clip as SVGClip;
+ const svgW = svgClip.viewBox?.width || 200;
+ const svgH = svgClip.viewBox?.height || 200;
+ const svgAspect = svgW / svgH;
+ if (svgAspect > 1) {
+ baseWidth = canvasWidth;
+ baseHeight = canvasWidth / svgAspect;
+ } else {
+ baseHeight = canvasHeight;
+ baseWidth = canvasHeight * svgAspect;
+ }
+ } else {
+ baseWidth = 200;
+ baseHeight = 200;
+ }
+
+ const shapeWidth = baseWidth * transform.scale.x * displayScale;
+ const shapeHeight = baseHeight * transform.scale.y * displayScale;
const posX = transform.position.x * canvasWidth * displayScale;
const posY = transform.position.y * canvasHeight * displayScale;
@@ -4825,8 +4842,11 @@ export const Preview: React.FC = () => {
const showTextClipHandles = !isPlaying && selectedTextClip && textClipBounds;
+
const showShapeClipHandles =
- !isPlaying && selectedShapeClip && shapeClipBounds;
+ !isPlaying &&
+ selectedShapeClip &&
+ shapeClipBounds;
const showSubtitleOverlay =
!isPlaying && selectedSubtitleObj && subtitleBounds;
@@ -5169,8 +5189,9 @@ export const Preview: React.FC = () => {
height: shapeClipBounds.height,
}}
>
- {/* Selection border - green for shape clips */}
- <div className="absolute inset-0 border-2 border-green-500 pointer-events-none" />
+ {selectedShapeClip.type !== "svg" && (
+ <div className="absolute inset-0 border-2 border-green-500 pointer-events-none" />
+ )}
{/* Move handle (center) */}
<div
@@ -5256,6 +5277,7 @@ export const Preview: React.FC = () => {
{/* Graphic Clip Hover Indicators */}
{!cropMode && !isPlaying &&
activeGraphicClips.map((clip) => {
+ if (clip.type === "svg") return null;
if (clip.id === selectedShapeClipId) return null;
if (clip.id !== hoveredGraphicClipId) return null;
const bounds = getGraphicClipDisplayBounds(clip);
diff --git a/apps/web/src/components/editor/preview/canvas-renderers.ts b/apps/web/src/components/editor/preview/canvas-renderers.ts
index c71b9e92..9b75471e 100644
--- a/apps/web/src/components/editor/preview/canvas-renderers.ts
+++ b/apps/web/src/components/editor/preview/canvas-renderers.ts
@@ -791,6 +791,48 @@ export const setImageLoadCallback = (callback: (() => void) | null): void => {
imageLoadCallback = callback;
};
+const wrapSVGWithTransparentPadding = (
+ svgContent: string,
+ width: number,
+ height: number,
+ padding: number,
+ viewBox?: { minX: number; minY: number; width: number; height: number },
+): string => {
+ if (padding <= 0) {
+ return svgContent;
+ }
+
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(svgContent, "image/svg+xml");
+ const svgEl = doc.querySelector("svg");
+
+ if (!svgEl) {
+ return svgContent;
+ }
+
+ const vbMinX = viewBox?.minX ?? 0;
+ const vbMinY = viewBox?.minY ?? 0;
+ const vbWidth = viewBox?.width ?? width;
+ const vbHeight = viewBox?.height ?? height;
+
+ const padX = (padding / width) * vbWidth;
+ const padY = (padding / height) * vbHeight;
+
+ const paddedWidth = width + padding * 2;
+ const paddedHeight = height + padding * 2;
+
+ svgEl.setAttribute("width", String(paddedWidth));
+ svgEl.setAttribute("height", String(paddedHeight));
+ svgEl.setAttribute(
+ "viewBox",
+ `${vbMinX - padX} ${vbMinY - padY} ${vbWidth + padX * 2} ${vbHeight + padY * 2}`,
+ );
+ svgEl.setAttribute("overflow", "visible");
+
+ return new XMLSerializer().serializeToString(svgEl);
+};
+
+
const renderStickerClip = (
ctx: CanvasRenderingContext2D,
stickerClip: StickerClip,
@@ -887,11 +929,8 @@ const renderSVGClip = (
ctx.save();
- const posX = transform.position.x * canvasWidth;
- const posY = transform.position.y * canvasHeight;
-
- ctx.translate(posX, posY);
- ctx.rotate((transform.rotation * Math.PI) / 180);
+ const posX = Math.round(transform.position.x * canvasWidth);
+ const posY = Math.round(transform.position.y * canvasHeight);
let animationScale = { x: transform.scale.x, y: transform.scale.y };
let animationOpacity = transform.opacity;
@@ -1172,21 +1211,54 @@ const renderSVGClip = (
}
}
- ctx.translate(animationTranslateX, animationTranslateY);
+ ctx.translate(
+ posX + Math.round(animationTranslateX),
+ posY + Math.round(animationTranslateY),
+ );
+ ctx.rotate((transform.rotation * Math.PI) / 180);
ctx.scale(animationScale.x, animationScale.y);
ctx.globalAlpha = animationOpacity;
- // Maintain cache of SVG images to avoid recreating blob URLs and Image objects on every frame
- const cacheKey = svgClip.id;
+ const svgWidth = viewBox?.width || 200;
+ const svgHeight = viewBox?.height || 200;
+ const svgAspect = svgWidth / svgHeight;
+ const maxScale = Math.max(
+ Math.abs(animationScale.x),
+ Math.abs(animationScale.y),
+ 1,
+ );
+ const scaleBucket = Math.ceil(maxScale * 2) / 2;
+
+ let renderWidth: number;
+ let renderHeight: number;
+ if (svgAspect > 1) {
+ renderWidth = Math.ceil(canvasWidth * scaleBucket);
+ renderHeight = Math.ceil(renderWidth / svgAspect);
+ } else {
+ renderHeight = Math.ceil(canvasHeight * scaleBucket);
+ renderWidth = Math.ceil(renderHeight * svgAspect);
+ }
+
+ const svgPad = 16;
+ const paddedWidth = renderWidth + svgPad * 2;
+ const paddedHeight = renderHeight + svgPad * 2;
+ const cacheKey = `${svgClip.id}_${renderWidth}x${renderHeight}`;
let img = svgImageCache.get(cacheKey);
if (!img) {
+ const scaledContent = wrapSVGWithTransparentPadding(
+ svgContent,
+ renderWidth,
+ renderHeight,
+ svgPad,
+ viewBox ? { minX: viewBox.minX, minY: viewBox.minY, width: svgWidth, height: svgHeight } : undefined,
+ );
+
img = new Image();
- const blob = new Blob([svgContent], { type: "image/svg+xml" });
+ const blob = new Blob([scaledContent], { type: "image/svg+xml" });
img.onload = () => imageLoadCallback?.();
img.src = URL.createObjectURL(blob);
svgImageCache.set(cacheKey, img);
- // Limit cache size to prevent unbounded memory growth, remove oldest on overflow
if (svgImageCache.size > 50) {
const firstKey = svgImageCache.keys().next().value;
if (firstKey) {
@@ -1200,72 +1272,83 @@ const renderSVGClip = (
}
if (img.complete && img.naturalWidth > 0) {
- const svgWidth = viewBox?.width || 200;
- const svgHeight = viewBox?.height || 200;
-
- const tempCanvas = document.createElement("canvas");
- tempCanvas.width = svgWidth;
- tempCanvas.height = svgHeight;
- const tempCtx = tempCanvas.getContext("2d");
-
- if (tempCtx) {
- tempCtx.drawImage(img, 0, 0, svgWidth, svgHeight);
-
- // Apply color tint to SVG by using 'source-in' composite mode
- // This multiplies the tint color only where the SVG pixels exist
- if (
- colorStyle &&
- colorStyle.colorMode &&
- colorStyle.colorMode !== "none"
- ) {
+ const drawWidth = renderWidth / scaleBucket;
+ const drawHeight = renderHeight / scaleBucket;
+ const padDraw = svgPad / scaleBucket;
+
+ const needsTempCanvas =
+ (colorStyle && colorStyle.colorMode && colorStyle.colorMode !== "none") ||
+ (drawProgress > 0 && drawProgress < 1);
+
+ if (clipRect) {
+ ctx.save();
+ ctx.beginPath();
+ ctx.rect(
+ -drawWidth / 2 + (clipRect.x / svgWidth) * drawWidth,
+ -drawHeight / 2 + (clipRect.y / svgHeight) * drawHeight,
+ (clipRect.width / svgWidth) * drawWidth,
+ (clipRect.height / svgHeight) * drawHeight,
+ );
+ ctx.clip();
+ }
+
+ if (needsTempCanvas) {
+ const tempCanvas = document.createElement("canvas");
+ tempCanvas.width = paddedWidth;
+ tempCanvas.height = paddedHeight;
+ const tempCtx = tempCanvas.getContext("2d");
+
+ if (tempCtx) {
+ tempCtx.drawImage(img, 0, 0, paddedWidth, paddedHeight);
+
if (
- colorStyle.colorMode === "tint" ||
- colorStyle.colorMode === "replace"
+ colorStyle &&
+ colorStyle.colorMode &&
+ colorStyle.colorMode !== "none"
) {
- tempCtx.globalCompositeOperation = "source-in";
- tempCtx.fillStyle = colorStyle.tintColor || "#ffffff";
- tempCtx.globalAlpha = colorStyle.tintOpacity ?? 1;
- tempCtx.fillRect(0, 0, svgWidth, svgHeight);
+ if (
+ colorStyle.colorMode === "tint" ||
+ colorStyle.colorMode === "replace"
+ ) {
+ tempCtx.globalCompositeOperation = "source-in";
+ tempCtx.fillStyle = colorStyle.tintColor || "#ffffff";
+ tempCtx.globalAlpha = colorStyle.tintOpacity ?? 1;
+ tempCtx.fillRect(0, 0, paddedWidth, paddedHeight);
+ }
}
- }
- if (clipRect) {
- ctx.save();
- ctx.beginPath();
- ctx.rect(
- -svgWidth / 2 + clipRect.x,
- -svgHeight / 2 + clipRect.y,
- clipRect.width,
- clipRect.height,
- );
- ctx.clip();
- }
+ if (drawProgress > 0 && drawProgress < 1) {
+ const dashLength = Math.max(renderWidth, renderHeight) * 4;
+ const offset = dashLength * (1 - drawProgress);
+ tempCtx.globalCompositeOperation = "destination-in";
+ tempCtx.strokeStyle = "#000";
+ tempCtx.lineWidth = Math.max(renderWidth, renderHeight);
+ tempCtx.setLineDash([dashLength]);
+ tempCtx.lineDashOffset = offset;
+ tempCtx.strokeRect(svgPad, svgPad, renderWidth, renderHeight);
+ tempCtx.setLineDash([]);
+ }
- // Draw animation: uses line dash offset to progressively reveal the SVG
- // As drawProgress goes from 0 to 1, the offset decreases, revealing more of the image
- if (drawProgress > 0 && drawProgress < 1) {
- const dashLength = Math.max(svgWidth, svgHeight) * 4;
- const offset = dashLength * (1 - drawProgress);
- tempCtx.globalCompositeOperation = "destination-in";
- tempCtx.strokeStyle = "#000";
- tempCtx.lineWidth = Math.max(svgWidth, svgHeight);
- tempCtx.setLineDash([dashLength]);
- tempCtx.lineDashOffset = offset;
- tempCtx.strokeRect(0, 0, svgWidth, svgHeight);
- tempCtx.setLineDash([]);
+ ctx.drawImage(
+ tempCanvas,
+ -(drawWidth / 2 + padDraw),
+ -(drawHeight / 2 + padDraw),
+ drawWidth + padDraw * 2,
+ drawHeight + padDraw * 2,
+ );
}
-
+ } else {
ctx.drawImage(
- tempCanvas,
- -svgWidth / 2,
- -svgHeight / 2,
- svgWidth,
- svgHeight,
+ img,
+ -(drawWidth / 2 + padDraw),
+ -(drawHeight / 2 + padDraw),
+ drawWidth + padDraw * 2,
+ drawHeight + padDraw * 2,
);
+ }
- if (clipRect) {
- ctx.restore();
- }
+ if (clipRect) {
+ ctx.restore();
}
}
diff --git a/packages/core/src/graphics/graphics-engine.ts b/packages/core/src/graphics/graphics-engine.ts
index 0386d0dd..7c78ccfe 100644
--- a/packages/core/src/graphics/graphics-engine.ts
+++ b/packages/core/src/graphics/graphics-engine.ts
@@ -561,48 +561,140 @@ export class GraphicsEngine {
* Note: SVG must be converted to image blob first due to canvas limitations with direct SVG rendering.
* URL.createObjectURL is revoked in finally to prevent memory leaks.
*/
+ private getAnimatedSVGSourceInset(
+ animatedState: AnimatedGraphicState,
+ width: number,
+ height: number,
+ ): number {
+ const transformIsAnimating =
+ Math.abs(animatedState.scale - 1) > 0.001 ||
+ Math.abs((animatedState.scaleX ?? 1) - 1) > 0.001 ||
+ Math.abs((animatedState.scaleY ?? 1) - 1) > 0.001 ||
+ Math.abs(animatedState.offsetX) > 0.001 ||
+ Math.abs(animatedState.offsetY) > 0.001 ||
+ Math.abs(animatedState.rotation) > 0.001;
+
+ if (!transformIsAnimating || width <= 2 || height <= 2) {
+ return 0;
+ }
+
+ const desiredInset = Math.max(
+ 2,
+ Math.ceil(Math.min(width, height) * 0.0125),
+ );
+ const maxInset = Math.floor((Math.min(width, height) - 1) / 2);
+ return Math.min(16, desiredInset, maxInset);
+ }
+
private async renderSVG(
ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
svg: SVGClip,
- _width: number,
- _height: number,
+ width: number,
+ height: number,
animatedState: AnimatedGraphicState,
): Promise<void> {
- const svgContent = svg.svgContent;
+ const svgAspect = svg.viewBox.width / svg.viewBox.height;
+ const scaleX = animatedState.transform.scale.x;
+ const scaleY = animatedState.transform.scale.y;
+ const maxScale = Math.max(Math.abs(scaleX), Math.abs(scaleY), 1);
+ const rasterScale = Math.ceil(maxScale * 2) / 2;
+
+ let renderWidth: number;
+ let renderHeight: number;
+ if (svgAspect > 1) {
+ renderWidth = Math.ceil(width * rasterScale);
+ renderHeight = Math.ceil(renderWidth / svgAspect);
+ } else {
+ renderHeight = Math.ceil(height * rasterScale);
+ renderWidth = Math.ceil(renderHeight * svgAspect);
+ }
+
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(svg.svgContent, "image/svg+xml");
+ const svgElement = doc.querySelector("svg");
+ if (svgElement) {
+ svgElement.setAttribute("width", String(renderWidth));
+ svgElement.setAttribute("height", String(renderHeight));
+ }
+ const serializer = new XMLSerializer();
+ const scaledSvgContent = svgElement
+ ? serializer.serializeToString(svgElement)
+ : svg.svgContent;
- const blob = new Blob([svgContent], { type: "image/svg+xml" });
+ const blob = new Blob([scaledSvgContent], { type: "image/svg+xml" });
const url = URL.createObjectURL(blob);
try {
const img = await this.loadImage(url);
- const svgWidth = svg.viewBox.width;
- const svgHeight = svg.viewBox.height;
-
if (animatedState.blur > 0) {
ctx.filter = `blur(${animatedState.blur}px)`;
}
+ const drawWidth = renderWidth / rasterScale;
+ const drawHeight = renderHeight / rasterScale;
+ const sourceInset = this.getAnimatedSVGSourceInset(
+ animatedState,
+ renderWidth,
+ renderHeight,
+ );
+ const sourceWidth = Math.max(1, renderWidth - sourceInset * 2);
+ const sourceHeight = Math.max(1, renderHeight - sourceInset * 2);
+ const drawX = -drawWidth / 2 + sourceInset / rasterScale;
+ const drawY = -drawHeight / 2 + sourceInset / rasterScale;
+ const sourceDrawWidth = sourceWidth / rasterScale;
+ const sourceDrawHeight = sourceHeight / rasterScale;
+
if (
svg.colorStyle &&
svg.colorStyle.colorMode !== "none" &&
(svg.colorStyle.colorMode === "tint" ||
svg.colorStyle.colorMode === "replace")
) {
- const tempCanvas = new OffscreenCanvas(svgWidth, svgHeight);
+ const tempCanvas = new OffscreenCanvas(renderWidth, renderHeight);
const tempCtx = tempCanvas.getContext("2d");
if (tempCtx) {
- tempCtx.drawImage(img, 0, 0, svgWidth, svgHeight);
+ tempCtx.drawImage(img, 0, 0, renderWidth, renderHeight);
tempCtx.globalCompositeOperation = "source-in";
tempCtx.fillStyle = svg.colorStyle.tintColor || "#ffffff";
tempCtx.globalAlpha = svg.colorStyle.tintOpacity ?? 1;
- tempCtx.fillRect(0, 0, svgWidth, svgHeight);
- ctx.drawImage(tempCanvas, -svgWidth / 2, -svgHeight / 2);
+ tempCtx.fillRect(0, 0, renderWidth, renderHeight);
+ ctx.drawImage(
+ tempCanvas,
+ sourceInset,
+ sourceInset,
+ sourceWidth,
+ sourceHeight,
+ drawX,
+ drawY,
+ sourceDrawWidth,
+ sourceDrawHeight,
+ );
} else {
- ctx.drawImage(img, -svgWidth / 2, -svgHeight / 2, svgWidth, svgHeight);
+ ctx.drawImage(
+ img,
+ sourceInset,
+ sourceInset,
+ sourceWidth,
+ sourceHeight,
+ drawX,
+ drawY,
+ sourceDrawWidth,
+ sourceDrawHeight,
+ );
}
} else {
- ctx.drawImage(img, -svgWidth / 2, -svgHeight / 2, svgWidth, svgHeight);
+ ctx.drawImage(
+ img,
+ sourceInset,
+ sourceInset,
+ sourceWidth,
+ sourceHeight,
+ drawX,
+ drawY,
+ sourceDrawWidth,
+ sourceDrawHeight,
+ );
}
ctx.filter = "none";