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

trouble with slider when sketch has a touchStarted() function defined

Open dnassler opened this issue 12 years ago • 12 comments

It seems that the slider control does not work properly when testing on the iPad if there is a touchPressed() function defined. The touchPressed function captures the touch event and prevents the slider from working when it is the thing being touched. Even if I try to check if i'm clicking on the slider in the touchPressed() function and return and do nothing else, the slider will not slide. :)

dnassler avatar Sep 11 '14 03:09 dnassler

Asuming when you say touchPressed(), you mean touchStarted().This is because we call preventDefault for touch when touchStarted() is defined. This is to make a new user not take the headache of doing preventDefault (@lmccart might better explain this). We have added the functionality which enables the user to prevent "preventDefault" by returning true from the touchStarted() function. Thanks @dnassler for bringing this up. I think this should be documented somewhere. Will do it soon.

hrishit avatar Sep 11 '14 17:09 hrishit

yes! although returning true actually allows the default to bubble through. (returning false or nothing prevents the default when the method is defined)

so you could do something like:

function touchStarted() {
  // do stuff
  return true;
}

function touchMoved() {
  // do stuff
  return true;
}

to allow the defaults to occur. does feel a little wonky on mobile though, if you have thoughts on how this could be improved please let us know.

lmccart avatar Sep 12 '14 00:09 lmccart

It looks like any "falsey" value (including undefined) will prevent the default https://github.com/lmccart/p5.js/blob/master/lib/p5.js#L3032

Should this be:

executeDefault = context.touchMoved(e);
if (executeDefault !== false) {
  e.preventDefault();
}

So that only returning "false" prevents the default? To me this feels more in-line with the behavior of javascript's onclick etc.

brysonian avatar Sep 12 '14 04:09 brysonian

@brysonian, you mean

executeDefault = context.touchMoved(e);
if (executeDefault === false) {
  e.preventDefault();
}

Yes, that makes sense but we cant just check for the value to be false as the user's function might not return anything at all. Plus, the behaviour is different for Mouse and Touch.... For mouse returning true or nothing will execute Default and returning false will preventDefault. For touch returning true will execute Default and returning false or nothing will preventDefault.

Not sure if this would be very apparent to the user or not, at least not without some documentation on this. Is there any better way we can handle this?

hrishit avatar Sep 12 '14 14:09 hrishit

hmm we could distinguish between undefined vs false, so @hrishit I think your snippet above would work actually. I agree with both of you, a default of allowing default seems to make more sense. then returning false would prevent it. returning true would allow the default also, though this line wouldn't be necessary as it would happen by default without any return.

I think just adding a line explaining this for each of the functions in the reference should make it clear enough.

lmccart avatar Sep 12 '14 14:09 lmccart

Yes sorry, === false. And yes @lmcart exactly. Only prevent on false (not falsey) and otherwise allow. Reminiscent of the olden days of onclick attributes.

brysonian avatar Sep 12 '14 14:09 brysonian

Wont trying to access an undefined variable result into a runtime error?

hrishit avatar Sep 12 '14 14:09 hrishit

I believe it should be fine... for example, a small test:

function returnFalse() {
  return false;
}

function returnNothing() { 
}

console.log(returnFalse() === false); // true
console.log(returnNothing() === false); // false

vs

console.log(returnFalse() === true); // false
console.log(returnNothing() === true); // false


lmccart avatar Sep 12 '14 14:09 lmccart

Yes, it wont :). Was thinking of some other reference.

hrishit avatar Sep 12 '14 15:09 hrishit

Not in this case because executeDefault is declared (with var), but it's value (and type) is undefined. Then the === will type and value check for false, rather than castable to false.

brysonian avatar Sep 12 '14 15:09 brysonian

Thanks @brysonian

hrishit avatar Sep 12 '14 15:09 hrishit

just switched this around and added documentation and examples, I think this should be set for now.

lmccart avatar Sep 25 '14 23:09 lmccart