You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useZoomPanEffect.ts 751B

1234567891011121314151617181920212223242526272829303132
  1. import React, { useEffect } from "react"
  2. import state from "state"
  3. /**
  4. * When the state's camera changes, update the transform of
  5. * the SVG group to reflect the correct zoom and pan.
  6. * @param ref
  7. */
  8. export default function useZoomPanEffect(
  9. ref: React.MutableRefObject<SVGGElement>
  10. ) {
  11. useEffect(() => {
  12. let { camera } = state.data
  13. return state.onUpdate(({ data }) => {
  14. const g = ref.current
  15. if (!g) return
  16. const { point, zoom } = data.camera
  17. if (point !== camera.point || zoom !== camera.zoom) {
  18. console.log("changed!")
  19. g.setAttribute(
  20. "transform",
  21. `scale(${zoom}) translate(${point[0]} ${point[1]})`
  22. )
  23. }
  24. camera = data.camera
  25. })
  26. }, [state])
  27. }