trouble with slider when sketch has a touchStarted() function defined
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. :)
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.
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.
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, 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?
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.
Yes sorry, === false. And yes @lmcart exactly. Only prevent on false (not falsey) and otherwise allow. Reminiscent of the olden days of onclick attributes.
Wont trying to access an undefined variable result into a runtime error?
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
Yes, it wont :). Was thinking of some other reference.
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.
Thanks @brysonian
just switched this around and added documentation and examples, I think this should be set for now.