// Paint bucket, gradient and shape tools. All render into a LiveStroke so they // preview live, respect the selection and produce a single undo step. import { Tool, snap45 } from './tool.js'; import { LiveStroke } from './paint.js'; import { blendOp } from '../core/layer.js'; import { regionMask, regionToCanvas, regionBounds } from '../imaging/floodfill.js'; import { flattenToCanvas } from '../render/compositor.js'; import { Color } from '../core/color.js'; /** Resolve color choice ('fg'|'bg'|'none') to a Color or null, grayscale on masks. */ function pick(app, which, target) { if (which === 'none') return null; const c = (which === 'bg' ? app.colors.bg : app.colors.fg).clone(); return target?.key === 'mask' ? c.toGray() : c; } function compositeFor(target, mode) { if (target.layer.alphaLock && target.key === 'canvas') return 'source-atop'; return blendOp(mode); } export class BucketTool extends Tool { static id = 'bucket'; static label = 'Paint Bucket'; static icon = 'bucket'; static shortcut = 'G'; static schema = [ { key: 'fill', label: 'Fill', type: 'select', default: 'fg', options: [{ value: 'fg', label: 'Foreground' }, { value: 'bg', label: 'Background' }] }, { key: 'tolerance', label: 'Tolerance', type: 'range', min: 0, max: 255, default: 32 }, { key: 'opacity', label: 'Opacity', type: 'range', min: 1, max: 100, default: 100, unit: '%' }, { key: 'mode', label: 'Mode', type: 'select', default: 'normal', options: 'blend' }, { key: 'contiguous', label: 'Contiguous', type: 'checkbox', default: true }, { key: 'sampleMerged', label: 'All layers', type: 'checkbox', default: false }, { key: 'antialias', label: 'Anti-alias', type: 'checkbox', default: true }, ]; get hint() { return 'Click to fill similar colors · Alt+click picks color'; } onPointerDown(e, p) { if (e.altKey) { this.app.pickColorAt(p, false); return; } const target = this.requireEditable(); if (!target) return; const doc = this.doc; const { width: w, height: h } = doc; if (p.x < 0 || p.y < 0 || p.x >= w || p.y >= h) return; const src = this.options.sampleMerged ? flattenToCanvas(doc) : target.layer[target.key]; const id = src.getContext('2d', { willReadFrequently: true }).getImageData(0, 0, w, h); const region = regionMask(id, p.x, p.y, this.options.tolerance, this.options.contiguous); if (!region) return; const color = pick(this.app, this.options.fill, target); if (target.key === 'canvas') this.app.noteColorUsed(color); const opacity = (this.options.opacity / 100) * color.a; color.a = 1; const stroke = new LiveStroke(doc, target, { opacity, composite: compositeFor(target, this.options.mode), label: 'Fill' }); const ctx = stroke.ctx; ctx.fillStyle = color.toCSS(); ctx.fillRect(0, 0, w, h); ctx.globalCompositeOperation = 'destination-in'; ctx.drawImage(regionToCanvas(region, w, h, this.options.antialias), 0, 0); ctx.globalCompositeOperation = 'source-over'; stroke.addDirty(regionBounds(region, w, h)); stroke.commit(); } } export class GradientTool extends Tool { static id = 'gradient'; static label = 'Gradient'; static icon = 'gradient'; static shortcut = 'G'; static schema = [ { key: 'type', label: 'Type', type: 'select', default: 'linear', options: [ { value: 'linear', label: 'Linear' }, { value: 'radial', label: 'Radial' }, { value: 'conic', label: 'Angle' }, { value: 'reflected', label: 'Reflected' }, ], }, { key: 'colors', label: 'Colors', type: 'select', default: 'fg-bg', options: [ { value: 'fg-bg', label: 'FG → BG' }, { value: 'fg-transparent', label: 'FG → Transparent' }, { value: 'rainbow', label: 'Spectrum' }, { value: 'fg-bg-fg', label: 'FG → BG → FG' }, ], }, { key: 'reverse', label: 'Reverse', type: 'checkbox', default: false }, { key: 'opacity', label: 'Opacity', type: 'range', min: 1, max: 100, default: 100, unit: '%' }, { key: 'mode', label: 'Mode', type: 'select', default: 'normal', options: 'blend' }, ]; get hint() { return 'Drag to draw a gradient · Shift: snap to 45°'; } stops(target) { const fg = pick(this.app, 'fg', target), bg = pick(this.app, 'bg', target); let stops; switch (this.options.colors) { case 'fg-transparent': { const t = fg.clone(); t.a = 0; stops = [[0, fg], [1, t]]; break; } case 'fg-bg-fg': stops = [[0, fg], [0.5, bg], [1, fg]]; break; case 'rainbow': stops = [0, 60, 120, 180, 240, 300, 360].map((h, i) => [i / 6, Color.fromHSV(h, 1, 1)]); if (target.key === 'mask') stops = stops.map(([o, c]) => [o, c.toGray()]); break; default: stops = [[0, fg], [1, bg]]; } if (this.options.reverse) stops = stops.map(([o, c]) => [1 - o, c]).reverse(); return stops; } onPointerDown(e, p) { const target = this.requireEditable(); if (!target) return; this.target = target; this.start = this.app.guides.snapPoint(p); this.end = this.start; this.stroke = new LiveStroke(this.doc, target, { opacity: this.options.opacity / 100, composite: compositeFor(target, this.options.mode), label: 'Gradient', }); } onPointerMove(e, p) { if (!this.stroke) return; if (e.shiftKey) { const v = snap45(p.x - this.start.x, p.y - this.start.y); p = { x: this.start.x + v.x, y: this.start.y + v.y }; } else { p = this.app.guides.snapPoint(p); } this.end = p; this.paint(); } paint() { const { stroke, start: a, end: b } = this; const doc = this.doc; stroke.clearBuffer(); const len = Math.hypot(b.x - a.x, b.y - a.y); if (len < 1) { stroke.update(); return; } const ctx = stroke.ctx; let g; let stops = this.stops(this.target); switch (this.options.type) { case 'radial': g = ctx.createRadialGradient(a.x, a.y, 0, a.x, a.y, len); break; case 'conic': g = ctx.createConicGradient(Math.atan2(b.y - a.y, b.x - a.x), a.x, a.y); break; case 'reflected': g = ctx.createLinearGradient(2 * a.x - b.x, 2 * a.y - b.y, b.x, b.y); stops = [...stops.map(([o, c]) => [0.5 - o / 2, c]).reverse(), ...stops.map(([o, c]) => [0.5 + o / 2, c])]; break; default: g = ctx.createLinearGradient(a.x, a.y, b.x, b.y); } for (const [o, c] of stops) g.addColorStop(Math.min(1, Math.max(0, o)), c.toCSS()); ctx.fillStyle = g; ctx.fillRect(0, 0, doc.width, doc.height); stroke.addDirty({ x: 0, y: 0, w: doc.width, h: doc.height }); stroke.update(); } onPointerUp() { if (!this.stroke) return; if (Math.hypot(this.end.x - this.start.x, this.end.y - this.start.y) < 1) this.stroke.cancel(); else this.stroke.commit(); this.stroke = null; this.app.renderer.request(); } cancel() { this.stroke?.cancel(); this.stroke = null; } drawOverlay(ctx, view, doc) { if (!this.stroke) return; const a = view.docToScreen(this.start, doc), b = view.docToScreen(this.end, doc); ctx.save(); ctx.lineWidth = 3; ctx.strokeStyle = 'rgba(0,0,0,0.6)'; ctx.beginPath(); ctx.moveTo(a.x, a.y); ctx.lineTo(b.x, b.y); ctx.stroke(); ctx.lineWidth = 1; ctx.strokeStyle = '#fff'; ctx.stroke(); for (const q of [a, b]) { ctx.beginPath(); ctx.arc(q.x, q.y, 4, 0, Math.PI * 2); ctx.fillStyle = '#fff'; ctx.fill(); ctx.stroke(); } ctx.restore(); } } export class ShapeTool extends Tool { static id = 'shape'; static label = 'Shapes'; static icon = 'shapes'; static shortcut = 'U'; static schema = [ { key: 'shape', label: 'Shape', type: 'select', default: 'rect', options: [ { value: 'rect', label: 'Rectangle' }, { value: 'roundrect', label: 'Rounded rect' }, { value: 'ellipse', label: 'Ellipse' }, { value: 'line', label: 'Line' }, { value: 'arrow', label: 'Arrow' }, { value: 'polygon', label: 'Polygon' }, { value: 'star', label: 'Star' }, ], }, { key: 'fill', label: 'Fill', type: 'select', default: 'fg', options: [{ value: 'fg', label: 'Foreground' }, { value: 'bg', label: 'Background' }, { value: 'none', label: 'None' }] }, { key: 'stroke', label: 'Stroke', type: 'select', default: 'none', options: [{ value: 'fg', label: 'Foreground' }, { value: 'bg', label: 'Background' }, { value: 'none', label: 'None' }] }, { key: 'strokeWidth', label: 'Width', type: 'range', min: 1, max: 100, default: 4, unit: 'px' }, { key: 'radius', label: 'Radius', type: 'range', min: 0, max: 200, default: 16, unit: 'px', when: (o) => o.shape === 'roundrect' }, { key: 'sides', label: 'Sides', type: 'range', min: 3, max: 24, default: 5, when: (o) => o.shape === 'polygon' || o.shape === 'star' }, { key: 'opacity', label: 'Opacity', type: 'range', min: 1, max: 100, default: 100, unit: '%' }, { key: 'antialias', label: 'Anti-alias', type: 'checkbox', default: true }, ]; get hint() { return 'Drag to draw · Shift: constrain · Alt: from center'; } onPointerDown(e, p) { const target = this.requireEditable(); if (!target) return; this.target = target; this.start = this.app.guides.snapPoint(p); this.end = this.start; this.stroke = new LiveStroke(this.doc, target, { opacity: this.options.opacity / 100, composite: compositeFor(target, 'normal'), label: 'Shape' }); } onPointerMove(e, p) { if (!this.stroke) return; this.end = this.app.guides.snapPoint(p); this.shift = e.shiftKey; this.alt = e.altKey; this.paint(); } geometry() { const a = this.start; let dx = this.end.x - a.x, dy = this.end.y - a.y; const isLine = this.options.shape === 'line' || this.options.shape === 'arrow'; if (this.shift) { if (isLine) ({ x: dx, y: dy } = snap45(dx, dy)); else { const m = Math.max(Math.abs(dx), Math.abs(dy)); dx = Math.sign(dx || 1) * m; dy = Math.sign(dy || 1) * m; } } if (isLine) return { x0: a.x, y0: a.y, x1: a.x + dx, y1: a.y + dy }; let x0 = a.x, y0 = a.y; if (this.alt) { x0 -= dx; y0 -= dy; dx *= 2; dy *= 2; } return { x: Math.min(x0, x0 + dx), y: Math.min(y0, y0 + dy), w: Math.abs(dx), h: Math.abs(dy) }; } paint() { const { stroke, options: o } = this; stroke.clearBuffer(); const ctx = stroke.ctx; const g = this.geometry(); const fill = pick(this.app, o.fill, this.target); const line = pick(this.app, o.stroke, this.target); const lw = o.strokeWidth; ctx.save(); ctx.lineJoin = 'round'; ctx.lineCap = 'round'; ctx.lineWidth = lw; const path = new Path2D(); let bounds; if (o.shape === 'line' || o.shape === 'arrow') { const strokeColor = line || fill || pick(this.app, 'fg', this.target); ctx.strokeStyle = strokeColor.toCSS(); ctx.fillStyle = strokeColor.toCSS(); const len = Math.hypot(g.x1 - g.x0, g.y1 - g.y0); if (o.shape === 'arrow' && len > 0) { const head = Math.max(lw * 3, 10); const ang = Math.atan2(g.y1 - g.y0, g.x1 - g.x0); const bx = g.x1 - Math.cos(ang) * head, by = g.y1 - Math.sin(ang) * head; path.moveTo(g.x0, g.y0); path.lineTo(bx, by); ctx.stroke(path); const hp = new Path2D(); hp.moveTo(g.x1, g.y1); hp.lineTo(bx + Math.cos(ang + Math.PI / 2) * head * 0.6, by + Math.sin(ang + Math.PI / 2) * head * 0.6); hp.lineTo(bx + Math.cos(ang - Math.PI / 2) * head * 0.6, by + Math.sin(ang - Math.PI / 2) * head * 0.6); hp.closePath(); ctx.fill(hp); } else { path.moveTo(g.x0, g.y0); path.lineTo(g.x1, g.y1); ctx.stroke(path); } bounds = { x: Math.min(g.x0, g.x1), y: Math.min(g.y0, g.y1), w: Math.abs(g.x1 - g.x0), h: Math.abs(g.y1 - g.y0) }; } else { const { x, y, w, h } = g; switch (o.shape) { case 'roundrect': path.roundRect(x, y, w, h, Math.min(o.radius, w / 2, h / 2)); break; case 'ellipse': path.ellipse(x + w / 2, y + h / 2, w / 2, h / 2, 0, 0, Math.PI * 2); break; case 'polygon': case 'star': { const n = o.sides, cx = x + w / 2, cy = y + h / 2; const pts = o.shape === 'star' ? n * 2 : n; for (let i = 0; i < pts; i++) { const ang = -Math.PI / 2 + (i * Math.PI * 2) / pts; const k = o.shape === 'star' && i % 2 ? 0.45 : 1; const px = cx + Math.cos(ang) * (w / 2) * k, py = cy + Math.sin(ang) * (h / 2) * k; if (i) path.lineTo(px, py); else path.moveTo(px, py); } path.closePath(); break; } default: path.rect(x, y, w, h); } if (fill) { ctx.fillStyle = fill.toCSS(); ctx.fill(path); } if (line) { ctx.strokeStyle = line.toCSS(); ctx.stroke(path); } bounds = g; } ctx.restore(); if (!o.antialias) hardenAlpha(stroke.buffer); stroke.addDirty({ x: bounds.x - lw * 4, y: bounds.y - lw * 4, w: bounds.w + lw * 8, h: bounds.h + lw * 8 }); stroke.update(); } onPointerUp() { if (!this.stroke) return; const g = this.geometry(); const tiny = g.w !== undefined ? g.w < 1 && g.h < 1 : Math.hypot(g.x1 - g.x0, g.y1 - g.y0) < 1; if (tiny) this.stroke.cancel(); else this.stroke.commit(); this.stroke = null; } cancel() { this.stroke?.cancel(); this.stroke = null; } } function hardenAlpha(canvas) { const ctx = canvas.getContext('2d'); const id = ctx.getImageData(0, 0, canvas.width, canvas.height); const d = id.data; for (let i = 3; i < d.length; i += 4) d[i] = d[i] >= 128 ? 255 : 0; ctx.putImageData(id, 0, 0); }