Skip to content

Commit 64ca5dd

Browse files
committed
replace mouse events with a pointer events
1 parent d78619c commit 64ca5dd

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/view/area.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export class Area extends Emitter {
1818
this._startPosition = null;
1919
this._zoom = new Zoom(container, el, 0.1, this.onZoom.bind(this));
2020
this._drag = new Drag(container, this.onTranslate.bind(this), this.onStart.bind(this));
21-
this.container.addEventListener('mousemove', this.mousemove.bind(this));
22-
this.container.addEventListener('touchmove', this.mousemove.bind(this));
21+
this.container.addEventListener('pointermove', this.pointermove.bind(this));
2322

2423
this.update();
2524
}
@@ -30,15 +29,15 @@ export class Area extends Emitter {
3029
this.el.style.transform = `translate(${t.x}px, ${t.y}px) scale(${t.k})`;
3130
}
3231

33-
mousemove(e) {
34-
const { clientX, clientY } = e instanceof TouchEvent ? e.touches[0] : e;
32+
pointermove(e) {
33+
const { clientX, clientY } = e;
3534
const rect = this.el.getBoundingClientRect();
3635
const x = clientX - rect.left;
3736
const y = clientY - rect.top;
3837
const k = this.transform.k;
3938

4039
this.mouse = { x: x / k, y: y / k };
41-
this.trigger('mousemove', { ...this.mouse });
40+
this.trigger('mousemove', { ...this.mouse }); // TODO rename on `pointermove`
4241
}
4342

4443
onStart() {

src/view/drag.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export class Drag {
22

33
constructor(el, onTranslate = () => {}, onStart = () => {}, onDrag = () => {}) {
4-
this.mouseStart = null;
4+
this.pointerStart = null;
55

66
this.el = el;
77
this.onTranslate = onTranslate;
@@ -12,46 +12,35 @@ export class Drag {
1212
}
1313

1414
initEvents(el) {
15-
el.addEventListener('mousedown', this.down.bind(this));
16-
window.addEventListener('mousemove', this.move.bind(this));
17-
window.addEventListener('mouseup', this.up.bind(this));
18-
19-
el.addEventListener('touchstart', this.down.bind(this));
20-
window.addEventListener('touchmove', this.move.bind(this), {
21-
passive: false
22-
});
23-
window.addEventListener('touchend', this.up.bind(this));
24-
}
25-
26-
getCoords(e) {
27-
const props = e.touches ? e.touches[0] : e;
15+
el.style.touchAction = 'none';
2816

29-
return [props.pageX, props.pageY];
17+
el.addEventListener('pointerdown', this.down.bind(this));
18+
window.addEventListener('pointermove', this.move.bind(this));
19+
window.addEventListener('pointerup', this.up.bind(this));
3020
}
3121

3222
down(e) {
3323
e.stopPropagation();
34-
this.mouseStart = this.getCoords(e);
24+
this.pointerStart = [e.pageX, e.pageY]
3525

3626
this.onStart(e);
3727
}
3828

3929
move(e) {
40-
if (!this.mouseStart) return;
30+
if (!this.pointerStart) return;
4131
e.preventDefault();
42-
e.stopPropagation();
4332

44-
let [x, y] = this.getCoords(e);
45-
let delta = [x - this.mouseStart[0], y - this.mouseStart[1]];
33+
let [x, y] = [e.pageX, e.pageY]
34+
let delta = [x - this.pointerStart[0], y - this.pointerStart[1]];
4635
let zoom = this.el.getBoundingClientRect().width / this.el.offsetWidth;
4736

4837
this.onTranslate(delta[0] / zoom, delta[1] / zoom, e);
4938
}
5039

5140
up(e) {
52-
if (!this.mouseStart) return;
41+
if (!this.pointerStart) return;
5342

54-
this.mouseStart = null;
43+
this.pointerStart = null;
5544
this.onDrag(e);
5645
}
5746
}

0 commit comments

Comments
 (0)