Skip to content

Commit fd44907

Browse files
mobile
1 parent 86f7162 commit fd44907

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

js10/cubes/scripts/script.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class IsometricCanvas {
103103
this.canvas.addEventListener('wheel', this._handleWheel.bind(this));
104104
this.canvas.addEventListener('click', this._handleClick.bind(this));
105105
this.canvas.addEventListener('contextmenu', (e) => e.preventDefault());
106+
this.canvas.addEventListener('touchstart', this._handleTouchStart.bind(this), { passive: false });
107+
this.canvas.addEventListener('touchmove', this._handleTouchMove.bind(this), { passive: false });
108+
this.canvas.addEventListener('touchend', this._handleTouchEnd.bind(this));
106109
}
107110

108111
_handleClick(e) {
@@ -221,6 +224,81 @@ class IsometricCanvas {
221224
}
222225
}
223226

227+
_handleTouchStart(e) {
228+
if (e.touches.length === 1) {
229+
this.isDragging = true;
230+
const touch = e.touches[0];
231+
this.lastMouseX = touch.clientX - this.canvas.getBoundingClientRect().left;
232+
this.lastMouseY = touch.clientY - this.canvas.getBoundingClientRect().top;
233+
} else if (e.touches.length === 2) {
234+
this._startPinchZoom(e);
235+
}
236+
}
237+
238+
_handleTouchMove(e) {
239+
e.preventDefault();
240+
241+
if (e.touches.length === 1 && this.isDragging) {
242+
const touch = e.touches[0];
243+
const x = touch.clientX - this.canvas.getBoundingClientRect().left;
244+
const y = touch.clientY - this.canvas.getBoundingClientRect().top;
245+
246+
const deltaX = x - this.lastMouseX;
247+
const deltaY = y - this.lastMouseY;
248+
249+
this.offsetX += deltaX;
250+
this.offsetY += deltaY;
251+
252+
this.lastMouseX = x;
253+
this.lastMouseY = y;
254+
255+
if (this.onViewChanged) this.onViewChanged();
256+
} else if (e.touches.length === 2) {
257+
this._handlePinchZoom(e);
258+
}
259+
}
260+
261+
_handleTouchEnd(e) {
262+
this.isDragging = false;
263+
}
264+
265+
_startPinchZoom(e) {
266+
const [t1, t2] = e.touches;
267+
this._initialPinchDistance = Math.hypot(
268+
t2.clientX - t1.clientX,
269+
t2.clientY - t1.clientY
270+
);
271+
this._initialScale = this.scale;
272+
}
273+
274+
_handlePinchZoom(e) {
275+
const [t1, t2] = e.touches;
276+
const distance = Math.hypot(
277+
t2.clientX - t1.clientX,
278+
t2.clientY - t1.clientY
279+
);
280+
281+
if (!this._initialPinchDistance) return;
282+
283+
const scaleFactor = distance / this._initialPinchDistance;
284+
const newScale = Math.max(this.minScale, Math.min(this.maxScale, this._initialScale * scaleFactor));
285+
286+
const rect = this.canvas.getBoundingClientRect();
287+
const centerX = (t1.clientX + t2.clientX) / 2 - rect.left;
288+
const centerY = (t1.clientY + t2.clientY) / 2 - rect.top;
289+
290+
const dx = centerX - this.originX - this.offsetX;
291+
const dy = centerY - this.originY - this.offsetY;
292+
293+
this.offsetX += dx - dx * (newScale / this.scale);
294+
this.offsetY += dy - dy * (newScale / this.scale);
295+
this.scale = newScale;
296+
297+
if (this.onViewChanged) this.onViewChanged();
298+
}
299+
300+
301+
224302
resetView() {
225303
this.offsetX = 0;
226304
this.offsetY = 0;

0 commit comments

Comments
 (0)