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

Create useLerp hook #241

Open
VictorGa opened this issue Sep 25, 2023 · 3 comments
Open

Create useLerp hook #241

VictorGa opened this issue Sep 25, 2023 · 3 comments
Assignees

Comments

@VictorGa
Copy link

Lerping hook for simple transitions. Properties would be initial number, final number, duration, ease method.

@leroykorterink
Copy link
Collaborator

@VictorGa can you provide an example of an implementation? A simple lerp can be implemented in a function, don't really see the benefit to have a hook for it.

I agree that having these functions ready to use is useful but wouldn't recommend implementing it via a react hook.

@ReneDrie
Copy link
Contributor

ReneDrie commented Oct 13, 2023

The lerp as a function is already available (gsap.utils.interpolate). But I assume this does the animationFrame loop, and keeps updating the current value by interpolating to the target value.

I currently use something like this:

import { noop } from 'lodash-es';
import { useCallback, useRef } from 'react';
import { useRafLoop } from 'react-use';

export type LerpPoint = { x: number; y: number };
export type LerpValue = number | LerpPoint;

export function useLerp(
  value: LerpValue,
  target: LerpValue,
  interpolateValue = 0.1,
  callback: (value: LerpValue) => void = noop,
): (target: LerpValue) => void {
  const currentValue = useRef<LerpValue>(value);
  const targetValue = useRef<LerpValue>(target);
  const interpolate = useRef<number>(interpolateValue);

  useRafLoop(() => {
    if (typeof targetValue.current === 'number' || typeof currentValue.current === 'number') {
      currentValue.current = gsap.utils.interpolate(
        currentValue.current,
        targetValue.current,
        interpolate.current,
      );
    } else {
      currentValue.current.x = gsap.utils.interpolate(
        currentValue.current.x,
        targetValue.current.x,
        interpolate.current,
      );
      currentValue.current.y = gsap.utils.interpolate(
        currentValue.current.y,
        targetValue.current.y,
        interpolate.current,
      );
    }

    callback(currentValue.current);
  });

  return useCallback((updatedTarget: LerpValue) => {
    targetValue.current = updatedTarget;
  }, []);
}

@VictorGa
Copy link
Author

Yes, exactly that. I didn't mean the formula in a hook. Mostly a way to interpolate the values in an easier way using a hook for it, regardless if we use interpolate from gsap or a formula, could be handy. We have an example from @evertmonk. I'll create a PR for it soon.

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

No branches or pull requests

4 participants