選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bounds-bg.tsx 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { useCallback, useRef } from 'react'
  2. import state, { useSelector } from 'state'
  3. import inputs from 'state/inputs'
  4. import styled from 'styles'
  5. import { getPage } from 'utils/utils'
  6. function handlePointerDown(e: React.PointerEvent<SVGRectElement>) {
  7. if (e.buttons !== 1) return
  8. if (!inputs.canAccept(e.pointerId)) return
  9. e.stopPropagation()
  10. e.currentTarget.setPointerCapture(e.pointerId)
  11. state.send('POINTED_BOUNDS', inputs.pointerDown(e, 'bounds'))
  12. }
  13. function handlePointerUp(e: React.PointerEvent<SVGRectElement>) {
  14. if (e.buttons !== 1) return
  15. if (!inputs.canAccept(e.pointerId)) return
  16. e.stopPropagation()
  17. e.currentTarget.releasePointerCapture(e.pointerId)
  18. state.send('STOPPED_POINTING', inputs.pointerUp(e))
  19. }
  20. export default function BoundsBg() {
  21. const rBounds = useRef<SVGRectElement>(null)
  22. const bounds = useSelector((state) => state.values.selectedBounds)
  23. const isSelecting = useSelector((s) => s.isIn('selecting'))
  24. const rotation = useSelector((s) => {
  25. if (s.data.selectedIds.size === 1) {
  26. const { shapes } = getPage(s.data)
  27. const selected = Array.from(s.data.selectedIds.values())[0]
  28. return shapes[selected].rotation
  29. } else {
  30. return 0
  31. }
  32. })
  33. if (!bounds) return null
  34. if (!isSelecting) return null
  35. const { width, height } = bounds
  36. return (
  37. <StyledBoundsBg
  38. ref={rBounds}
  39. width={Math.max(1, width)}
  40. height={Math.max(1, height)}
  41. transform={`
  42. rotate(${rotation * (180 / Math.PI)},
  43. ${(bounds.minX + bounds.maxX) / 2},
  44. ${(bounds.minY + bounds.maxY) / 2})
  45. translate(${bounds.minX},${bounds.minY})`}
  46. onPointerDown={handlePointerDown}
  47. onPointerUp={handlePointerUp}
  48. />
  49. )
  50. }
  51. const StyledBoundsBg = styled('rect', {
  52. fill: '$boundsBg',
  53. })