2021-05-09 13:04:42 +00:00
|
|
|
import React, { useEffect } from "react"
|
|
|
|
import state from "state"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the state's camera changes, update the transform of
|
|
|
|
* the SVG group to reflect the correct zoom and pan.
|
|
|
|
* @param ref
|
|
|
|
*/
|
2021-05-09 21:22:25 +00:00
|
|
|
export default function useCamera(ref: React.MutableRefObject<SVGGElement>) {
|
2021-05-09 13:04:42 +00:00
|
|
|
useEffect(() => {
|
|
|
|
let { camera } = state.data
|
|
|
|
|
|
|
|
return state.onUpdate(({ data }) => {
|
|
|
|
const g = ref.current
|
|
|
|
if (!g) return
|
|
|
|
|
|
|
|
const { point, zoom } = data.camera
|
|
|
|
|
|
|
|
if (point !== camera.point || zoom !== camera.zoom) {
|
|
|
|
g.setAttribute(
|
|
|
|
"transform",
|
|
|
|
`scale(${zoom}) translate(${point[0]} ${point[1]})`
|
|
|
|
)
|
2021-05-16 07:09:46 +00:00
|
|
|
|
|
|
|
localStorage.setItem("code_slate_camera", JSON.stringify(data.camera))
|
2021-05-09 13:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
camera = data.camera
|
|
|
|
})
|
|
|
|
}, [state])
|
|
|
|
}
|