Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

feat: pass physics values to onFrame callback #136

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __tests__/rAF/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('update', () => {
const mockMoverState: Partial<Entity> = {
velocity: [2, 0],
position: [5, 0],
acceleration: [-1.5, -2],
};

const element: StatefulAnimatingElement<
Expand Down
2 changes: 1 addition & 1 deletion src/animation/gravity2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export interface Gravity2DParams extends AnimationParams {
};
G?: number;
};
onUpdate: VectorSetter;
}

export interface Gravity2DController extends Controller {
Expand Down Expand Up @@ -112,6 +111,7 @@ export function gravity2D({
onUpdate({
position: state.mover.position,
velocity: state.mover.velocity,
acceleration: state.mover.acceleration,
});
};

Expand Down
11 changes: 7 additions & 4 deletions src/animation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import type { CSSProperties, RefObject } from 'react';
import type { vector as Vector } from '../core';
import type { entity as Entity } from '../forces';

export type VectorSetter = (values: {
interface MotionVectors {
position: Vector<number>;
velocity?: Vector<number>;
}) => void;
velocity: Vector<number>;
acceleration: Vector<number>;
}

export type VectorSetter = (motionVectors: MotionVectors) => void;

export type Listener = (
timestamp: DOMHighResTimeStamp,
Expand All @@ -30,7 +33,7 @@ export interface HooksParams {
pause?: boolean;
delay?: number;
repeat?: number;
onFrame?: (progress: number) => void;
onFrame?: (progress: number, motionVectors: MotionVectors) => void;
onAnimationComplete?: () => void;
disableHardwareAcceleration?: boolean;
reducedMotion?: {
Expand Down
17 changes: 13 additions & 4 deletions src/hooks/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { RefObject, MutableRefObject, CSSProperties } from 'react';

import { AnimationCache, PlayState, VectorSetter } from '../animation';
import {
AnimationCache,
HooksParams,
PlayState,
VectorSetter,
} from '../animation';
import type { CSSPairs, InterpolatedResult } from '../parsers';

interface OnUpdateParams<E extends HTMLElement | SVGElement> {
Expand All @@ -10,7 +15,7 @@ interface OnUpdateParams<E extends HTMLElement | SVGElement> {
ref: RefObject<E>;
i: number;
cache: MutableRefObject<AnimationCache>;
onFrame?: (progress: number) => void;
onFrame?: HooksParams['onFrame'];
}

/**
Expand All @@ -26,7 +31,11 @@ export const onUpdate = <E extends HTMLElement | SVGElement>({
i,
cache,
onFrame,
}: OnUpdateParams<E>): VectorSetter => ({ position }) => {
}: OnUpdateParams<E>): VectorSetter => ({
position,
velocity,
acceleration,
}) => {
interpolators.forEach(({ interpolator, property, values }) => {
const value = interpolator({
range: [0, maxPosition],
Expand All @@ -40,7 +49,7 @@ export const onUpdate = <E extends HTMLElement | SVGElement>({

if (onFrame) {
const progress = position[0] / maxPosition;
onFrame(progress);
onFrame(progress, { position, velocity, acceleration });
}

// Update the cache of derived animation values.
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useGravity2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
Controller,
Gravity2DController,
gravity2DDefaultConfig,
VectorSetter,
} from '../animation';

type UseGravity2DArgs = {
config?: Gravity2DParams['config'];
pause?: boolean;
delay?: number;
onFrame?: () => void;
onFrame?: VectorSetter;
onAnimationComplete?: () => void;
disableHardwareAcceleration?: boolean;
};
Expand Down Expand Up @@ -40,14 +41,16 @@ export const useGravity2D = <E extends HTMLElement | SVGElement = any>({
() =>
gravity2D({
config,
onUpdate: ({ position: [x, y] }) => {
onUpdate: ({ position, velocity, acceleration }) => {
moverRef.current &&
(moverRef.current.style.transform = `translate(${x}px, ${y}px) translate(-50%, -50%)${
(moverRef.current.style.transform = `translate(${position[0]}px, ${
position[1]
}px) translate(-50%, -50%)${
disableHardwareAcceleration ? '' : ' translateZ(0)'
}`);

if (onFrame) {
onFrame();
onFrame({ position, velocity, acceleration });
}
},
onComplete: () => {
Expand Down
1 change: 1 addition & 0 deletions src/rAF/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function update<C>({
element.onUpdate({
velocity: element.state.mover.velocity,
position: element.state.mover.position,
acceleration: element.state.mover.acceleration,
});
}
}
Expand Down