aboutsummaryrefslogtreecommitdiff
path: root/web/pw-visualizer/src/webgl/util.ts
blob: 0a93d67e003bc181aa90a4b67c5d6312e05be8c4 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
export interface Dictionary<T> {
  [Key: string]: T;
}


interface OnLoadable {
  onload: any;
}

export function onload2promise<T extends OnLoadable>(obj: T): Promise<T> {
  return new Promise(resolve => {
    obj.onload = () => resolve(obj);
  });
}

export function resizeCanvasToDisplaySize(
    canvas: HTMLCanvasElement,
    multiplier?: number,
): boolean {
    multiplier = multiplier || 1;
    var width  = canvas.clientWidth  * multiplier | 0;
    var height = canvas.clientHeight * multiplier | 0;
    if (canvas.width !== width ||  canvas.height !== height) {
      canvas.width  = width;
      canvas.height = height;
      return true;
    }
    return false;
}

export class FPSCounter {
  last: number;
  count: number;
  _delta: number;
  _prev: number;

  _frame_start: number;
  _total_frametime: number;

  constructor() {
    this.last = 0;
    this.count = 0;
    this._delta = 0;
    this._prev = 0;
  }

  frame(now: number) {
    this._frame_start = performance.now();
    this.count += 1;
    this._delta = now - this._prev;
    this._prev = now;

    if (now - this.last > 1000) {
      this.last = now;
      console.log(`${this.count} fps, ${(this._total_frametime / this.count).toFixed(2)}ms avg per frame`);
      this.count = 0;
      this._total_frametime = 0;
    }
  }

  frame_end() {
    this._total_frametime += (performance.now() - this._frame_start);
  }

  delta(now: number): number {
    return this._delta;
  }
}

export class Resizer {
    hoovering = false;
    dragging = false;

    mouse_pos = [0, 0];
    last_drag = [0, 0];

    viewbox: number[];
    orig_viewbox: number[];

    el_box: number[];

    scaleX = 1;
    scaleY = 1;

    constructor(el: HTMLCanvasElement, viewbox: number[], keep_aspect_ratio=false) {
        viewbox = [-viewbox[0] - viewbox[2], - viewbox[1] - viewbox[3], viewbox[2], viewbox[3]];
        this.viewbox = [...viewbox];
        this.el_box = [el.width, el.height];

        if (keep_aspect_ratio) {
            const or_width = this.viewbox[2];
            const or_height = this.viewbox[3];

            const width_percentage =  this.viewbox[2] / el.width;
            const height_percentage = this.viewbox[3] / el.height;

            if (width_percentage < height_percentage) {
                // width should be larger
                this.viewbox[2] = height_percentage * el.width;
            } else {
                // height should be larger
                this.viewbox[3] = width_percentage * el.height;
            }

            this.viewbox[0] -= (this.viewbox[2] - or_width) / 2;
            this.viewbox[1] -= (this.viewbox[3] - or_height) / 2;

            this.scaleX = this.viewbox[2] / this.viewbox[3];
        }

        this.orig_viewbox = [...this.viewbox];

        el.addEventListener("mouseleave", this.mouseleave.bind(this), { capture: false, passive: true});
        el.addEventListener("mousemove", this.mousemove.bind(this), { capture: false, passive: true});
        el.addEventListener("mousedown", this.mousedown.bind(this), { capture: false, passive: true});
        el.addEventListener("mouseup", this.mouseup.bind(this), { capture: false, passive: true});

        window.addEventListener('wheel', this.wheel.bind(this), { capture: false, passive: true});
    }

    _clip_viewbox() {
        this.viewbox[0] = Math.max(this.viewbox[0], this.orig_viewbox[0]);
        this.viewbox[1] = Math.max(this.viewbox[1], this.orig_viewbox[1]);

        this.viewbox[0] = Math.min(this.viewbox[0] + this.viewbox[2], this.orig_viewbox[0] + this.orig_viewbox[2]) - this.viewbox[2];
        this.viewbox[1] = Math.min(this.viewbox[1] + this.viewbox[3], this.orig_viewbox[1] + this.orig_viewbox[3]) - this.viewbox[3];
    }

    mouseleave() {
        this.hoovering = false;
    }

    mousemove(e: MouseEvent) {
        // when using mouseenter, hooveing will not be set to true if the mouse is already on the element when it is being created.
        // TODO: is there a better way?
        this.hoovering = true;
        this.mouse_pos = [e.offsetX, this.el_box[1] - e.offsetY];

        if (this.dragging) {
            const scaleX = this.viewbox[2] / this.el_box[0];
            const scaleY = this.viewbox[3] / this.el_box[1];

            this.viewbox[0] += (this.last_drag[0] - this.mouse_pos[0]) * scaleX;
            this.viewbox[1] += (this.last_drag[1] - this.mouse_pos[1]) * scaleY;

            this.last_drag = [...this.mouse_pos];

            this._clip_viewbox();
        }
    }

    mousedown() {
        this.dragging = true;
        this.last_drag = [...this.mouse_pos];
    }

    mouseup() {
        this.dragging = false;
    }

    wheel(e: WheelEvent) {
        if (this.hoovering) {
            const delta = e.deltaY > 0 ? 0.1 * this.viewbox[2] : -0.1 * this.viewbox[2];
            const dx =  delta * this.scaleX;
            const dy = delta * this.scaleY;

            const mouse_dx = this.mouse_pos[0] / this.el_box[0];
            const mouse_dy = this.mouse_pos[1] / this.el_box[1];

            this._zoom([dx, dy], [mouse_dx, mouse_dy]);
        }
    }

    _zoom(deltas: number[], center: number[]) {
      this.viewbox[2] += deltas[0];
      this.viewbox[0] -= deltas[0] * center[0];
      this.viewbox[2] = Math.min(this.viewbox[2], this.orig_viewbox[2]);

      this.viewbox[3] += deltas[1];
      this.viewbox[1] -= deltas[1] * center[1];
      this.viewbox[3] = Math.min(this.viewbox[3], this.orig_viewbox[3]);

      this._clip_viewbox();
    }

    get_viewbox(): number[] {
      return this.viewbox;
    }

    get_mouse_pos(): number[] {
        return this.mouse_pos;
    }
}

export class Mesh {
  cells: number[];
  positions: number[];

  constructor(mesh: any) {
      this.cells = mesh.cells.flat();
      this.positions = mesh.positions.flat();
  }
}