-
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
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
// Copyright 2026 Shota FUJI
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
import css from "./x-preview.css";
const SCALE_MIN = 0.1;
const SCALE_MAX = 5.0;
const TOUCH_IDLE = 0;
const TOUCH_PANNING = 1;
const TOUCH_SCALING = 2;
function idle() {
return {
state: TOUCH_IDLE,
touch: null,
x: 0,
y: 0,
distance: 0,
scale: 0.0,
};
}
function panning(touch, x, y) {
return {
state: TOUCH_PANNING,
touch,
x,
y,
distance: 0,
scale: 0.0,
};
}
function scaling(distance, scale) {
return {
state: TOUCH_SCALING,
touch: null,
x: 0,
y: 0,
distance,
scale,
};
}
export class XPreview extends HTMLElement {
#slot = document.createElement("slot");
#gesturePlane = document.createElement("div");
#size = null;
#resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
this.#size = entry.contentRect;
return;
}
});
#dx = 0;
#dy = 0;
#scale = 1.0;
#touch = idle();
#isRenderScheduled = false;
constructor() {
super();
const shadow = this.attachShadow({
mode: "open",
});
const style = document.createElement("style");
style.textContent = css;
shadow.appendChild(style);
shadow.appendChild(this.#slot);
this.#gesturePlane.classList.add("gesture-plane");
shadow.appendChild(this.#gesturePlane);
const reset = document.createElement("button");
reset.textContent = "Reset Viewport";
reset.classList.add("reset");
reset.classList.add("hidden");
reset.addEventListener("click", (event) => {
this.#dx = 0;
this.#dy = 0;
this.#scale = 1.0;
this.#render();
});
shadow.appendChild(reset);
}
connectedCallback() {
this.#gesturePlane.addEventListener("wheel", this.#onWheel);
this.#gesturePlane.addEventListener("touchstart", this.#onTouchChange);
this.#gesturePlane.addEventListener("touchend", this.#onTouchChange);
this.#gesturePlane.addEventListener("touchmove", this.#onTouchMove);
this.#gesturePlane.addEventListener("touchcancel", this.#onTouchCancel);
this.#render();
this.#resizeObserver.observe(this.#gesturePlane);
}
disconnectedCallback() {
this.#gesturePlane.removeEventListener("wheel", this.#onWheel);
this.#gesturePlane.removeEventListener("touchstart", this.#onTouchChange);
this.#gesturePlane.removeEventListener("touchend", this.#onTouchChange);
this.#gesturePlane.removeEventListener("touchmove", this.#onTouchMove);
this.#gesturePlane.removeEventListener("touchcancel", this.#onTouchCancel);
this.#resizeObserver.disconnect();
}
#onTouchChange = (event) => {
switch (event.touches.length) {
case 0:
return;
case 1:
this.#touch = panning(event.touches.item(0), this.#dx, this.#dy);
return;
default:
const distance = getPinchSize(event.touches);
if (distance !== null) {
this.#touch = scaling(distance, this.#scale);
}
return;
}
};
#onTouchCancel = (event) => {
this.#touch = idle();
};
#onTouchMove = (event) => {
switch (this.#touch.state) {
case TOUCH_IDLE:
return;
case TOUCH_PANNING: {
this.#dx =
this.#touch.x + (event.touches[0].clientX - this.#touch.touch.clientX) / this.#scale;
this.#dy =
this.#touch.y + (event.touches[0].clientY - this.#touch.touch.clientY) / this.#scale;
this.#render();
return;
}
case TOUCH_SCALING: {
const distance = getPinchSize(event.touches);
if (distance === null) {
return;
}
this.#scale = Math.max(
SCALE_MIN,
Math.min(SCALE_MAX, this.#touch.scale * (distance / this.#touch.distance)),
);
this.#render();
return;
}
}
};
#onWheel = (event) => {
event.preventDefault();
if (event.ctrlKey || event.metaKey) {
let { deltaY } = event;
// Workaround shitty design.
switch (event.deltaMode) {
// DOM_DELTA_LINE
case 1:
deltaY *= 15;
break;
// DOM_DELTA_PAGE
case 2:
deltaY *= 100;
break;
}
const previous = this.#scale;
this.#scale = Math.max(SCALE_MIN, Math.min(SCALE_MAX, this.#scale * (1 - deltaY / 100)));
const ox = event.offsetX - this.#size.width * 0.5;
const oy = event.offsetY - this.#size.height * 0.5;
this.#dx += ox / this.#scale - ox / previous;
this.#dy += oy / this.#scale - oy / previous;
} else {
this.#dx -= event.deltaX / this.#scale;
this.#dy -= event.deltaY / this.#scale;
}
this.#render();
};
#render() {
if (this.#isRenderScheduled) {
return;
}
this.#isRenderScheduled = true;
requestAnimationFrame(() => {
if (!this.isConnected) {
return;
}
for (const element of this.#slot.assignedElements()) {
element.style.transform = `scale(${this.#scale}) translate(${this.#dx}px, ${this.#dy}px)`;
}
this.#isRenderScheduled = false;
});
}
}
function getPinchSize(touches) {
const firstTouch = touches.item(0);
if (!firstTouch) {
return null;
}
const px = firstTouch.clientX;
const py = firstTouch.clientY;
let tx = 0;
let ty = 0;
for (let i = 1, touch; (touch = touches.item(i)); i++) {
tx += touch.clientX;
ty += touch.clientY;
}
return Math.sqrt(Math.pow(px - tx / touches.length, 2) + Math.pow(py - ty / touches.length, 2));
}