// Navigator: thumbnail of the whole image with the visible viewport; click/drag to pan. // Also shows pointer position and the color under the cursor. import { el, createCanvas } from '../../core/util.js'; const W = 236, H = 120; export class NavigatorPanel { constructor(app, container) { this.app = app; this.canvas = createCanvas(W, H); this.canvas.className = 'nav-canvas'; this.info = el('div', { class: 'nav-info' }); this.el = el('div', { class: 'panel nav-panel' }, el('div', { class: 'panel-title' }, 'Navigator'), this.canvas, this.info); container.appendChild(this.el); const redraw = () => this.schedule(); app.on('doc', redraw); app.on('docChange', redraw); app.on('view', redraw); app.on('pointerInfo', (info) => this.showInfo(info)); const panTo = (e) => { const doc = app.doc; if (!doc || !this.map) return; const r = this.canvas.getBoundingClientRect(); const x = ((e.clientX - r.left) / r.width) * W, y = ((e.clientY - r.top) / r.height) * H; const dx = (x - this.map.ox) / this.map.k, dy = (y - this.map.oy) / this.map.k; const rend = app.renderer; doc.view.panX = rend.width / 2 - dx * doc.view.zoom; doc.view.panY = rend.height / 2 - dy * doc.view.zoom; rend.request(); app.emit('view'); }; this.canvas.addEventListener('pointerdown', (e) => { this.canvas.setPointerCapture(e.pointerId); panTo(e); const move = (ev) => panTo(ev); const up = () => { this.canvas.removeEventListener('pointermove', move); this.canvas.removeEventListener('pointerup', up); }; this.canvas.addEventListener('pointermove', move); this.canvas.addEventListener('pointerup', up); }); } schedule() { if (this.timer) return; this.timer = setTimeout(() => { this.timer = null; this.draw(); }, 100); } draw() { const ctx = this.canvas.getContext('2d'); ctx.clearRect(0, 0, W, H); const doc = this.app.doc; if (!doc) return; const k = Math.min(W / doc.width, H / doc.height); const w = doc.width * k, h = doc.height * k; const ox = (W - w) / 2, oy = (H - h) / 2; this.map = { k, ox, oy }; ctx.fillStyle = '#fff'; ctx.fillRect(ox, oy, w, h); ctx.imageSmoothingQuality = 'high'; ctx.drawImage(this.app.renderer.composite, ox, oy, w, h); // viewport rectangle const r = this.app.renderer; const a = r.screenToDoc({ x: 0, y: 0 }, doc), b = r.screenToDoc({ x: r.width, y: r.height }, doc); ctx.strokeStyle = '#e5484d'; ctx.lineWidth = 2; ctx.strokeRect(ox + a.x * k, oy + a.y * k, (b.x - a.x) * k, (b.y - a.y) * k); } showInfo(info) { if (!info) { this.info.textContent = ''; return; } const { x, y, color } = info; const c = color ? `RGBA ${color[0]}, ${color[1]}, ${color[2]}, ${(color[3] / 255).toFixed(2)}` : ''; this.info.innerHTML = ''; const sw = el('span', { class: 'mini-swatch' }); if (color) sw.style.setProperty('--c', `rgba(${color[0]},${color[1]},${color[2]},${color[3] / 255})`); this.info.append(el('span', {}, `X ${x} Y ${y}`), color ? sw : '', el('span', {}, c)); } }