Skip to content

Commit c0626e6

Browse files
committed
- added touch events to heat example
1 parent 59f9b79 commit c0626e6

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

examples/heat_equation.html

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,15 @@ <h3>Note on stability</h3>
172172
this.scaleFactor = 2;
173173
this.mouseButtonDown = false
174174

175-
// register mouse event listeners (zoom/selection)
175+
// register mouse event listeners
176176
this.canvas.addEventListener("mousedown", this.mouseDown.bind(this), false);
177177
this.canvas.addEventListener("mousemove", this.mouseMove.bind(this), false);
178-
this.canvas.addEventListener("mouseup", this.mouseUp.bind(this), false);
178+
this.canvas.addEventListener("mouseup", this.mouseUp.bind(this), false);
179+
// register touch event listeners (mobile / tablet)
180+
// passive:false is required so we can call preventDefault() to suppress scrolling
181+
this.canvas.addEventListener("touchstart", this.touchStart.bind(this), { passive: false });
182+
this.canvas.addEventListener("touchmove", this.touchMove.bind(this), { passive: false });
183+
this.canvas.addEventListener("touchend", this.touchEnd.bind(this), { passive: false });
179184
}
180185

181186
// set simulation parameters from GUI and start mainLoop
@@ -285,6 +290,32 @@ <h3>Note on stability</h3>
285290
this.mainLoop();
286291
}
287292

293+
// Convert the first touch point to a plain {clientX, clientY} object
294+
// so it can be passed directly to the existing mouse handlers.
295+
getTouchClient(event)
296+
{
297+
const t = event.touches.length > 0 ? event.touches[0] : event.changedTouches[0];
298+
return { clientX: t.clientX, clientY: t.clientY };
299+
}
300+
301+
touchStart(event)
302+
{
303+
event.preventDefault();
304+
this.mouseDown({ which: 1, ...this.getTouchClient(event) });
305+
}
306+
307+
touchMove(event)
308+
{
309+
event.preventDefault();
310+
this.mouseMove(this.getTouchClient(event));
311+
}
312+
313+
touchEnd(event)
314+
{
315+
event.preventDefault();
316+
this.mouseUp(event);
317+
}
318+
288319
mouseDown(event)
289320
{
290321
// left mouse button down

0 commit comments

Comments
 (0)