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 null safety for layer effects and text shadow

Augustus Otu51ee421ffb3b
diff --git a/apps/image/src/components/editor/canvas/Canvas.tsx b/apps/image/src/components/editor/canvas/Canvas.tsx
index fce1bad4..d30fceeb 100644
--- a/apps/image/src/components/editor/canvas/Canvas.tsx
+++ b/apps/image/src/components/editor/canvas/Canvas.tsx
@@ -485,7 +485,9 @@ function renderLayer(
layer: Layer,
project: { assets: Record<string, { dataUrl?: string; blobUrl?: string }> }
) {
- const { transform, shadow, glow } = layer;
+ const { transform } = layer;
+ const shadow = layer.shadow ?? { enabled: false, color: 'rgba(0, 0, 0, 0.5)', blur: 10, offsetX: 0, offsetY: 4 };
+ const glow = layer.glow ?? { enabled: false, color: '#ffffff', blur: 20, intensity: 1 };
ctx.save();
ctx.translate(transform.x, transform.y);
@@ -496,7 +498,7 @@ function renderLayer(
if (glow.enabled && glow.blur > 0) {
ctx.save();
ctx.shadowColor = glow.color;
- ctx.shadowBlur = glow.blur * glow.intensity;
+ ctx.shadowBlur = glow.blur * (glow.intensity ?? 1);
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
@@ -737,11 +739,12 @@ function renderTextLayer(ctx: CanvasRenderingContext2D, layer: TextLayer) {
}
}
- if (style.textShadow?.enabled) {
- ctx.shadowColor = style.textShadow.color;
- ctx.shadowBlur = style.textShadow.blur;
- ctx.shadowOffsetX = style.textShadow.offsetX;
- ctx.shadowOffsetY = style.textShadow.offsetY;
+ const textShadow = style.textShadow;
+ if (textShadow?.enabled) {
+ ctx.shadowColor = textShadow.color ?? 'rgba(0, 0, 0, 0.5)';
+ ctx.shadowBlur = textShadow.blur ?? 4;
+ ctx.shadowOffsetX = textShadow.offsetX ?? 0;
+ ctx.shadowOffsetY = textShadow.offsetY ?? 2;
}
lines.forEach((line, i) => {
@@ -759,7 +762,7 @@ function renderTextLayer(ctx: CanvasRenderingContext2D, layer: TextLayer) {
ctx.fillText(line, textX, y);
});
- if (style.textShadow?.enabled) {
+ if (textShadow?.enabled) {
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
diff --git a/apps/image/src/components/editor/inspector/ShapeSection.tsx b/apps/image/src/components/editor/inspector/ShapeSection.tsx
index 0808637a..399436c2 100644
--- a/apps/image/src/components/editor/inspector/ShapeSection.tsx
+++ b/apps/image/src/components/editor/inspector/ShapeSection.tsx
@@ -266,11 +266,12 @@ export function ShapeSection({ layer }: Props) {
<label className="text-xs font-medium">Corner Radius</label>
<button
onClick={() => {
- const currentRadius = layer.shapeStyle.cornerRadius;
+ const currentRadius = layer.shapeStyle.cornerRadius ?? 0;
+ const defaultCorners = { topLeft: 0, topRight: 0, bottomRight: 0, bottomLeft: 0 };
handleStyleChange({
individualCorners: !layer.shapeStyle.individualCorners,
corners: layer.shapeStyle.individualCorners
- ? layer.shapeStyle.corners
+ ? (layer.shapeStyle.corners ?? defaultCorners)
: {
topLeft: currentRadius,
topRight: currentRadius,
diff --git a/apps/image/src/components/editor/inspector/TextSection.tsx b/apps/image/src/components/editor/inspector/TextSection.tsx
index 280e8a29..97dcf7b9 100644
--- a/apps/image/src/components/editor/inspector/TextSection.tsx
+++ b/apps/image/src/components/editor/inspector/TextSection.tsx
@@ -441,20 +441,20 @@ export function TextSection({ layer }: Props) {
<div className="flex items-center gap-2">
<input
type="color"
- value={layer.style.textShadow.color.startsWith('rgba') ? '#000000' : layer.style.textShadow.color}
+ value={(layer.style.textShadow?.color ?? 'rgba(0, 0, 0, 0.5)').startsWith('rgba') ? '#000000' : layer.style.textShadow?.color ?? '#000000'}
onChange={(e) =>
handleStyleChange({
- textShadow: { ...layer.style.textShadow, color: e.target.value },
+ textShadow: { ...(layer.style.textShadow ?? { enabled: true, color: 'rgba(0, 0, 0, 0.5)', blur: 4, offsetX: 0, offsetY: 2 }), color: e.target.value },
})
}
className="w-8 h-8 rounded border border-input cursor-pointer"
/>
<input
type="text"
- value={layer.style.textShadow.color}
+ value={layer.style.textShadow?.color ?? 'rgba(0, 0, 0, 0.5)'}
onChange={(e) =>
handleStyleChange({
- textShadow: { ...layer.style.textShadow, color: e.target.value },
+ textShadow: { ...(layer.style.textShadow ?? { enabled: true, color: 'rgba(0, 0, 0, 0.5)', blur: 4, offsetX: 0, offsetY: 2 }), color: e.target.value },
})
}
className="flex-1 px-2 py-1.5 text-xs bg-background border border-input rounded-md focus:outline-none focus:ring-1 focus:ring-primary font-mono"
@@ -465,13 +465,13 @@ export function TextSection({ layer }: Props) {
<div>
<div className="flex items-center justify-between mb-1.5">
<label className="text-[10px] text-muted-foreground">Blur</label>
- <span className="text-[10px] text-muted-foreground">{layer.style.textShadow.blur}px</span>
+ <span className="text-[10px] text-muted-foreground">{layer.style.textShadow?.blur ?? 4}px</span>
</div>
<Slider
- value={[layer.style.textShadow.blur]}
+ value={[layer.style.textShadow?.blur ?? 4]}
onValueChange={([blur]) =>
handleStyleChange({
- textShadow: { ...layer.style.textShadow, blur },
+ textShadow: { ...(layer.style.textShadow ?? { enabled: true, color: 'rgba(0, 0, 0, 0.5)', blur: 4, offsetX: 0, offsetY: 2 }), blur },
})
}
min={0}
@@ -484,13 +484,13 @@ export function TextSection({ layer }: Props) {
<div>
<div className="flex items-center justify-between mb-1.5">
<label className="text-[10px] text-muted-foreground">Offset X</label>
- <span className="text-[10px] text-muted-foreground">{layer.style.textShadow.offsetX}px</span>
+ <span className="text-[10px] text-muted-foreground">{layer.style.textShadow?.offsetX ?? 0}px</span>
</div>
<Slider
- value={[layer.style.textShadow.offsetX]}
+ value={[layer.style.textShadow?.offsetX ?? 0]}
onValueChange={([offsetX]) =>
handleStyleChange({
- textShadow: { ...layer.style.textShadow, offsetX },
+ textShadow: { ...(layer.style.textShadow ?? { enabled: true, color: 'rgba(0, 0, 0, 0.5)', blur: 4, offsetX: 0, offsetY: 2 }), offsetX },
})
}
min={-30}
@@ -501,13 +501,13 @@ export function TextSection({ layer }: Props) {
<div>
<div className="flex items-center justify-between mb-1.5">
<label className="text-[10px] text-muted-foreground">Offset Y</label>
- <span className="text-[10px] text-muted-foreground">{layer.style.textShadow.offsetY}px</span>
+ <span className="text-[10px] text-muted-foreground">{layer.style.textShadow?.offsetY ?? 2}px</span>
</div>
<Slider
- value={[layer.style.textShadow.offsetY]}
+ value={[layer.style.textShadow?.offsetY ?? 2]}
onValueChange={([offsetY]) =>
handleStyleChange({
- textShadow: { ...layer.style.textShadow, offsetY },
+ textShadow: { ...(layer.style.textShadow ?? { enabled: true, color: 'rgba(0, 0, 0, 0.5)', blur: 4, offsetX: 0, offsetY: 2 }), offsetY },
})
}
min={-30}