-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
undefined velocities are passed to onTouch's onEnd event #788
undefined velocities are passed to onTouch's onEnd event #788
Conversation
a542b9a
to
84607e8
Compare
9f816f9
to
7d0cc37
Compare
@@ -48,6 +48,9 @@ export const SpringBackTouchAnimation = () => { | |||
rectY.current = y - offsetY.current; | |||
}, | |||
onEnd: ({ velocityX, velocityY }) => { | |||
if (velocityX == null || velocityY == null) { | |||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to find out why the touch handler passes null for these values - maybe that could help others as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be solved by looking in the file useTouchHandler.ts
on lines 59..60?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And thanks so much for submitting a PR, @gaishimo !! We really appreciate it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review 👍
I fixed onTouchHandler
.
83b8482
If the finger is released immediately, only one touch record is sent as below.
touches: [{"force": 0, "id": 5126886480, "timestamp": 1660266755.592, "type": 2, "x": 69, "y": 147}]
In this case, since velocity cannot be calculated, I think it is preferable to assume that it was not moving and should return zero.
7f85740
to
22a8b91
Compare
22a8b91
to
e502c6b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again :) I'll merge very soon!
package/src/views/useTouchHandler.ts
Outdated
TouchHandlers, | ||
TouchHandler, | ||
TouchInfo, | ||
ExtendedTouchInfo, TouchHandler, TouchHandlers, TouchInfo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we don't want changes in formatting and ordering in a PR, so this block could be left out since it is not really related to the fix. We'll leave it for now, just wanted to let you know :)
package/src/views/useTouchHandler.ts
Outdated
velocityX: prevVelocityRef.current[touch.id]?.x, | ||
velocityY: prevVelocityRef.current[touch.id]?.y, | ||
velocityX: prevVelocityRef.current[touch.id]?.x ?? 0, | ||
velocityY: prevVelocityRef.current[touch.id]?.y ?? 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect :) Thanks for finding this and providing a fix!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be a small clean-up there.
- The velocity is computed above and might be needed to be set to 0 if
timeDiffseconds === 0
. If should be done at the same place. - The velocity is computed and stored in x and y property of the previous touch, could it be velocityX and velocityY? Maybe it shouldn't be stored in the ref at all? It looks like we are calculating the velocity based on a velocity in the next frame (might be wrong).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1: So something like:
if (timeDiffseconds > 0) {
prevVelocityRef.current[touch.id] = {
x: distX / timeDiffseconds / PixelRatio.get(),
y: distY / timeDiffseconds / PixelRatio.get(),
};
} else {
prevVelocityRef.current[touch.id] = { x: 0, y: 0 }; // <- new
}
But wouldn't this be covered by the fix proposed in this PR?
2: We're using a ref to store the previous one so that we only calculate velocities when we move - on end/cancel we should use the previously calculated value - since end/cancel are called immediately (timeDiffSeconds === 0) after the last active.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Exactly and this is part of this PR right? Since the goal is to have this value always defined and the types to be able to statically catch such "bug"?
- Got it and it makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and yes :) So then we're good to go with this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. Thanks for your review.
21ed220
to
9cfdaf3
Compare
9cfdaf3
to
946534b
Compare
awesome thks :) |
@wcandillon OK, thanks for the opportunity to contribute to this good library 👍 |
Thank you for fixing this issue, let us know if you have any questions about Skia 🙌🏼 |
@chrfalch This change is causing this crash for us: My const onTouch = useTouchHandler({
onStart: () => {
if (!disabled) {
runTiming(pressed, 1, { duration: 150 })
}
},
onEnd: () => {
if (!disabled) {
runTiming(pressed, 0, { duration: 150 })
}
},
}) |
sorry about that, we have a hot fix for it at #798 |
Sorry, I used the code of reference PR as is when updating the PR... I should have checked after modifying it 🙇🙏 |
@gaishimo not at all, this was our mistake, but it was also a blessing in disguise as it helped us find a serious bug related to velocity computation. |
SpringBackTouch example freezes if you release your finger immediately after tapping without dragging. It seems to be caused by
undefined
velocity values being passed torunSpring
inonEnd.
I fixed the example to return if the values are undefined.I fixed
useTouchHandler
.fixes #788