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.

useCamera.ts 708B

1234567891011121314151617181920212223242526272829
  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 useCamera(ref: React.MutableRefObject<SVGGElement>) {
  9. useEffect(() => {
  10. let { camera } = state.data
  11. return state.onUpdate(({ data }) => {
  12. const g = ref.current
  13. if (!g) return
  14. const { point, zoom } = data.camera
  15. if (point !== camera.point || zoom !== camera.zoom) {
  16. g.setAttribute(
  17. "transform",
  18. `scale(${zoom}) translate(${point[0]} ${point[1]})`
  19. )
  20. }
  21. camera = data.camera
  22. })
  23. }, [state])
  24. }