// Crop tool: drag a rectangle (or adjust the default full-canvas box) and press Enter. import { Tool } from './tool.js'; import { cropTo } from '../core/ops.js'; const HANDLES = [ [0, 0], [0.5, 0], [1, 0], [1, 0.5], [1, 1], [0.5, 1], [0, 1], [0, 0.5], ]; const ASPECTS = { free: null, original: 'orig', '1:1': 1, '4:3': 4 / 3, '3:2': 3 / 2, '16:9': 16 / 9, '3:4': 3 / 4, '9:16': 9 / 16 }; export class CropTool extends Tool { static id = 'crop'; static label = 'Crop'; static icon = 'crop'; static shortcut = 'C'; static schema = [ { key: 'aspect', label: 'Aspect', type: 'select', default: 'free', options: Object.keys(ASPECTS).map((k) => ({ value: k, label: k === 'free' ? 'Free' : k === 'original' ? 'Original' : k })) }, { key: 'shade', label: 'Shade outside', type: 'checkbox', default: true }, { key: 'thirds', label: 'Rule of thirds', type: 'checkbox', default: true }, { type: 'action', label: 'Apply', action: 'apply', primary: true }, { type: 'action', label: 'Cancel', action: 'cancel' }, ]; get hint() { return 'Drag to set crop · drag handles to adjust · Enter to apply · Esc to cancel'; } get hidesSelection() { return !!this.rect; } get cursor() { return this.hoverCursor || 'crosshair'; } activate() { this.rect = null; } deactivate() { this.rect = null; } ratio() { const a = ASPECTS[this.options.aspect]; if (a === 'orig') return this.doc.width / this.doc.height; return a; } hitTest(p) { if (!this.rect) return null; const view = this.app.renderer; const s = view.docToScreen(p); for (let i = 0; i < HANDLES.length; i++) { const [hx, hy] = HANDLES[i]; const hp = view.docToScreen({ x: this.rect.x + this.rect.w * hx, y: this.rect.y + this.rect.h * hy }); if (Math.abs(hp.x - s.x) <= 7 && Math.abs(hp.y - s.y) <= 7) return { handle: i }; } const r = this.rect; if (p.x >= r.x && p.y >= r.y && p.x <= r.x + r.w && p.y <= r.y + r.h) return { inside: true }; return null; } onHover(e, p) { const hit = this.hitTest(p); const cursors = ['nwse-resize', 'ns-resize', 'nesw-resize', 'ew-resize', 'nwse-resize', 'ns-resize', 'nesw-resize', 'ew-resize']; const next = hit?.handle != null ? cursors[hit.handle] : hit?.inside ? 'move' : 'crosshair'; if (next !== this.hoverCursor) { this.hoverCursor = next; this.app.updateCursor(); } } onPointerDown(e, p) { const hit = this.hitTest(p); if (hit?.handle != null) this.drag = { type: 'handle', handle: hit.handle, start: p, orig: { ...this.rect } }; else if (hit?.inside) this.drag = { type: 'move', start: p, orig: { ...this.rect } }; else this.drag = { type: 'new', start: this.app.guides.snapPoint(p) }; } onPointerMove(e, p) { const d = this.drag; if (!d) return; if (d.type !== 'move') p = this.app.guides.snapPoint(p); const ratio = this.ratio() ?? (e.shiftKey ? 1 : null); if (d.type === 'new') { let dx = p.x - d.start.x, dy = p.y - d.start.y; if (ratio) { const w = Math.max(Math.abs(dx), Math.abs(dy) * ratio); dx = Math.sign(dx || 1) * w; dy = Math.sign(dy || 1) * (w / ratio); } this.rect = norm({ x: d.start.x, y: d.start.y, w: dx, h: dy }); } else if (d.type === 'move') { const { dx, dy } = this.app.guides.snapMove(d.orig, Math.round(p.x - d.start.x), Math.round(p.y - d.start.y)); this.rect = { ...d.orig, x: d.orig.x + dx, y: d.orig.y + dy }; } else { const [hx, hy] = HANDLES[d.handle]; const o = d.orig; let x0 = o.x, y0 = o.y, x1 = o.x + o.w, y1 = o.y + o.h; if (hx === 0) x0 = p.x; else if (hx === 1) x1 = p.x; if (hy === 0) y0 = p.y; else if (hy === 1) y1 = p.y; let r = { x: x0, y: y0, w: x1 - x0, h: y1 - y0 }; if (ratio && hx !== 0.5 && hy !== 0.5) { const w = Math.max(Math.abs(r.w), Math.abs(r.h) * ratio); const h = w / ratio; r = { x: hx === 0 ? x1 - w : x0, y: hy === 0 ? y1 - h : y0, w, h, }; } else if (ratio) { if (hx === 0.5) { const w = Math.abs(r.h) * ratio; r.x = o.x + o.w / 2 - w / 2; r.w = w; } else { const h = Math.abs(r.w) / ratio; r.y = o.y + o.h / 2 - h / 2; r.h = h; } } this.rect = norm(r); } this.app.setStatusHint(`${this.rect.w} × ${this.rect.h}`); this.app.renderer.request(); } onPointerUp() { this.drag = null; if (this.rect && (this.rect.w < 1 || this.rect.h < 1)) this.rect = null; this.app.renderer.request(); } onDoubleClick(e, p) { if (this.hitTest(p)?.inside) this.apply(); } onKeyDown(e) { if (!this.rect) return false; if (e.key === 'Enter') { this.apply(); return true; } if (e.key === 'Escape') { this.cancel(); return true; } return false; } runAction(name) { if (name === 'apply') this.apply(); else this.cancel(); } apply() { if (!this.rect) { this.app.toast('Drag on the canvas to choose the crop area'); return; } cropTo(this.doc, this.rect); this.rect = null; this.app.renderer.request(); } cancel() { this.rect = null; this.drag = null; this.app.renderer.request(); } drawOverlay(ctx, view, doc) { const r = this.rect; if (!r) return; const a = view.docToScreen({ x: r.x, y: r.y }, doc); const b = view.docToScreen({ x: r.x + r.w, y: r.y + r.h }, doc); if (this.options.shade) { ctx.save(); ctx.fillStyle = 'rgba(0,0,0,0.55)'; ctx.beginPath(); ctx.rect(0, 0, view.width, view.height); ctx.rect(a.x, a.y, b.x - a.x, b.y - a.y); ctx.fill('evenodd'); ctx.restore(); } if (this.options.thirds) { ctx.save(); ctx.strokeStyle = 'rgba(255,255,255,0.5)'; ctx.lineWidth = 1; ctx.beginPath(); for (const t of [1 / 3, 2 / 3]) { const x = Math.round(a.x + (b.x - a.x) * t) + 0.5, y = Math.round(a.y + (b.y - a.y) * t) + 0.5; ctx.moveTo(x, a.y); ctx.lineTo(x, b.y); ctx.moveTo(a.x, y); ctx.lineTo(b.x, y); } ctx.stroke(); ctx.restore(); } view.outlineRect(r, doc); for (const [hx, hy] of HANDLES) view.handle({ x: r.x + r.w * hx, y: r.y + r.h * hy }, 8, doc); } } function norm(r) { const x = Math.round(Math.min(r.x, r.x + r.w)), y = Math.round(Math.min(r.y, r.y + r.h)); return { x, y, w: Math.round(Math.max(r.x, r.x + r.w)) - x, h: Math.round(Math.max(r.y, r.y + r.h)) - y }; }