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.

bounds-bg.tsx 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { useRef } from "react"
  2. import state, { useSelector } from "state"
  3. import inputs from "state/inputs"
  4. import styled from "styles"
  5. export default function BoundsBg() {
  6. const rBounds = useRef<SVGRectElement>(null)
  7. const bounds = useSelector((state) => state.values.selectedBounds)
  8. const rotation = useSelector((s) => {
  9. if (s.data.selectedIds.size === 1) {
  10. const { shapes } = s.data.document.pages[s.data.currentPageId]
  11. const selected = Array.from(s.data.selectedIds.values())[0]
  12. return shapes[selected].rotation
  13. } else {
  14. return 0
  15. }
  16. })
  17. if (!bounds) return null
  18. const { minX, minY, width, height } = bounds
  19. return (
  20. <StyledBoundsBg
  21. ref={rBounds}
  22. x={minX}
  23. y={minY}
  24. width={Math.max(1, width)}
  25. height={Math.max(1, height)}
  26. onPointerDown={(e) => {
  27. if (e.buttons !== 1) return
  28. e.stopPropagation()
  29. rBounds.current.setPointerCapture(e.pointerId)
  30. state.send("POINTED_BOUNDS", inputs.pointerDown(e, "bounds"))
  31. }}
  32. transform={`rotate(${rotation * (180 / Math.PI)},${minX + width / 2}, ${
  33. minY + height / 2
  34. })`}
  35. />
  36. )
  37. }
  38. const StyledBoundsBg = styled("rect", {
  39. fill: "$boundsBg",
  40. })