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.

canvas.tsx 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import styled from "styles"
  2. import { getPointerEventInfo } from "utils/utils"
  3. import React, { useCallback, useRef } from "react"
  4. import useZoomEvents from "hooks/useZoomEvents"
  5. import useCamera from "hooks/useCamera"
  6. import Page from "./page"
  7. import Brush from "./brush"
  8. import state from "state"
  9. export default function Canvas() {
  10. const rCanvas = useRef<SVGSVGElement>(null)
  11. const rGroup = useRef<SVGGElement>(null)
  12. const events = useZoomEvents(rCanvas)
  13. useCamera(rGroup)
  14. const handlePointerDown = useCallback((e: React.PointerEvent) => {
  15. rCanvas.current.setPointerCapture(e.pointerId)
  16. state.send("POINTED_CANVAS", getPointerEventInfo(e))
  17. }, [])
  18. const handlePointerMove = useCallback((e: React.PointerEvent) => {
  19. state.send("MOVED_POINTER", getPointerEventInfo(e))
  20. }, [])
  21. const handlePointerUp = useCallback((e: React.PointerEvent) => {
  22. rCanvas.current.releasePointerCapture(e.pointerId)
  23. state.send("STOPPED_POINTING", getPointerEventInfo(e))
  24. }, [])
  25. return (
  26. <MainSVG
  27. ref={rCanvas}
  28. {...events}
  29. onPointerDown={handlePointerDown}
  30. onPointerMove={handlePointerMove}
  31. onPointerUp={handlePointerUp}
  32. >
  33. <MainGroup ref={rGroup}>
  34. <Page />
  35. <Brush />
  36. </MainGroup>
  37. </MainSVG>
  38. )
  39. }
  40. const MainSVG = styled("svg", {
  41. position: "fixed",
  42. top: 0,
  43. left: 0,
  44. width: "100%",
  45. height: "100%",
  46. touchAction: "none",
  47. zIndex: 100,
  48. })
  49. const MainGroup = styled("g", {})