tldraw/components/canvas/canvas.tsx

66 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-05-09 13:04:42 +00:00
import styled from "styles"
2021-05-10 12:16:57 +00:00
import React, { useCallback, useRef } from "react"
2021-05-09 13:04:42 +00:00
import useZoomEvents from "hooks/useZoomEvents"
2021-05-09 21:22:25 +00:00
import useCamera from "hooks/useCamera"
import Page from "./page"
2021-05-10 12:16:57 +00:00
import Brush from "./brush"
import state from "state"
import Bounds from "./bounds/bounding-box"
import BoundsBg from "./bounds/bounds-bg"
2021-05-13 06:44:52 +00:00
import inputs from "state/inputs"
2021-05-09 13:04:42 +00:00
export default function Canvas() {
const rCanvas = useRef<SVGSVGElement>(null)
const rGroup = useRef<SVGGElement>(null)
const events = useZoomEvents(rCanvas)
2021-05-09 21:22:25 +00:00
useCamera(rGroup)
2021-05-09 13:04:42 +00:00
2021-05-10 12:16:57 +00:00
const handlePointerDown = useCallback((e: React.PointerEvent) => {
rCanvas.current.setPointerCapture(e.pointerId)
state.send("POINTED_CANVAS", inputs.pointerDown(e, "canvas"))
2021-05-10 12:16:57 +00:00
}, [])
const handlePointerMove = useCallback((e: React.PointerEvent) => {
2021-05-13 06:44:52 +00:00
state.send("MOVED_POINTER", inputs.pointerMove(e))
2021-05-10 12:16:57 +00:00
}, [])
const handlePointerUp = useCallback((e: React.PointerEvent) => {
rCanvas.current.releasePointerCapture(e.pointerId)
2021-05-13 06:44:52 +00:00
state.send("STOPPED_POINTING", { id: "canvas", ...inputs.pointerUp(e) })
2021-05-10 12:16:57 +00:00
}, [])
2021-05-09 13:04:42 +00:00
return (
2021-05-10 12:16:57 +00:00
<MainSVG
ref={rCanvas}
{...events}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
>
2021-05-09 13:04:42 +00:00
<MainGroup ref={rGroup}>
2021-05-12 22:08:53 +00:00
<BoundsBg />
2021-05-09 21:22:25 +00:00
<Page />
2021-05-12 22:08:53 +00:00
<Bounds />
2021-05-10 12:16:57 +00:00
<Brush />
2021-05-09 13:04:42 +00:00
</MainGroup>
</MainSVG>
)
}
const MainSVG = styled("svg", {
position: "fixed",
top: 0,
left: 0,
width: "100%",
height: "100%",
touchAction: "none",
zIndex: 100,
2021-05-23 13:46:04 +00:00
"& *": {
userSelect: "none",
},
2021-05-09 13:04:42 +00:00
})
const MainGroup = styled("g", {})