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(image): add defensive checks for edge cases in Phase 5/6 tools

Augustus Otu47e6d0909fee
diff --git a/apps/image/src/tools/transform/free-transform.ts b/apps/image/src/tools/transform/free-transform.ts
index 9cf3957f..f0f520a9 100644
--- a/apps/image/src/tools/transform/free-transform.ts
+++ b/apps/image/src/tools/transform/free-transform.ts
@@ -107,10 +107,14 @@ export function createRotateMatrix(angle: number): TransformMatrix {
}
export function createSkewMatrix(skewX: number, skewY: number): TransformMatrix {
+ const maxSkew = Math.PI / 2 - 0.01;
+ const clampedSkewX = Math.max(-maxSkew, Math.min(maxSkew, skewX));
+ const clampedSkewY = Math.max(-maxSkew, Math.min(maxSkew, skewY));
+
return {
a: 1,
- b: Math.tan(skewY),
- c: Math.tan(skewX),
+ b: Math.tan(clampedSkewY),
+ c: Math.tan(clampedSkewX),
d: 1,
e: 0,
f: 0,
diff --git a/apps/image/src/tools/transform/liquify.ts b/apps/image/src/tools/transform/liquify.ts
index 144270f4..88ea695d 100644
--- a/apps/image/src/tools/transform/liquify.ts
+++ b/apps/image/src/tools/transform/liquify.ts
@@ -52,13 +52,14 @@ export function createDisplacementMesh(
height: number,
cellSize: number = 8
): DisplacementMesh {
- const cols = Math.ceil(width / cellSize) + 1;
- const rows = Math.ceil(height / cellSize) + 1;
+ const safeCellSize = Math.max(1, cellSize);
+ const cols = Math.ceil(width / safeCellSize) + 1;
+ const rows = Math.ceil(height / safeCellSize) + 1;
return {
width,
height,
- cellSize,
+ cellSize: safeCellSize,
cols,
rows,
displacements: new Float32Array(cols * rows * 2),
@@ -127,7 +128,7 @@ function setFrozen(mesh: DisplacementMesh, col: number, row: number, frozen: boo
}
function brushFalloff(distance: number, radius: number, density: number): number {
- if (distance >= radius) return 0;
+ if (radius <= 0 || distance >= radius) return 0;
const normalized = distance / radius;
const densityFactor = density / 100;
diff --git a/apps/image/src/tools/transform/warp.ts b/apps/image/src/tools/transform/warp.ts
index 3c429c03..adb520b3 100644
--- a/apps/image/src/tools/transform/warp.ts
+++ b/apps/image/src/tools/transform/warp.ts
@@ -53,29 +53,31 @@ export function createWarpGrid(
rows: number = 4,
cols: number = 4
): WarpGrid {
+ const safeRows = Math.max(1, Math.round(rows));
+ const safeCols = Math.max(1, Math.round(cols));
const points: WarpPoint[][] = [];
- for (let row = 0; row <= rows; row++) {
+ for (let row = 0; row <= safeRows; row++) {
const rowPoints: WarpPoint[] = [];
- for (let col = 0; col <= cols; col++) {
- const x = (col / cols) * width;
- const y = (row / rows) * height;
+ for (let col = 0; col <= safeCols; col++) {
+ const x = (col / safeCols) * width;
+ const y = (row / safeRows) * height;
- const handleOffset = Math.min(width / cols, height / rows) * 0.3;
+ const handleOffset = Math.min(width / safeCols, height / safeRows) * 0.3;
rowPoints.push({
x,
y,
handleLeft: col > 0 ? { x: x - handleOffset, y } : null,
- handleRight: col < cols ? { x: x + handleOffset, y } : null,
+ handleRight: col < safeCols ? { x: x + handleOffset, y } : null,
handleTop: row > 0 ? { x, y: y - handleOffset } : null,
- handleBottom: row < rows ? { x, y: y + handleOffset } : null,
+ handleBottom: row < safeRows ? { x, y: y + handleOffset } : null,
});
}
points.push(rowPoints);
}
- return { rows, cols, points };
+ return { rows: safeRows, cols: safeCols, points };
}
export function applyWarpPreset(
@@ -272,11 +274,15 @@ function bicubicInterpolate(
): { x: number; y: number } {
const { rows, cols, points } = grid;
+ if (rows < 1 || cols < 1 || points.length === 0 || points[0].length === 0) {
+ return { x: 0, y: 0 };
+ }
+
const colF = u * cols;
const rowF = v * rows;
- const col = Math.min(Math.floor(colF), cols - 1);
- const row = Math.min(Math.floor(rowF), rows - 1);
+ const col = Math.max(0, Math.min(Math.floor(colF), cols - 1));
+ const row = Math.max(0, Math.min(Math.floor(rowF), rows - 1));
const localU = colF - col;
const localV = rowF - row;
diff --git a/apps/image/src/tools/vector/path-operations.ts b/apps/image/src/tools/vector/path-operations.ts
index 2ed9862a..3b995fe2 100644
--- a/apps/image/src/tools/vector/path-operations.ts
+++ b/apps/image/src/tools/vector/path-operations.ts
@@ -220,7 +220,8 @@ export function offsetPath(path: VectorPath, distance: number): VectorPath {
const cross = normPrev.x * normNext.y - normPrev.y * normNext.x;
const dot = normPrev.x * normNext.x + normPrev.y * normNext.y;
const halfAngle = Math.acos(Math.max(-1, Math.min(1, dot))) / 2;
- const miterLength = distance / Math.cos(halfAngle);
+ const cosHalfAngle = Math.cos(halfAngle);
+ const miterLength = cosHalfAngle > 0.01 ? distance / cosHalfAngle : distance * 10;
const sign = cross >= 0 ? 1 : -1;
const offsetX = (bisector.x / bisectorLen) * miterLength * sign;