OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
feat: implement aspect ratio preservation in rendering engines
Augustus Otu43bb959c4728
diff --git a/packages/core/src/video/chroma-key-engine.ts b/packages/core/src/video/chroma-key-engine.tsindex a3a00269..8d2dfe08 100644--- a/packages/core/src/video/chroma-key-engine.ts+++ b/packages/core/src/video/chroma-key-engine.ts@@ -56,6 +56,19 @@ export class ChromaKeyEngine {this.ctx = this.canvas.getContext("2d", { willReadFrequently: true })!;}+ // Match the working canvas to the incoming frame so the source is processed+ // at its native resolution/aspect (the subsequent drawImage(...,this.width,+ // this.height) calls become 1:1) instead of being stretched to the fixed+ // engine size. Output is then aspect-correct and fit downstream.+ private resizeTo(width: number, height: number): void {+ if (width <= 0 || height <= 0) return;+ if (this.width === width && this.height === height) return;+ this.width = width;+ this.height = height;+ this.canvas.width = width;+ this.canvas.height = height;+ }+enableChromaKey(clipId: string): void {const existing = this.clipSettings.get(clipId);if (existing) {@@ -93,6 +106,7 @@ export class ChromaKeyEngine {}sampleKeyColor(image: ImageBitmap, x: number, y: number): RGB {+ this.resizeTo(image.width, image.height);// Draw image to canvasthis.ctx.clearRect(0, 0, this.width, this.height);this.ctx.drawImage(image, 0, 0, this.width, this.height);@@ -164,6 +178,7 @@ export class ChromaKeyEngine {settings: ChromaKeySettings,startTime: number = performance.now(),): Promise<ChromaKeyResult> {+ this.resizeTo(image.width, image.height);// Draw source imagethis.ctx.clearRect(0, 0, this.width, this.height);this.ctx.drawImage(image, 0, 0, this.width, this.height);@@ -206,6 +221,7 @@ export class ChromaKeyEngine {}getMatte(image: ImageBitmap, clipId: string): ChromaKeyMatte {+ this.resizeTo(image.width, image.height);const settings = this.clipSettings.get(clipId);if (!settings || !settings.enabled) {@@ -343,6 +359,7 @@ export class ChromaKeyEngine {foreground: ImageBitmap,background: ImageBitmap,): Promise<ImageBitmap> {+ this.resizeTo(foreground.width, foreground.height);// Draw background firstthis.ctx.clearRect(0, 0, this.width, this.height);this.ctx.drawImage(background, 0, 0, this.width, this.height);@@ -375,6 +392,7 @@ export class ChromaKeyEngine {}countTransparentPixels(image: ImageBitmap): number {+ this.resizeTo(image.width, image.height);this.ctx.clearRect(0, 0, this.width, this.height);this.ctx.drawImage(image, 0, 0, this.width, this.height);diff --git a/packages/core/src/video/color-grading-engine.ts b/packages/core/src/video/color-grading-engine.tsindex 8651e3f0..51a52a3a 100644--- a/packages/core/src/video/color-grading-engine.ts+++ b/packages/core/src/video/color-grading-engine.ts@@ -405,6 +405,20 @@ export class ColorGradingEngine {const gl = this.gl!;const shader = this.shaders.get("colorWheels")!;+ // Render at the source frame's own resolution so a clip whose aspect+ // differs from the project canvas keeps its orientation. Rendering into the+ // fixed engine canvas stretches the frame to fill the GL viewport, and the+ // downstream contain re-fit would then bake that distortion in.+ if (+ this.canvas &&+ (this.canvas.width !== image.width || this.canvas.height !== image.height)+ ) {+ this.canvas.width = image.width;+ this.canvas.height = image.height;+ this.width = image.width;+ this.height = image.height;+ }+// Upload source imageconst sourceTexture = this.uploadTexture(image);diff --git a/packages/core/src/video/mask-engine.ts b/packages/core/src/video/mask-engine.tsindex bf85f3d2..acb74297 100644--- a/packages/core/src/video/mask-engine.ts+++ b/packages/core/src/video/mask-engine.ts@@ -404,6 +404,21 @@ export class MaskEngine {this.maskCtx = this.maskCanvas.getContext("2d")!;}+ // Match the working canvases to the incoming frame so the source is masked at+ // its native resolution/aspect (the drawImage(...,this.width,this.height)+ // calls become 1:1) instead of being stretched to the fixed engine size. The+ // normalized 0..1 mask path then scales to the frame and stays aligned.+ private resizeTo(width: number, height: number): void {+ if (width <= 0 || height <= 0) return;+ if (this.width === width && this.height === height) return;+ this.width = width;+ this.height = height;+ this.canvas.width = width;+ this.canvas.height = height;+ this.maskCanvas.width = width;+ this.maskCanvas.height = height;+ }+createShapeMask(clipId: string, shape: MaskShape): Mask {const path = shapeToPath(shape);const mask: Mask = {@@ -623,6 +638,7 @@ export class MaskEngine {time?: number,): Promise<MaskResult> {const startTime = performance.now();+ this.resizeTo(image.width, image.height);const path =time !== undefined? this.getMaskAtTime(mask.id, time) || mask.path@@ -665,6 +681,7 @@ export class MaskEngine {mask: MaskDefinition,): Promise<MaskResult> {const startTime = performance.now();+ this.resizeTo(image.width, image.height);this.ctx.clearRect(0, 0, this.width, this.height);this.maskCtx.clearRect(0, 0, this.width, this.height);this.generateMaskShape(mask);diff --git a/packages/core/src/video/transition-engine.ts b/packages/core/src/video/transition-engine.tsindex 292a3fd9..e489a689 100644--- a/packages/core/src/video/transition-engine.ts+++ b/packages/core/src/video/transition-engine.ts@@ -28,6 +28,8 @@ export class TransitionEngine {private width: number;private height: number;private initialized = false;+ private scratch: OffscreenCanvas | null = null;+ private scratchCtx: OffscreenCanvasRenderingContext2D | null = null;constructor(config: TransitionEngineConfig) {this.width = config.width;@@ -60,6 +62,57 @@ export class TransitionEngine {return this.ctx;}+ // Letterbox (contain) a source frame into an engine-sized frame so the+ // per-transition geometry — which assumes inputs already fill the canvas —+ // preserves the source aspect ratio instead of stretching it. Returns the+ // original frame untouched when it is already engine-sized (e.g. the+ // scrub path pre-letterboxes), so callers must only close the result when+ // it differs from the input.+ private async fitToCanvas(source: ImageBitmap): Promise<ImageBitmap> {+ if (source.width === this.width && source.height === this.height) {+ return source;+ }+ if (typeof OffscreenCanvas === "undefined") {+ return source;+ }+ const sourceWidth = source.width;+ const sourceHeight = source.height;+ if (sourceWidth <= 0 || sourceHeight <= 0) {+ return source;+ }++ if (+ !this.scratch ||+ this.scratch.width !== this.width ||+ this.scratch.height !== this.height+ ) {+ this.scratch = new OffscreenCanvas(this.width, this.height);+ this.scratchCtx = this.scratch.getContext("2d");+ }+ const scratchCtx = this.scratchCtx;+ if (!scratchCtx) {+ return source;+ }++ const sourceAspect = sourceWidth / sourceHeight;+ const canvasAspect = this.width / this.height;+ let drawWidth: number;+ let drawHeight: number;+ if (sourceAspect > canvasAspect) {+ drawWidth = this.width;+ drawHeight = this.width / sourceAspect;+ } else {+ drawHeight = this.height;+ drawWidth = this.height * sourceAspect;+ }+ const drawX = (this.width - drawWidth) / 2;+ const drawY = (this.height - drawHeight) / 2;++ scratchCtx.clearRect(0, 0, this.width, this.height);+ scratchCtx.drawImage(source, drawX, drawY, drawWidth, drawHeight);+ return await createImageBitmap(this.scratch);+ }+async renderTransition(outgoingFrame: ImageBitmap,incomingFrame: ImageBitmap,@@ -77,15 +130,24 @@ export class TransitionEngine {clampedProgress,transition.params.curve as string,);++ // Letterbox both inputs to the engine canvas first so a clip whose aspect+ // differs from the project (e.g. a portrait clip in a landscape project)+ // keeps its orientation through the transition instead of being stretched+ // to fill. The scrub path already passes engine-sized frames, so this is a+ // no-op there; the multitrack-preview and export paths pass native frames.+ const outgoing = await this.fitToCanvas(outgoingFrame);+ const incoming = await this.fitToCanvas(incomingFrame);+this.ctx.clearRect(0, 0, this.width, this.height);switch (transition.type) {case "crossfade":- await this.renderCrossfade(outgoingFrame, incomingFrame, easedProgress);+ await this.renderCrossfade(outgoing, incoming, easedProgress);break;case "dipToBlack":await this.renderDipToColor(- outgoingFrame,- incomingFrame,+ outgoing,+ incoming,easedProgress,"black",(transition.params.holdDuration as number) || 0,@@ -93,8 +155,8 @@ export class TransitionEngine {break;case "dipToWhite":await this.renderDipToColor(- outgoingFrame,- incomingFrame,+ outgoing,+ incoming,easedProgress,"white",(transition.params.holdDuration as number) || 0,@@ -102,8 +164,8 @@ export class TransitionEngine {break;case "wipe":await this.renderWipe(- outgoingFrame,- incomingFrame,+ outgoing,+ incoming,easedProgress,(transition.params.direction as string) || "left",(transition.params.softness as number) || 0,@@ -111,8 +173,8 @@ export class TransitionEngine {break;case "slide":await this.renderSlide(- outgoingFrame,- incomingFrame,+ outgoing,+ incoming,easedProgress,(transition.params.direction as string) || "left",(transition.params.pushOut as boolean) || false,@@ -120,8 +182,8 @@ export class TransitionEngine {break;case "zoom":await this.renderZoom(- outgoingFrame,- incomingFrame,+ outgoing,+ incoming,easedProgress,(transition.params.scale as number) || 2,(transition.params.center as { x: number; y: number }) || {@@ -132,18 +194,23 @@ export class TransitionEngine {break;case "push":await this.renderPush(- outgoingFrame,- incomingFrame,+ outgoing,+ incoming,easedProgress,(transition.params.direction as string) || "left",);break;default:- await this.renderCrossfade(outgoingFrame, incomingFrame, easedProgress);+ await this.renderCrossfade(outgoing, incoming, easedProgress);}const frame = await createImageBitmap(this.canvas);+ // fitToCanvas returns the original when it is already engine-sized, so+ // only close the letterboxed copies we actually allocated.+ if (outgoing !== outgoingFrame) outgoing.close();+ if (incoming !== incomingFrame) incoming.close();+return {frame,processingTime: performance.now() - startTime,