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

feature: add useAnimationLoop hook #129

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

AlenaAlyona
Copy link
Contributor

@AlenaAlyona AlenaAlyona commented Apr 6, 2023

useAnimationLoop hook is a wrapper around requestAnimationFrame function.
This hook will execute the passed callback function every frame.

src/hooks/useAnimationLoop/useAnimationLoop.test.tsx Outdated Show resolved Hide resolved
src/hooks/useAnimationLoop/useAnimationLoop.test.tsx Outdated Show resolved Hide resolved
src/hooks/useAnimationLoop/useAnimationLoop.test.tsx Outdated Show resolved Hide resolved
src/hooks/useAnimationLoop/useAnimationLoop.test.tsx Outdated Show resolved Hide resolved
@leroykorterink leroykorterink marked this pull request as ready for review May 15, 2023 19:47
Comment on lines +7 to +8
`useAnimationLoop` hook is a wrapper around requestAnimationFrame function. This hook will execute
the passed callback function every frame.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
`useAnimationLoop` hook is a wrapper around requestAnimationFrame function. This hook will execute
the passed callback function every frame.
`useAnimationLoop` hook is a wrapper around the `requestAnimationFrame` function. This hook will invoke
the passed `callback` function every frame.

Comment on lines +18 to +19
- `callback` - function accepts `delta` parameter which represents time passed since last
invocation;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- `callback` - function accepts `delta` parameter which represents time passed since last
invocation;
- `callback` - a function that receives a `delta` parameter which represents time passed since last
invocation, in milliseconds, as a [DOMHighResTimeStamp](https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp)

const [currentTimestamp, setCurrentTimestamp] = useState(Date.now);
const [isRunning, toggleIsRunning] = useToggle(true);
useAnimationLoop(() => {
setCurrentTimestamp(Date.now);
Copy link
Member

Choose a reason for hiding this comment

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

In this example, it might be more useful to do something (e.g. log) the delta that is passed, to better showcase the features of this hook.

* useAnimationLoop hook is a wrapper around requestAnimationFrame method.
* This hook will execute a callback function every next frame.
*
* @param callback - callback function with @param delta which represents time passed since last invocation
Copy link
Member

Choose a reason for hiding this comment

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

This docs (still) say delta, while the implementation (has changed?) returns the actual time.

When running an individual animationFrame, the time might be useful. When you running them in a loop, the delta is often more useful.
I don't mind also forwarding the timestamp, but I feel we should at least calculate and pass the delta.

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

3 participants