1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
// 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();
}
|