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 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import styled from 'styles'
  2. import state, { useSelector } from 'state'
  3. import inputs from 'state/inputs'
  4. import React, { useCallback, useRef } from 'react'
  5. import useZoomEvents from 'hooks/useZoomEvents'
  6. import useCamera from 'hooks/useCamera'
  7. import Defs from './defs'
  8. import Page from './page'
  9. import Brush from './brush'
  10. import Bounds from './bounds/bounding-box'
  11. import BoundsBg from './bounds/bounds-bg'
  12. import Selected from './selected'
  13. import Handles from './bounds/handles'
  14. import { isMobile, throttle } from 'utils/utils'
  15. export default function Canvas() {
  16. const rCanvas = useRef<SVGSVGElement>(null)
  17. const rGroup = useRef<SVGGElement>(null)
  18. useCamera(rGroup)
  19. useZoomEvents()
  20. const isReady = useSelector((s) => s.isIn('ready'))
  21. const handlePointerDown = useCallback((e: React.PointerEvent) => {
  22. if (!inputs.canAccept(e.pointerId)) return
  23. rCanvas.current.setPointerCapture(e.pointerId)
  24. state.send('POINTED_CANVAS', inputs.pointerDown(e, 'canvas'))
  25. }, [])
  26. const handleTouchStart = useCallback((e: React.TouchEvent) => {
  27. if (e.touches.length === 2) {
  28. state.send('TOUCH_UNDO')
  29. } else {
  30. if (isMobile()) {
  31. state.send('TOUCHED_CANVAS')
  32. }
  33. }
  34. }, [])
  35. const handlePointerMove = useCallback((e: React.PointerEvent) => {
  36. if (!inputs.canAccept(e.pointerId)) return
  37. throttledPointerMove(inputs.pointerMove(e))
  38. }, [])
  39. const handlePointerUp = useCallback((e: React.PointerEvent) => {
  40. if (!inputs.canAccept(e.pointerId)) return
  41. rCanvas.current.releasePointerCapture(e.pointerId)
  42. state.send('STOPPED_POINTING', { id: 'canvas', ...inputs.pointerUp(e) })
  43. }, [])
  44. return (
  45. <MainSVG
  46. ref={rCanvas}
  47. onPointerDown={handlePointerDown}
  48. onPointerMove={handlePointerMove}
  49. onPointerUp={handlePointerUp}
  50. onTouchStart={handleTouchStart}
  51. // onTouchMove={handleTouchMove}
  52. >
  53. <Defs />
  54. {isReady && (
  55. <g ref={rGroup}>
  56. <BoundsBg />
  57. <Page />
  58. {/* <Selected /> */}
  59. <Bounds />
  60. <Handles />
  61. <Brush />
  62. </g>
  63. )}
  64. </MainSVG>
  65. )
  66. }
  67. const MainSVG = styled('svg', {
  68. position: 'fixed',
  69. top: 0,
  70. left: 0,
  71. width: '100%',
  72. height: '100%',
  73. touchAction: 'none',
  74. zIndex: 100,
  75. backgroundColor: '$canvas',
  76. pointerEvents: 'all',
  77. '& *': {
  78. userSelect: 'none',
  79. },
  80. })
  81. // const throttledPointerMove = throttle((payload: any) => {
  82. // state.send('MOVED_POINTER', payload)
  83. // }, 16)
  84. const throttledPointerMove = (payload: any) => {
  85. state.send('MOVED_POINTER', payload)
  86. }