p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

mouse[XY] in mousePressed as alternative to touchStarted

Open ynaoto opened this issue 12 years ago • 9 comments

Hi, As documented, mousePressed handler is called instead of touchStarted on the touch devices if touchStarted is not defined, but it seems that mouseX and mouseY are not set as expected.

ynaoto avatar Aug 11 '14 00:08 ynaoto

thanks very good point, I will look into this!

lmccart avatar Aug 11 '14 19:08 lmccart

I would like to work on this. Did the initial investigation and looks like this is happening as we are not calling setMouseButton method when we are using mousePressed for a touch event. This issue will arise in a vice versa situation as well (should touchStarted be used for a mouseclick). I thought just calling setMouseButton method would be enough but realised that the method itself needs some rectification to make this work properly. I'll update the thread with further investigations soon. @lmccart do you have any suggestion? in case you already had something in mind. Thanks.

hrishit avatar Aug 15 '14 12:08 hrishit

Had one doubt

onmousedown: var context = this._isGlobal ? window : this; this._setProperty('isMousePressed', true); this._setProperty('mouseIsPressed', true); this.setMouseButton(e);

ontouchstart: var context = this._isGlobal ? window : this; context.setTouchPoints(e);

why are we using the global context for touch and not for mouseclick?

hrishit avatar Aug 15 '14 14:08 hrishit

good question. there is a bit of a typo here, context.setTouchPoints(e) should really be this.setTouchPoints(e). making this change now...

basically what's happening here is we're accounting for two different modes, consider these two sketches:

global

function setup() {
}

function mousePressed() {
  // blah
}

instance

var s = function(sketch) {
  sketch.setup = function() {
  };

  sketch.mousePressed = function() {
    // blah
  };
}

so in this part of the p5.js library, we're determining whether we're searching for a windowbound function called mousePressed, or one bound to the particular instance. then we handle it accordingly. however, functions like setMouseButton and setTouchPoints, etc, are not user defined, they are part of the p5 instance in either case, so we can just call this.setTouchPoints or this.updateMouseCoords:

    var context = this._isGlobal ? window : this;
    if (typeof context.mousePressed === 'function') {
      //e.preventDefault();
      context.mousePressed(e);
    } else if (typeof context.touchStarted === 'function') {
      e.preventDefault();
      context.touchStarted(e);
    }

lmccart avatar Aug 15 '14 21:08 lmccart

you are welcome to tackle this issue, I think the key is to think about use cases. you're proposing just calling both updateTouchPoints and updateMouseCoords with either a touch or mouse click, if I understand correctly? I think this makes sense, we just need to think about whether there's ever some case where you might be using both and this behavior would be confusing? I can't really think of anything so I think your solution should be fine.

lmccart avatar Aug 15 '14 21:08 lmccart

Thanks for the replies. About calling both updateTouchPoints and updateMouseCoords, I think we should call just the relevant functions. i.e. inside mousepressed call updateMouseCoords by default and call updateTouchPoints only when mousepressed is triggered though a touch event, not when mousepressed is triggered through a mouseclick.

Plus I think we will require similar fixes for onmousemove/ontouchmove and onmouseup/ontouchend as well.

hrishit avatar Aug 16 '14 11:08 hrishit

p5.prototype.ontouchend = function(e) { var context = this._isGlobal ? window : this; this.setTouchPoints(e);

ontouchend method sets the touchpoint, wondering why this would be required as the touchpoint would have already gotten updated by ontouchmove method, right?

hrishit avatar Aug 16 '14 11:08 hrishit

re: calling only when triggered by touch etc. ah of course, makes sense! yes you're right about the setTouchPoints in ontouchend being unnecessary. good catch, you can go ahead and remove that.

lmccart avatar Aug 16 '14 15:08 lmccart

I've made the changes/fixes and created a pull request on master #319

hrishit avatar Aug 17 '14 07:08 hrishit