Skip to content

Commit

Permalink
recursive useTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Apr 23, 2024
1 parent 101135b commit 5ba3486
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions hooks/use-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,26 @@ export const TransformProvider = forwardRef(function TransformProvider(
{ children },
ref,
) {
const parentTransformRef = useRef(structuredClone(DEFAULT_TRANSFORM))
const transformRef = useRef(structuredClone(DEFAULT_TRANSFORM))

const getTransform = useCallback(() => transformRef.current, [])
const getTransform = useCallback(() => {
const transform = structuredClone(parentTransformRef.current)

transform.translate.x += transformRef.current.translate.x
transform.translate.y += transformRef.current.translate.y
transform.translate.z += transformRef.current.translate.z

transform.rotate.x += transformRef.current.rotate.x
transform.rotate.y += transformRef.current.rotate.y
transform.rotate.z += transformRef.current.rotate.z

transform.scale.x *= transformRef.current.scale.x
transform.scale.y *= transformRef.current.scale.y
transform.scale.z *= transformRef.current.scale.z

return transform
}, [])

const callbacksRefs = useRef([])

Expand All @@ -62,8 +79,8 @@ export const TransformProvider = forwardRef(function TransformProvider(
}, [])

const update = useCallback(() => {
callbacksRefs.current.forEach((callback) => callback(transformRef.current))
}, [])
callbacksRefs.current.forEach((callback) => callback(getTransform()))
}, [getTransform])

const setTranslate = useCallback(
(x = 0, y = 0, z = 0) => {
Expand Down Expand Up @@ -110,6 +127,18 @@ export const TransformProvider = forwardRef(function TransformProvider(
[update],
)

useTransform(
(transform) => {
parentTransformRef.current = structuredClone(transform)
update()

// if (debug) {
// console.log(parentTransformRef.current.translate)
// }
},
[update],
)

useImperativeHandle(ref, () => ({
setTranslate,
setRotate,
Expand Down

1 comment on commit 5ba3486

@github-actions
Copy link

Choose a reason for hiding this comment

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

"⚡️ Lighthouse report for the changes in this commit:

🟠 Performance: 89
🟢 Accessibility: 94
🟢 Best practices: 96
🟢 SEO: 92
🟠 PWA: 88

Lighthouse ran on https://satus-8qw3ivrec-darkroom-engineering.vercel.app/"

Please sign in to comment.