-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
useAnimatedTransform
hook (#620)
Co-authored-by: dv-raghad-jamalaldeen <[email protected]> Co-authored-by: Holger Stitz <[email protected]>
- Loading branch information
1 parent
5e4dc82
commit 352ecbf
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* eslint-disable react-compiler/react-compiler */ | ||
import * as React from 'react'; | ||
import { ZoomTransform } from '../interfaces'; | ||
import { useSyncedRef } from '../../../hooks'; | ||
|
||
function linearInterpolate(startMatrix: ZoomTransform, endMatrix: ZoomTransform, t: number) { | ||
return startMatrix.map((startValue, index) => { | ||
const endValue = endMatrix[index]; | ||
|
||
if (endValue === undefined) { | ||
throw new Error('Supplied matrices are not of the same length'); | ||
} | ||
|
||
const cosT = (1 - Math.cos(t * Math.PI)) / 2; | ||
return startValue * (1 - cosT) + endValue * cosT; | ||
}); | ||
} | ||
|
||
/** | ||
* Hook that returns an animate function that can be used to animate between two zoom transforms (keyframes). | ||
* After calling animate, the onIntermediate callback will be called with the monitors refresh rate (requestAnimationFrame) | ||
* with the intermediate transform values (cosine interpolated). | ||
*/ | ||
export function useAnimatedTransform({ onIntermediate }: { onIntermediate: (intermediateTransform: ZoomTransform) => void }) { | ||
const stateRef = React.useRef({ | ||
start: undefined as ZoomTransform | undefined, | ||
end: undefined as ZoomTransform | undefined, | ||
t0: performance.now(), | ||
}); | ||
|
||
const animationFrameRef = React.useRef<number | undefined>(undefined); | ||
const onIntermediateRef = useSyncedRef(onIntermediate); | ||
|
||
const requestFrame = () => { | ||
animationFrameRef.current = requestAnimationFrame((t1) => { | ||
if (stateRef.current.start && stateRef.current.end) { | ||
const t = (t1 - stateRef.current.t0) / 1000; | ||
// End of animation | ||
if (t >= 1) { | ||
animationFrameRef.current = undefined; | ||
onIntermediateRef.current(stateRef.current.end); | ||
return; | ||
} | ||
|
||
const newMatrix = linearInterpolate(stateRef.current.start, stateRef.current.end, t); | ||
onIntermediateRef.current(newMatrix); | ||
|
||
requestFrame(); | ||
} | ||
}); | ||
}; | ||
|
||
const requestFrameRef = useSyncedRef(requestFrame); | ||
|
||
const animate = React.useCallback( | ||
(start: ZoomTransform, end: ZoomTransform) => { | ||
stateRef.current = { | ||
start, | ||
end, | ||
t0: performance.now(), | ||
}; | ||
|
||
if (animationFrameRef.current) { | ||
cancelAnimationFrame(animationFrameRef.current); | ||
} | ||
|
||
requestFrameRef.current(); | ||
}, | ||
[requestFrameRef], | ||
); | ||
|
||
React.useEffect(() => { | ||
return () => { | ||
if (animationFrameRef.current) { | ||
cancelAnimationFrame(animationFrameRef.current); | ||
} | ||
}; | ||
}, []); | ||
|
||
return { | ||
animate, | ||
}; | ||
} |