@@ -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