aboutsummaryrefslogtreecommitdiff
path: root/build/js/ui/panels/navigator.js
blob: d295719f9a69a844c03839685dc210e822b40682 (plain)
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
// 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));
  }
}