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

After update to Chrome 56, mousePressed is having issues

Open jacorre opened this issue 9 years ago • 22 comments

Everything was working fine for a while with a program I have until Chrome was recently updated to v56. On the desktop, it still seems to work fine, but if you're on mobile or use the emulator in the browser, there seems to be an issue with mousePressed.

When I press anywhere on the canvas, I get the following in the console:

Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

Uncaught TypeError: Cannot read property 'clientX' of undefined at d (p5.min.js:5) at e._updateNextMouseCoords (p5.min.js:5) at e._ontouchend (p5.min.js:6)

I do return false at the end of the mousePressed function. If I comment that out, I no longer receive the warning about preventDefault but I still get the Uncaught TypeError.

jacorre avatar Feb 18 '17 15:02 jacorre

which version of p5 are you using? I think this should be fixed in the most recent version 0.5.7

lmccart avatar Feb 18 '17 15:02 lmccart

I was using 0.5.6. I updated to 0.5.7 and the Uncaught TypeError is no longer appearing. However, the issue about the "Unable to preventDefault" is still appearing.

From my testing, I believe that's impacting the mousePressed behavior to act like it's doing two mouse presses instead of one. This only appears to happen with mobile.

jacorre avatar Feb 18 '17 17:02 jacorre

Did some further testing. Didn't notice any issues on iPad with Chrome or Safari. Seems to be an issue with Android on Chrome. Treats a tap as two.

jacorre avatar Feb 19 '17 03:02 jacorre

Set up a simple example at http://codepen.io/jacorre/pen/zNVBvy.

Tapping canvas increases count by two when it should be one.

jacorre avatar Feb 19 '17 17:02 jacorre

Has anyone else been able to reproduce this on Chrome for Android? Please view my pen for an example. I have two applications that have been impacted by this and I'd really like to resolve this soon if possible. Thanks in advance for any help provided.

jacorre avatar Feb 21 '17 19:02 jacorre

Hi,

It seems that to make life easier, browser also populates both events: mouseEvent and touchEvent. If you put return false; in the end of mousePressed function, it will start working for mobile. It is still counting 2 in the browser emulation (Since you are manipulating it with a mouse).

P5 tries to make sure both of these also. Unfortunately Chrome fires 2 events. So p5 will fire 2 events also. That is the reason, it is still broken in chrome emulator.

reijovosu avatar Feb 21 '17 20:02 reijovosu

I forgot to include return false in my pen example. I updated the example.

If you are using Chrome on a mobile device for Android, it still increases the count by 2. It wasn't doing that prior to the browser update to Chrome 56. What is different that could be causing that? Is it a bug with P5? Is it a bug with Chrome?

Thanks!

jacorre avatar Feb 21 '17 20:02 jacorre

  1. Your codepen works with my android.

  2. It is a bit of both. But mainly it is chrome, that fires 2 events on simulation/emulator mode. As I said on mobile device it should be fine. Perhaps your code is cached in the device and not updated yet.

reijovosu avatar Feb 21 '17 20:02 reijovosu

What version of Chrome do you have for Android? I cleared my cache and tried it again and the count still increases by 2.

jacorre avatar Feb 21 '17 20:02 jacorre

Yep. I updated the chrome on mobile and got the same thing.

To others: latest chrome for mobile is fireing touch and mouse event. Here is a snippet to test it:

var cnt = 0;
var eArr=[];

function setup () {
  createCanvas(windowWidth,windowHeight);
}

function mousePressed (e) {
    console.log(e.constructor.name);
	cnt++;
    eArr.push(e.constructor.name);
    return false;
}

function draw () {
  background("#fff");	
  textSize(32);
  text(cnt, 10, 30);
  for(var s=0; s<eArr.length; s++){
       text(eArr[s],10,100+(s*40));
  }
}

I don't have iOS device atm, but as I could see in the XCode simulator, that only touch event is fired. This will change a lot of projects for Android devices.

reijovosu avatar Feb 21 '17 20:02 reijovosu

@jacorre to "hack" your issue atm. You can add a blank touchStarted event. So that p5 will ignore it. And this should work on all devices.

var cnt = 0;

function setup () {
  createCanvas(windowWidth,windowHeight);
}

function mousePressed () {
  cnt++;
  return false;
}

function touchStarted(){
  return false;
}

function draw () {
  background("#fff");	
  textSize(32);
  text(cnt, 10, 30);
}

reijovosu avatar Feb 21 '17 21:02 reijovosu

That's a good workaround so far from what I can tell. Thanks!

jacorre avatar Feb 21 '17 21:02 jacorre

Using Chrome 56 and updated p5js v0.5.7 February 08, 2017, and returning false for touchStarted() and mousePressed() functions I still see: A Warning: Unable to preventDefault inside passive event listener due to target being treated as passive. An Error : Uncaught TypeError: Cannot read property 'clientX' of undefined

qrencha avatar Feb 21 '17 21:02 qrencha

Hmm, p5 has an interesting model where it delegates touchstart to either mousePressed or touchStarted calls. I presume to fix this you probably want to add touch-action: none to your canvas if you are always going to return false.

it could be as simple as adding canvas { touch-action: none } to your CSS albeit I'm not entirely sure exactly how createCanvas works.

@RByers

dtapuska avatar Feb 23 '17 19:02 dtapuska

Hello,

p5 is meant for beginners to make cool animations, simple games and art projects with nice mathematics engine built in. So this feature is actually very useful and logical.

Before people could make a small game while using only one function mousePressed or touchStarted to make it work on every platform. The events were on the same line. So you can escape them with e.preventDefault. Even if you are using 2 different functions.

Atm there is no escaping from mousePressed in chrome mobile. "touch-action: none" does not help with this situation, because it handles panning and zooming.

The problem lies on tap. When you drag, the mousePressed does not fire.

Example problematic project: You have a game with free floating random circles when you tap a circle, it pops. But you count touches and hits. Every time you touch a screen touches count goes up by 2 and hits perhaps also - depends on timing.

To escape that it is necessary to write custom code to Chrome mobile (welcome IE6).

reijovosu avatar Feb 23 '17 19:02 reijovosu

For that scenario specifying canvas { touch-action: none; } and calling preventDefault on touchend events (to suppress 'mouse' / 'click' events) should be the right solution that works everywhere.

RByers avatar Feb 23 '17 19:02 RByers

I failed to mention, this problem is not p5 specific. Native DOM behaves same way when touchstart and mousedown event is listening.

reijovosu avatar Feb 23 '17 19:02 reijovosu

Ok handling touchend event in p5 seemed to fix the the problem.

reijovosu avatar Feb 23 '17 20:02 reijovosu

Example projects where I noticed the issue:

  • Game where tapping on screen would decrease chances by 2 instead of 1.
  • Application where a toggle menu option would open and then immediately close when tapping.

Thanks to everyone who has helped look into this!

jacorre avatar Feb 23 '17 21:02 jacorre

thanks @feedzh I think your fix has resolved this issue. if anyone is still have issues after the next release please feel free to reopen this one.

lmccart avatar Mar 14 '17 23:03 lmccart