Skip to content
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

Merged

Conversation

gaishimo
Copy link
Contributor

@gaishimo gaishimo commented Aug 11, 2022

SpringBackTouch example freezes if you release your finger immediately after tapping without dragging. It seems to be caused by undefined velocity values being passed to runSpring in onEnd.

I fixed the example to return if the values are undefined.
I fixed useTouchHandler.

fixes #788

Example

@gaishimo gaishimo force-pushed the fix-fleeze-of-spring-back-touch-example branch from a542b9a to 84607e8 Compare August 11, 2022 07:39
@gaishimo gaishimo force-pushed the fix-fleeze-of-spring-back-touch-example branch 2 times, most recently from 9f816f9 to 7d0cc37 Compare August 11, 2022 07:51
@@ -48,6 +48,9 @@ export const SpringBackTouchAnimation = () => {
rectY.current = y - offsetY.current;
},
onEnd: ({ velocityX, velocityY }) => {
if (velocityX == null || velocityY == null) {
return;
Copy link
Collaborator

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.

Copy link
Collaborator

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?

Copy link
Collaborator

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!

Copy link
Contributor Author

@gaishimo gaishimo Aug 12, 2022

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.

@gaishimo gaishimo force-pushed the fix-fleeze-of-spring-back-touch-example branch from 7f85740 to 22a8b91 Compare August 12, 2022 01:23
@gaishimo gaishimo changed the title Fix freeze problem of SpringBackTouch example undefined velocities are passed to onTouch's onEnd event Aug 12, 2022
@gaishimo gaishimo force-pushed the fix-fleeze-of-spring-back-touch-example branch from 22a8b91 to e502c6b Compare August 12, 2022 06:08
Copy link
Collaborator

@chrfalch chrfalch left a 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!

TouchHandlers,
TouchHandler,
TouchInfo,
ExtendedTouchInfo, TouchHandler, TouchHandlers, TouchInfo
Copy link
Collaborator

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 :)

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,
Copy link
Collaborator

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!

Copy link
Collaborator

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.

  1. 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.
  2. 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).

Copy link
Collaborator

@chrfalch chrfalch Aug 12, 2022

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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"?
  2. Got it and it makes sense.

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@wcandillon
Copy link
Collaborator

@gaishimo We think the fix should look like this: #794 could you update this PR accordingly? We will merge it and release it. Thank you for your contribution.

@gaishimo gaishimo force-pushed the fix-fleeze-of-spring-back-touch-example branch from 21ed220 to 9cfdaf3 Compare August 12, 2022 08:44
@gaishimo gaishimo force-pushed the fix-fleeze-of-spring-back-touch-example branch from 9cfdaf3 to 946534b Compare August 12, 2022 08:45
@wcandillon
Copy link
Collaborator

awesome thks :)

@wcandillon wcandillon self-requested a review August 12, 2022 08:45
@wcandillon wcandillon merged commit f960a35 into Shopify:main Aug 12, 2022
@gaishimo
Copy link
Contributor Author

@wcandillon OK, thanks for the opportunity to contribute to this good library 👍

@wcandillon
Copy link
Collaborator

Thank you for fixing this issue, let us know if you have any questions about Skia 🙌🏼

@gaishimo gaishimo deleted the fix-fleeze-of-spring-back-touch-example branch August 12, 2022 09:32
@thespacemanatee
Copy link
Contributor

thespacemanatee commented Aug 12, 2022

@chrfalch This change is causing this crash for us:

My TouchHandler:

const onTouch = useTouchHandler({
  onStart: () => {
    if (!disabled) {
      runTiming(pressed, 1, { duration: 150 })
    }
  },
  onEnd: () => {
    if (!disabled) {
      runTiming(pressed, 0, { duration: 150 })
    }
  },
})

@wcandillon
Copy link
Collaborator

sorry about that, we have a hot fix for it at #798

@gaishimo
Copy link
Contributor Author

Sorry, I used the code of reference PR as is when updating the PR... I should have checked after modifying it 🙇🙏

@wcandillon
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants