您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

useCamera.ts 903B

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