Skip to content

Commit ae7e206

Browse files
authored
Refactor touch event listeners to prevent page scrolling on touch move (#378)
* Refactor touch event listeners to prevent page scrolling on touch move Updated touch event listeners to use passive options and prevent default behavior on touchmove. * Enhance touchmove event handling Added stopImmediatePropagation to touchmove event. * Set canvas fill style to red before drawing * Fix escape characters in fill_style assignment * Update offscreen_canvas.ipynb * Simplify touchmove event listener Refactor touchmove event listener to simplify event handling. * Update offscreen.ts * Update offscreen_canvas.ipynb * prevent defaults
1 parent c77d005 commit ae7e206

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/offscreen.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class OffscreenCanvasView extends DOMWidgetView {
102102
}
103103
}
104104
async function sendTouchEvent(event: TouchEvent): Promise<void> {
105+
106+
event.preventDefault();
107+
event.stopImmediatePropagation();
108+
105109
const rect = that.el.getBoundingClientRect();
106110
const scaleX = that.el.width / rect.width;
107111
const scaleY = that.el.height / rect.height;
@@ -171,10 +175,12 @@ class OffscreenCanvasView extends DOMWidgetView {
171175
});
172176

173177
// touch events
174-
this.el.addEventListener('touchstart', sendTouchEvent);
175-
this.el.addEventListener('touchend', sendTouchEvent);
176-
this.el.addEventListener('touchmove', sendTouchEvent);
177-
this.el.addEventListener('touchcancel', sendTouchEvent);
178+
const opts = { passive: false };
179+
180+
this.el.addEventListener('touchstart', e => sendTouchEvent(e), opts);
181+
this.el.addEventListener('touchend', e => sendTouchEvent(e), opts);
182+
this.el.addEventListener('touchcancel', e => sendTouchEvent(e), opts);
183+
this.el.addEventListener('touchmove', e => sendTouchEvent(e), opts);
178184

179185
// keyboard events
180186
this.el.addEventListener('keydown', sendKeyboardEvent);

src/widget.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,22 +1377,27 @@ export class CanvasView extends DOMWidgetView {
13771377
});
13781378
this.el.addEventListener('wheel', {
13791379
handleEvent: this.onMouseWheel.bind(this)
1380-
});
1380+
}, { passive: false });
1381+
13811382
this.el.addEventListener('touchstart', {
13821383
handleEvent: this.onTouchStart.bind(this)
1383-
});
1384+
}, { passive: false });
1385+
13841386
this.el.addEventListener('touchend', {
13851387
handleEvent: this.onTouchEnd.bind(this)
1386-
});
1388+
}, { passive: false });
1389+
13871390
this.el.addEventListener('touchmove', {
13881391
handleEvent: this.onTouchMove.bind(this)
1389-
});
1392+
}, { passive: false });
1393+
13901394
this.el.addEventListener('touchcancel', {
13911395
handleEvent: this.onTouchCancel.bind(this)
1392-
});
1396+
}, { passive: false });
1397+
13931398
this.el.addEventListener('keydown', {
13941399
handleEvent: this.onKeyDown.bind(this)
1395-
});
1400+
}, { passive: false });
13961401

13971402
this.el.setAttribute('tabindex', '0');
13981403

@@ -1441,6 +1446,8 @@ export class CanvasView extends DOMWidgetView {
14411446
}
14421447

14431448
private onTouchStart(event: TouchEvent) {
1449+
event.preventDefault();
1450+
event.stopImmediatePropagation();
14441451
const touches: Touch[] = Array.from(event.touches);
14451452
this.model.send(
14461453
{
@@ -1452,6 +1459,8 @@ export class CanvasView extends DOMWidgetView {
14521459
}
14531460

14541461
private onTouchEnd(event: TouchEvent) {
1462+
event.preventDefault();
1463+
event.stopImmediatePropagation();
14551464
const touches: Touch[] = Array.from(event.touches);
14561465
this.model.send(
14571466
{
@@ -1463,6 +1472,8 @@ export class CanvasView extends DOMWidgetView {
14631472
}
14641473

14651474
private onTouchMove(event: TouchEvent) {
1475+
event.preventDefault();
1476+
event.stopImmediatePropagation();
14661477
const touches: Touch[] = Array.from(event.touches);
14671478
this.model.send(
14681479
{
@@ -1474,6 +1485,8 @@ export class CanvasView extends DOMWidgetView {
14741485
}
14751486

14761487
private onTouchCancel(event: TouchEvent) {
1488+
event.preventDefault();
1489+
event.stopImmediatePropagation();
14771490
const touches: Touch[] = Array.from(event.touches);
14781491
this.model.send(
14791492
{

0 commit comments

Comments
 (0)