aboutsummaryrefslogtreecommitdiff
path: root/build/js/tools/nav.js
blob: 134f10afa65ff2d57068a945c9ff407bb62c49ca (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Hand, zoom and eyedropper tools.
import { Tool } from './tool.js';

export class HandTool extends Tool {
  static id = 'hand';
  static label = 'Hand';
  static icon = 'hand';
  static shortcut = 'H';
  static schema = [];
  get cursor() { return this.drag ? 'grabbing' : 'grab'; }
  get hint() { return 'Drag to pan · double-click to fit · hold Space with any tool'; }
  onPointerDown(e) {
    this.drag = { x: e.clientX, y: e.clientY };
    this.app.updateCursor();
  }
  onPointerMove(e) {
    if (!this.drag) return;
    this.app.renderer.pan(e.clientX - this.drag.x, e.clientY - this.drag.y);
    this.drag = { x: e.clientX, y: e.clientY };
  }
  onPointerUp() { this.drag = null; this.app.updateCursor(); }
  onDoubleClick() { this.app.renderer.fit(this.doc, true); }
}

export class ZoomTool extends Tool {
  static id = 'zoom';
  static label = 'Zoom';
  static icon = 'zoom';
  static shortcut = 'Z';
  static schema = [];
  get cursor() { return 'zoom-in'; }
  get hint() { return 'Click to zoom in · Alt+click to zoom out · drag to zoom to area'; }
  onPointerDown(e, p) { this.drag = { start: p, end: p, screen: this.app.renderer.clientToScreen(e), alt: e.altKey }; }
  onPointerMove(e, p) { if (this.drag) { this.drag.end = p; this.app.renderer.request(); } }
  onPointerUp(e, p) {
    const d = this.drag;
    this.drag = null;
    if (!d) return;
    const r = this.app.renderer;
    const w = Math.abs(p.x - d.start.x), h = Math.abs(p.y - d.start.y);
    if (w * this.doc.view.zoom > 8 && h * this.doc.view.zoom > 8) {
      const z = Math.min(r.width / w, r.height / h);
      r.setZoom(z);
      const v = this.doc.view;
      v.panX = r.width / 2 - ((p.x + d.start.x) / 2) * v.zoom;
      v.panY = r.height / 2 - ((p.y + d.start.y) / 2) * v.zoom;
      r.request();
    } else {
      r.zoomStep(d.alt ? -1 : 1, d.screen);
    }
  }
  drawOverlay(ctx, view, doc) {
    const d = this.drag;
    if (!d) return;
    view.outlineRect({ x: Math.min(d.start.x, d.end.x), y: Math.min(d.start.y, d.end.y), w: Math.abs(d.end.x - d.start.x), h: Math.abs(d.end.y - d.start.y) }, doc);
  }
}

export class EyedropperTool extends Tool {
  static id = 'eyedropper';
  static label = 'Eyedropper';
  static icon = 'eyedropper';
  static shortcut = 'I';
  static schema = [
    { key: 'sample', label: 'Sample', type: 'select', default: 'merged', options: [{ value: 'merged', label: 'All layers' }, { value: 'layer', label: 'Current layer' }] },
    { key: 'size', label: 'Average', type: 'select', default: 1, options: [{ value: 1, label: 'Point' }, { value: 3, label: '3×3' }, { value: 5, label: '5×5' }, { value: 11, label: '11×11' }] },
  ];
  get hint() { return 'Click: set foreground · Alt+click: set background'; }
  onPointerDown(e, p) {
    this.which = e.altKey ? 'bg' : 'fg';
    this.previous = this.app.colors[this.which].clone();
    this.down = true;
    this.sample(e, p);
  }
  onPointerMove(e, p) { if (this.down) this.sample(e, p); }
  onPointerUp() { this.down = false; this.app.renderer.request(); }
  cancel() {
    if (this.down && this.previous) this.app.setColor(this.which, this.previous);
    this.down = false;
  }

  /** Sampling ring around the pointer: new color on top, previous color below. */
  drawOverlay(ctx, view) {
    const s = view.pointer;
    if (!this.down || !s) return;
    const now = this.app.colors[this.which];
    const R = 46, W = 16;
    ctx.save();
    ctx.lineWidth = W;
    ctx.beginPath(); ctx.arc(s.x, s.y, R, Math.PI, 2 * Math.PI); ctx.strokeStyle = now.toCSS(); ctx.stroke();
    ctx.beginPath(); ctx.arc(s.x, s.y, R, 0, Math.PI); ctx.strokeStyle = this.previous.toCSS(); ctx.stroke();
    ctx.lineWidth = 1;
    ctx.strokeStyle = 'rgba(128,128,128,0.9)';
    for (const r of [R - W / 2, R + W / 2]) { ctx.beginPath(); ctx.arc(s.x, s.y, r, 0, Math.PI * 2); ctx.stroke(); }
    ctx.restore();
  }
  sample(e, p) {
    this.app.pickColorAt(p, this.which === 'bg', { merged: this.options.sample === 'merged', size: Number(this.options.size) });
    this.app.renderer.request();
  }
}