// Layer model. Every layer owns a document-sized canvas. Text layers keep their // source properties and re-render that canvas whenever they change. import { createCanvas, cloneCanvas, uid } from './util.js'; export const BLEND_MODES = [ { id: 'normal', label: 'Normal', op: 'source-over' }, { id: 'multiply', label: 'Multiply', op: 'multiply' }, { id: 'screen', label: 'Screen', op: 'screen' }, { id: 'overlay', label: 'Overlay', op: 'overlay' }, { id: 'darken', label: 'Darken', op: 'darken' }, { id: 'lighten', label: 'Lighten', op: 'lighten' }, { id: 'color-dodge', label: 'Color Dodge', op: 'color-dodge' }, { id: 'color-burn', label: 'Color Burn', op: 'color-burn' }, { id: 'linear-dodge', label: 'Linear Dodge (Add)', op: 'lighter' }, { id: 'hard-light', label: 'Hard Light', op: 'hard-light' }, { id: 'soft-light', label: 'Soft Light', op: 'soft-light' }, { id: 'difference', label: 'Difference', op: 'difference' }, { id: 'exclusion', label: 'Exclusion', op: 'exclusion' }, { id: 'hue', label: 'Hue', op: 'hue' }, { id: 'saturation', label: 'Saturation', op: 'saturation' }, { id: 'color', label: 'Color', op: 'color' }, { id: 'luminosity', label: 'Luminosity', op: 'luminosity' }, ]; const OPS = Object.fromEntries(BLEND_MODES.map((m) => [m.id, m.op])); export const blendOp = (id) => OPS[id] || 'source-over'; export const DEFAULT_TEXT = { content: 'Text', x: 0, y: 0, fontFamily: 'Arial', fontSize: 48, bold: false, italic: false, underline: false, color: '#000000', align: 'left', lineHeight: 1.2, strokeWidth: 0, strokeColor: '#ffffff', shadow: false, }; /** Properties captured for undo/redo and saved to project files. */ const PROP_KEYS = ['name', 'visible', 'opacity', 'blendMode', 'locked', 'alphaLock', 'maskEnabled', 'type', 'clip', 'effects']; export class Layer { constructor(width, height, { name = 'Layer', type = 'raster', canvas = null } = {}) { this.id = uid('layer'); this.name = name; this.type = type; this.canvas = canvas || createCanvas(width, height); this.visible = true; this.opacity = 1; this.blendMode = 'normal'; this.locked = false; this.alphaLock = false; this.clip = false; // clip to the layer below (clipping mask) this.mask = null; // canvas, grayscale: white = visible this.maskEnabled = true; this.text = type === 'text' ? { ...DEFAULT_TEXT } : null; this.effects = null; // layer styles (see render/effects.js); replaced, never mutated // Transient, render-only state. this.dx = 0; // live move offset this.dy = 0; this.preview = null; // canvas that temporarily replaces content (filter previews) this.maskVersion = 0; this._maskAlpha = null; this._maskAlphaVersion = -1; this.thumbVersion = 0; } get width() { return this.canvas.width; } get height() { return this.canvas.height; } /** Context in default state (composite mode, alpha and transform reset). */ get ctx() { const ctx = this.canvas.getContext('2d'); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.globalAlpha = 1; ctx.globalCompositeOperation = 'source-over'; return ctx; } get isText() { return this.type === 'text'; } /** Bump after any pixel change so thumbnails refresh. */ touch() { this.thumbVersion++; } touchMask() { this.maskVersion++; this.thumbVersion++; } captureProps() { const p = {}; for (const k of PROP_KEYS) p[k] = this[k]; p.text = this.text ? { ...this.text } : null; return p; } applyProps(p) { for (const k of PROP_KEYS) if (k in p) this[k] = p[k]; this.text = p.text ? { ...p.text } : null; } /** Re-render a text layer from its properties. */ renderText() { if (!this.isText) return; renderTextToCanvas(this.canvas, this.text); this.touch(); } /** Alpha-only canvas derived from the grayscale mask (value = luminance * alpha). */ maskAlpha(source = null) { if (!this.mask) return null; if (!source && this._maskAlpha && this._maskAlphaVersion === this.maskVersion) return this._maskAlpha; const src = source || this.mask; const out = this._maskAlpha && this._maskAlpha.width === src.width && this._maskAlpha.height === src.height ? this._maskAlpha : createCanvas(src.width, src.height); const sctx = src.getContext('2d', { willReadFrequently: true }); const id = sctx.getImageData(0, 0, src.width, src.height); const d = id.data; for (let i = 0; i < d.length; i += 4) { const lum = (d[i] * 54 + d[i + 1] * 183 + d[i + 2] * 19) >> 8; d[i + 3] = (lum * d[i + 3] + 127) / 255; d[i] = d[i + 1] = d[i + 2] = 0; } out.getContext('2d').putImageData(id, 0, 0); this._maskAlpha = out; this._maskAlphaVersion = source ? -1 : this.maskVersion; return out; } /** Deep copy (new id). */ duplicate(nameSuffix = ' copy') { const l = new Layer(this.width, this.height, { name: this.name + nameSuffix, type: this.type, canvas: cloneCanvas(this.canvas) }); l.applyProps(this.captureProps()); l.name = this.name + nameSuffix; if (this.mask) l.mask = cloneCanvas(this.mask); return l; } } export function makeMaskCanvas(w, h, fill = '#ffffff') { const c = createCanvas(w, h); const ctx = c.getContext('2d'); ctx.fillStyle = fill; ctx.fillRect(0, 0, w, h); return c; } export function textFont(t, scale = 1) { const fam = /[\s,]/.test(t.fontFamily) && !/["']/.test(t.fontFamily) ? `"${t.fontFamily}"` : t.fontFamily; return `${t.italic ? 'italic ' : ''}${t.bold ? 'bold ' : ''}${t.fontSize * scale}px ${fam}, sans-serif`; } /** Measure the layout box of text properties: { x, y, w, h, lines:[{text,x,y,w}] }. */ export function layoutText(t, ctx = measureCtx()) { ctx.font = textFont(t); const lines = String(t.content).split('\n'); const lh = t.fontSize * t.lineHeight; const widths = lines.map((s) => ctx.measureText(s).width); const boxW = Math.max(1, ...widths); const out = lines.map((s, i) => { const lw = widths[i]; const ox = t.align === 'center' ? (boxW - lw) / 2 : t.align === 'right' ? boxW - lw : 0; return { text: s, x: t.x + ox, y: t.y + i * lh, w: lw }; }); return { x: t.x, y: t.y, w: boxW, h: Math.max(lh, lines.length * lh), lineHeight: lh, lines: out }; } let _measure = null; function measureCtx() { if (!_measure) _measure = createCanvas(1, 1).getContext('2d'); return _measure; } export function renderTextToCanvas(canvas, t) { const ctx = canvas.getContext('2d'); ctx.save(); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, canvas.width, canvas.height); const layout = layoutText(t, ctx); ctx.font = textFont(t); ctx.textBaseline = 'middle'; const lh = layout.lineHeight; if (t.shadow) { ctx.shadowColor = 'rgba(0,0,0,0.55)'; ctx.shadowBlur = Math.max(2, t.fontSize * 0.08); ctx.shadowOffsetX = ctx.shadowOffsetY = Math.max(1, t.fontSize * 0.05); } for (const line of layout.lines) { const cy = line.y + lh / 2; if (t.strokeWidth > 0) { ctx.lineJoin = 'round'; ctx.lineWidth = t.strokeWidth * 2; ctx.strokeStyle = t.strokeColor; ctx.strokeText(line.text, line.x, cy); } ctx.fillStyle = t.color; ctx.fillText(line.text, line.x, cy); if (t.underline && line.w > 0) { const thick = Math.max(1, t.fontSize / 16); ctx.fillRect(line.x, cy + t.fontSize * 0.42, line.w, thick); } } ctx.restore(); }