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): address code review issues in zoom, rulers, export, and canvas

Augustus Otuf2eb788f12d2
diff --git a/apps/image/src/components/editor/ExportDialog.tsx b/apps/image/src/components/editor/ExportDialog.tsx
index 630a92d0..436935a6 100644
--- a/apps/image/src/components/editor/ExportDialog.tsx
+++ b/apps/image/src/components/editor/ExportDialog.tsx
@@ -100,11 +100,11 @@ export function ExportDialog({ open, onClose }: ExportDialogProps) {
}, [artboard, sizeMode, effectiveScale, customWidth, customHeight]);
useEffect(() => {
- if (artboard && customWidth === 0 && customHeight === 0) {
+ if (artboard) {
setCustomWidth(artboard.size.width);
setCustomHeight(artboard.size.height);
}
- }, [artboard, customWidth, customHeight]);
+ }, [artboard?.id]);
const handleCustomWidthChange = (newWidth: number) => {
setCustomWidth(newWidth);
diff --git a/apps/image/src/components/editor/canvas/Canvas.tsx b/apps/image/src/components/editor/canvas/Canvas.tsx
index 12ca28da..8fec7312 100644
--- a/apps/image/src/components/editor/canvas/Canvas.tsx
+++ b/apps/image/src/components/editor/canvas/Canvas.tsx
@@ -661,43 +661,46 @@ function renderTextLayer(ctx: CanvasRenderingContext2D, layer: TextLayer) {
}
let fillStyle: string | CanvasGradient = style.color;
- if (style.fillType === 'gradient' && style.gradient) {
- const textWidth = Math.max(...lines.map((line) => ctx.measureText(line).width));
+ if (style.fillType === 'gradient' && style.gradient && lines.length > 0) {
+ const lineWidths = lines.map((line) => ctx.measureText(line).width);
+ const textWidth = lineWidths.length > 0 ? Math.max(...lineWidths) : 0;
const textHeight = lines.length * lineHeight;
- let gradientStartX = 0;
- if (style.textAlign === 'center') gradientStartX = (transform.width - textWidth) / 2;
- else if (style.textAlign === 'right') gradientStartX = transform.width - textWidth;
-
- if (style.gradient.type === 'linear') {
- const angleRad = (style.gradient.angle * Math.PI) / 180;
- const cos = Math.cos(angleRad);
- const sin = Math.sin(angleRad);
- const halfWidth = textWidth / 2;
- const halfHeight = textHeight / 2;
- const len = Math.abs(halfWidth * cos) + Math.abs(halfHeight * sin);
-
- const centerX = gradientStartX + halfWidth;
- const centerY = halfHeight;
- const gradient = ctx.createLinearGradient(
- centerX - len * cos,
- centerY - len * sin,
- centerX + len * cos,
- centerY + len * sin
- );
- style.gradient.stops.forEach((stop) => {
- gradient.addColorStop(stop.offset, stop.color);
- });
- fillStyle = gradient;
- } else {
- const centerX = gradientStartX + textWidth / 2;
- const centerY = textHeight / 2;
- const radius = Math.max(textWidth, textHeight) / 2;
- const gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius);
- style.gradient.stops.forEach((stop) => {
- gradient.addColorStop(stop.offset, stop.color);
- });
- fillStyle = gradient;
+ if (textWidth > 0 && textHeight > 0) {
+ let gradientStartX = 0;
+ if (style.textAlign === 'center') gradientStartX = (transform.width - textWidth) / 2;
+ else if (style.textAlign === 'right') gradientStartX = transform.width - textWidth;
+
+ if (style.gradient.type === 'linear') {
+ const angleRad = (style.gradient.angle * Math.PI) / 180;
+ const cos = Math.cos(angleRad);
+ const sin = Math.sin(angleRad);
+ const halfWidth = textWidth / 2;
+ const halfHeight = textHeight / 2;
+ const len = Math.abs(halfWidth * cos) + Math.abs(halfHeight * sin);
+
+ const centerX = gradientStartX + halfWidth;
+ const centerY = halfHeight;
+ const gradient = ctx.createLinearGradient(
+ centerX - len * cos,
+ centerY - len * sin,
+ centerX + len * cos,
+ centerY + len * sin
+ );
+ style.gradient.stops.forEach((stop) => {
+ gradient.addColorStop(stop.offset, stop.color);
+ });
+ fillStyle = gradient;
+ } else {
+ const centerX = gradientStartX + textWidth / 2;
+ const centerY = textHeight / 2;
+ const radius = Math.max(textWidth, textHeight) / 2;
+ const gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius);
+ style.gradient.stops.forEach((stop) => {
+ gradient.addColorStop(stop.offset, stop.color);
+ });
+ fillStyle = gradient;
+ }
}
}
diff --git a/apps/image/src/components/editor/canvas/Rulers.tsx b/apps/image/src/components/editor/canvas/Rulers.tsx
index c06ba8a8..1c5acb25 100644
--- a/apps/image/src/components/editor/canvas/Rulers.tsx
+++ b/apps/image/src/components/editor/canvas/Rulers.tsx
@@ -25,6 +25,7 @@ export function Rulers({ containerWidth, containerHeight }: RulersProps) {
useEffect(() => {
if (!showRulers || !artboard) return;
+ if (containerWidth <= RULER_SIZE || containerHeight <= RULER_SIZE) return;
const hCanvas = horizontalRef.current;
const vCanvas = verticalRef.current;
@@ -34,10 +35,10 @@ export function Rulers({ containerWidth, containerHeight }: RulersProps) {
const vCtx = vCanvas.getContext('2d');
if (!hCtx || !vCtx) return;
- hCanvas.width = containerWidth;
+ hCanvas.width = containerWidth - RULER_SIZE;
hCanvas.height = RULER_SIZE;
vCanvas.width = RULER_SIZE;
- vCanvas.height = containerHeight;
+ vCanvas.height = containerHeight - RULER_SIZE;
const centerX = containerWidth / 2 + panX;
const centerY = containerHeight / 2 + panY;
diff --git a/apps/image/src/components/editor/toolbar/ZoomControl.tsx b/apps/image/src/components/editor/toolbar/ZoomControl.tsx
index c73f9596..67763d09 100644
--- a/apps/image/src/components/editor/toolbar/ZoomControl.tsx
+++ b/apps/image/src/components/editor/toolbar/ZoomControl.tsx
@@ -25,7 +25,8 @@ export function ZoomControl() {
const { project } = useProjectStore();
const handleZoomToFit = () => {
- const artboard = project?.artboards.find((a) => a.id === project.activeArtboardId);
+ const activeId = project?.activeArtboardId;
+ const artboard = activeId ? project?.artboards.find((a) => a.id === activeId) : null;
if (!artboard) {
resetView();
return;
@@ -34,6 +35,11 @@ export function ZoomControl() {
const viewportWidth = window.innerWidth - 600;
const viewportHeight = window.innerHeight - 200;
+ if (artboard.size.width <= 0 || artboard.size.height <= 0) {
+ resetView();
+ return;
+ }
+
const fitZoom = Math.min(
viewportWidth / artboard.size.width,
viewportHeight / artboard.size.height,
@@ -44,7 +50,8 @@ export function ZoomControl() {
};
const handleZoomToFill = () => {
- const artboard = project?.artboards.find((a) => a.id === project.activeArtboardId);
+ const activeId = project?.activeArtboardId;
+ const artboard = activeId ? project?.artboards.find((a) => a.id === activeId) : null;
if (!artboard) {
resetView();
return;
@@ -53,6 +60,11 @@ export function ZoomControl() {
const viewportWidth = window.innerWidth - 600;
const viewportHeight = window.innerHeight - 200;
+ if (artboard.size.width <= 0 || artboard.size.height <= 0) {
+ resetView();
+ return;
+ }
+
const fillZoom = Math.max(
viewportWidth / artboard.size.width,
viewportHeight / artboard.size.height