@@ -13,15 +13,12 @@ var p5 = require('../core/core');
1313var constants = require ( '../core/constants' ) ;
1414
1515/*
16- * These are helper vars that store the mouseX and mouseY vals
17- * between the time that a mouse event happens and the next frame
18- * of draw. This is done to deal with the asynchronicity of event
19- * calls interacting with the draw loop. When a mouse event occurs
20- * the _nextMouseX/Y vars are updated, then on each call of draw, mouseX/Y
21- * and pmouseX/Y are updated using the _nextMouseX/Y vals.
16+ * This is a flag which is false until the first time
17+ * we receive a mouse event. The pmouseX and pmouseY
18+ * values will match the mouseX and mouseY values until
19+ * this interaction takes place.
2220 */
23- p5 . prototype . _nextMouseX = 0 ;
24- p5 . prototype . _nextMouseY = 0 ;
21+ p5 . prototype . _hasMouseInteracted = false ;
2522
2623/**
2724 * The system variable mouseX always contains the current horizontal
@@ -312,27 +309,32 @@ p5.prototype.mouseIsPressed = false;
312309p5 . prototype . isMousePressed = false ; // both are supported
313310
314311p5 . prototype . _updateNextMouseCoords = function ( e ) {
312+ var x = this . mouseX ;
313+ var y = this . mouseY ;
315314 if ( e . type === 'touchstart' ||
316315 e . type === 'touchmove' ||
317316 e . type === 'touchend' || e . touches ) {
318- this . _setProperty ( '_nextMouseX' , this . _nextTouchX ) ;
319- this . _setProperty ( '_nextMouseY' , this . _nextTouchY ) ;
320- } else {
321- if ( this . _curElement !== null ) {
322- var mousePos = getMousePos ( this . _curElement . elt , e ) ;
323- this . _setProperty ( '_nextMouseX' , mousePos . x ) ;
324- this . _setProperty ( '_nextMouseY' , mousePos . y ) ;
325- }
317+ x = this . touchX ;
318+ y = this . touchY ;
319+ } else if ( this . _curElement !== null ) {
320+ var mousePos = getMousePos ( this . _curElement . elt , e ) ;
321+ x = mousePos . x ;
322+ y = mousePos . y ;
326323 }
324+ this . _setProperty ( 'mouseX' , x ) ;
325+ this . _setProperty ( 'mouseY' , y ) ;
327326 this . _setProperty ( 'winMouseX' , e . pageX ) ;
328327 this . _setProperty ( 'winMouseY' , e . pageY ) ;
328+ if ( ! this . _hasMouseInteracted ) {
329+ // For first draw, make previous and next equal
330+ this . _updateMouseCoords ( ) ;
331+ this . _setProperty ( '_hasMouseInteracted' , true ) ;
332+ }
329333} ;
330334
331335p5 . prototype . _updateMouseCoords = function ( ) {
332336 this . _setProperty ( 'pmouseX' , this . mouseX ) ;
333337 this . _setProperty ( 'pmouseY' , this . mouseY ) ;
334- this . _setProperty ( 'mouseX' , this . _nextMouseX ) ;
335- this . _setProperty ( 'mouseY' , this . _nextMouseY ) ;
336338 this . _setProperty ( 'pwinMouseX' , this . winMouseX ) ;
337339 this . _setProperty ( 'pwinMouseY' , this . winMouseY ) ;
338340} ;
0 commit comments