Skip to content

Looking at creating a shake animation on user pressing ENTER #1746

Discussion options

You must be logged in to vote

Consider this:

const { x, y } = useSpring({
    from: { x: 0, y: 0 },
    to: shakeState.shake ? { x: 1, y: 1 } : { x: 0, y: 0 },
    onRest: () => {
       if(shakeState.shake){
        setShakeState((shakeState) => ({
          ...shakeState,
          shake: false,
        }));
      }
  });

  const xInterpolate = x.to(
    [0, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 1],
    [shakeXStart, shakeXEnd, shakeXStart, shakeXEnd, shakeXStart, shakeXEnd, shakeXStart, shakeXEnd],
  );

  const yInterpolate = y.to(
    [0, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 1],
    [shakeYStart, shakeYEnd, shakeYStart, shakeYEnd, shakeYStart, shakeYEnd, shakeYStart, shakeYEnd],
  );

// Update state when ready to …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@miguelespinoza
Comment options

Answer selected by miguelespinoza
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants