OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
feat(image): add gradient fill support for text layers
Augustus Otub182562c8776
diff --git a/apps/image/src/components/editor/canvas/Canvas.tsx b/apps/image/src/components/editor/canvas/Canvas.tsxindex 8bdccfa7..12ca28da 100644--- a/apps/image/src/components/editor/canvas/Canvas.tsx+++ b/apps/image/src/components/editor/canvas/Canvas.tsx@@ -660,6 +660,47 @@ function renderTextLayer(ctx: CanvasRenderingContext2D, layer: TextLayer) {ctx.fill();}+ let fillStyle: string | CanvasGradient = style.color;+ if (style.fillType === 'gradient' && style.gradient) {+ const textWidth = Math.max(...lines.map((line) => ctx.measureText(line).width));+ 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;+ }+ }+lines.forEach((line, i) => {const y = i * lineHeight;@@ -671,7 +712,7 @@ function renderTextLayer(ctx: CanvasRenderingContext2D, layer: TextLayer) {ctx.strokeText(line, textX, y);}- ctx.fillStyle = style.color;+ ctx.fillStyle = fillStyle;ctx.fillText(line, textX, y);});}diff --git a/apps/image/src/components/editor/inspector/TextSection.tsx b/apps/image/src/components/editor/inspector/TextSection.tsxindex 160120be..d0e99731 100644--- a/apps/image/src/components/editor/inspector/TextSection.tsx+++ b/apps/image/src/components/editor/inspector/TextSection.tsx@@ -1,7 +1,8 @@import { useProjectStore } from '../../../stores/project-store';-import type { TextLayer, TextStyle } from '../../../types/project';+import type { TextLayer, TextStyle, TextFillType, Gradient } from '../../../types/project';import { AlignLeft, AlignCenter, AlignRight, Bold, Italic, Underline, CaseUpper, CaseLower, CaseSensitive, Strikethrough } from 'lucide-react';import { FontPicker } from '../../ui/FontPicker';+import { GradientPicker } from '../../ui/GradientPicker';import { Slider } from '@openreel/ui';interface Props {@@ -224,22 +225,64 @@ export function TextSection({ layer }: Props) {</button></div>- <div>- <label className="block text-[10px] text-muted-foreground mb-1">Color</label>- <div className="flex items-center gap-2">- <input- type="color"- value={layer.style.color}- onChange={(e) => handleStyleChange({ color: e.target.value })}- className="w-8 h-8 rounded border border-input cursor-pointer"- />- <input- type="text"- value={layer.style.color}- onChange={(e) => handleStyleChange({ 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"- />+ <div className="space-y-3 p-3 bg-secondary/50 rounded-lg">+ <div className="flex items-center justify-between">+ <label className="text-xs font-medium">Fill</label>+ <div className="flex gap-1">+ <button+ onClick={() => handleStyleChange({ fillType: 'solid' as TextFillType })}+ className={`px-2 py-1 text-[10px] rounded-md transition-colors ${+ (layer.style.fillType ?? 'solid') === 'solid'+ ? 'bg-primary text-primary-foreground'+ : 'bg-secondary text-secondary-foreground hover:bg-accent'+ }`}+ >+ Solid+ </button>+ <button+ onClick={() => {+ const gradient: Gradient = layer.style.gradient ?? {+ type: 'linear',+ angle: 90,+ stops: [+ { offset: 0, color: layer.style.color },+ { offset: 1, color: '#8b5cf6' },+ ],+ };+ handleStyleChange({ fillType: 'gradient' as TextFillType, gradient });+ }}+ className={`px-2 py-1 text-[10px] rounded-md transition-colors ${+ layer.style.fillType === 'gradient'+ ? 'bg-primary text-primary-foreground'+ : 'bg-secondary text-secondary-foreground hover:bg-accent'+ }`}+ >+ Gradient+ </button>+ </div></div>++ {(layer.style.fillType ?? 'solid') === 'solid' ? (+ <div className="flex items-center gap-2">+ <input+ type="color"+ value={layer.style.color}+ onChange={(e) => handleStyleChange({ color: e.target.value })}+ className="w-8 h-8 rounded border border-input cursor-pointer"+ />+ <input+ type="text"+ value={layer.style.color}+ onChange={(e) => handleStyleChange({ 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"+ />+ </div>+ ) : (+ <GradientPicker+ value={layer.style.gradient}+ onChange={(gradient) => handleStyleChange({ gradient })}+ />+ )}</div><div className="grid grid-cols-2 gap-2">diff --git a/apps/image/src/types/project.ts b/apps/image/src/types/project.tsindex af52f535..4439faf5 100644--- a/apps/image/src/types/project.ts+++ b/apps/image/src/types/project.ts@@ -77,6 +77,8 @@ export interface ImageLayer extends BaseLayer {flipVertical: boolean;}+export type TextFillType = 'solid' | 'gradient';+export interface TextStyle {fontFamily: string;fontSize: number;@@ -87,7 +89,9 @@ export interface TextStyle {verticalAlign: 'top' | 'middle' | 'bottom';lineHeight: number;letterSpacing: number;+ fillType: TextFillType;color: string;+ gradient: Gradient | null;strokeColor: string | null;strokeWidth: number;backgroundColor: string | null;@@ -268,7 +272,9 @@ export const DEFAULT_TEXT_STYLE: TextStyle = {verticalAlign: 'top',lineHeight: 1.4,letterSpacing: 0,+ fillType: 'solid',color: '#ffffff',+ gradient: null,strokeColor: null,strokeWidth: 0,backgroundColor: null,