@@ -289,7 +289,7 @@ export default function (
289289 camera . updateProjectionMatrix ( ) ;
290290 }
291291
292- function onDocumentMouseDown ( event ) {
292+ function onMouseDown ( event , touch ) {
293293 event . preventDefault ( ) ;
294294
295295 isMouseDown = true ;
@@ -299,24 +299,27 @@ export default function (
299299 onMouseDownTheta = theta ;
300300 onMouseDownPhi = phi ;
301301
302- onMouseDownPosition [ 0 ] = event . clientX ;
303- onMouseDownPosition [ 1 ] = event . clientY ;
302+ onMouseDownPosition [ 0 ] = touch ? event . touches [ 0 ] . clientX : event . clientX ;
303+ onMouseDownPosition [ 1 ] = touch ? event . touches [ 0 ] . clientY : event . clientY ;
304304
305305 onMouseDownFocus = focus . clone ( ) ;
306306 }
307307
308- function onDocumentMouseMove ( event ) {
308+ function onMouseMove ( event , touch ) {
309309 event . preventDefault ( ) ;
310310
311311 if ( ! isMouseDown ) return ;
312312
313+ const clientX = touch ? event . touches [ 0 ] . clientX : event . clientX ;
314+ const clientY = touch ? event . touches [ 0 ] . clientY : event . clientY ;
315+
313316 positionTickNumbers ( hasAxes , tickNumbers , ticks , camera , canvasSize , maxSize ) ;
314317
315318 if ( event . shiftKey ) { // pan
316319 if ( ! isShiftDown ) {
317320 isShiftDown = true ;
318- onMouseDownPosition [ 0 ] = event . clientX ;
319- onMouseDownPosition [ 1 ] = event . clientY ;
321+ onMouseDownPosition [ 0 ] = clientX ;
322+ onMouseDownPosition [ 1 ] = clientY ;
320323 autoRescale = false ;
321324 container . style . cursor = 'move' ;
322325 }
@@ -332,17 +335,17 @@ export default function (
332335 . normalize ( )
333336 . cross ( cameraX ) ;
334337
335- focus . x = onMouseDownFocus . x + ( radius / canvasSize ) * ( cameraX . x * ( onMouseDownPosition [ 0 ] - event . clientX ) + cameraY . x * ( onMouseDownPosition [ 1 ] - event . clientY ) ) ;
336- focus . y = onMouseDownFocus . y + ( radius / canvasSize ) * ( cameraX . y * ( onMouseDownPosition [ 0 ] - event . clientX ) + cameraY . y * ( onMouseDownPosition [ 1 ] - event . clientY ) ) ;
337- focus . z = onMouseDownFocus . z + ( radius / canvasSize ) * ( cameraY . z * ( onMouseDownPosition [ 1 ] - event . clientY ) ) ;
338+ focus . x = onMouseDownFocus . x + ( radius / canvasSize ) * ( cameraX . x * ( onMouseDownPosition [ 0 ] - clientX ) + cameraY . x * ( onMouseDownPosition [ 1 ] - clientY ) ) ;
339+ focus . y = onMouseDownFocus . y + ( radius / canvasSize ) * ( cameraX . y * ( onMouseDownPosition [ 0 ] - clientX ) + cameraY . y * ( onMouseDownPosition [ 1 ] - clientY ) ) ;
340+ focus . z = onMouseDownFocus . z + ( radius / canvasSize ) * ( cameraY . z * ( onMouseDownPosition [ 1 ] - clientY ) ) ;
338341
339342 updateCameraPosition ( ) ;
340343 } else if ( event . ctrlKey ) { // zoom
341344 if ( ! isCtrlDown ) {
342345 isCtrlDown = true ;
343346 onCtrlDownFov = camera . fov ;
344- onMouseDownPosition [ 0 ] = event . clientX ;
345- onMouseDownPosition [ 1 ] = event . clientY ;
347+ onMouseDownPosition [ 0 ] = clientX ;
348+ onMouseDownPosition [ 1 ] = clientY ;
346349 autoRescale = false ;
347350 container . style . cursor = 'crosshair' ;
348351 }
@@ -358,16 +361,16 @@ export default function (
358361 camera . updateProjectionMatrix ( ) ;
359362 } else { // spin
360363 if ( isCtrlDown || isShiftDown ) {
361- onMouseDownPosition [ 0 ] = event . clientX ;
362- onMouseDownPosition [ 1 ] = event . clientY ;
364+ onMouseDownPosition [ 0 ] = clientX ;
365+ onMouseDownPosition [ 1 ] = clientY ;
363366 isShiftDown = false ;
364367 isCtrlDown = false ;
365368 }
366369
367370 container . style . cursor = 'grabbing' ;
368371
369- phi = onMouseDownPhi + 2 * Math . PI * ( onMouseDownPosition [ 0 ] - event . clientX ) / canvasSize ;
370- theta = onMouseDownTheta + 2 * Math . PI * ( onMouseDownPosition [ 1 ] - event . clientY ) / canvasSize ;
372+ phi = onMouseDownPhi + 2 * Math . PI * ( onMouseDownPosition [ 0 ] - clientX ) / canvasSize ;
373+ theta = onMouseDownTheta + 2 * Math . PI * ( onMouseDownPosition [ 1 ] - clientY ) / canvasSize ;
371374
372375 // This prevents spinning over the poles by keeping the angle
373376 // in the range [1e-12, Pi - 1e-12].
@@ -383,7 +386,7 @@ export default function (
383386 render ( ) ;
384387 }
385388
386- function onDocumentMouseUp ( event ) {
389+ function onMouseUp ( event ) {
387390 event . preventDefault ( ) ;
388391
389392 isMouseDown = false ;
@@ -397,9 +400,14 @@ export default function (
397400 }
398401
399402 // bind mouse events
400- container . addEventListener ( 'mousemove' , onDocumentMouseMove ) ;
401- container . addEventListener ( 'mousedown' , onDocumentMouseDown ) ;
402- container . addEventListener ( 'mouseup' , onDocumentMouseUp ) ;
403+ container . addEventListener ( 'mousemove' , ( event ) => onMouseMove ( event , false ) ) ;
404+ container . addEventListener ( 'touchmove' , ( event ) => onMouseMove ( event , true ) ) ;
405+
406+ container . addEventListener ( 'mousedown' , ( event ) => onMouseDown ( event , false ) ) ;
407+ container . addEventListener ( 'touchstart' , ( event ) => onMouseDown ( event , true ) ) ;
408+
409+ container . addEventListener ( 'mouseup' , onMouseUp ) ;
410+ container . addEventListener ( 'touchend' , onMouseUp ) ;
403411
404412 window . addEventListener ( 'resize' , ( ) => {
405413 canvasSize = Math . min ( maxSize , window . innerWidth * innerWidthMultiplier ) ;
0 commit comments