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.

ray.tsx 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { RayShape, ShapeType } from "types"
  4. import { createShape } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. const ray = createShape<RayShape>({
  8. boundsCache: new WeakMap([]),
  9. create(props) {
  10. return {
  11. id: uuid(),
  12. type: ShapeType.Ray,
  13. isGenerated: false,
  14. name: "Ray",
  15. parentId: "page0",
  16. childIndex: 0,
  17. point: [0, 0],
  18. direction: [0, 0],
  19. rotation: 0,
  20. style: {},
  21. ...props,
  22. }
  23. },
  24. render({ id, direction }) {
  25. const [x2, y2] = vec.add([0, 0], vec.mul(direction, 100000))
  26. return (
  27. <g id={id}>
  28. <line x1={0} y1={0} x2={x2} y2={y2} />
  29. <circle cx={0} cy={0} r={4} />
  30. </g>
  31. )
  32. },
  33. getBounds(shape) {
  34. if (this.boundsCache.has(shape)) {
  35. return this.boundsCache.get(shape)
  36. }
  37. const {
  38. point: [x, y],
  39. } = shape
  40. const bounds = {
  41. minX: x,
  42. maxX: x + 8,
  43. minY: y,
  44. maxY: y + 8,
  45. width: 8,
  46. height: 8,
  47. }
  48. this.boundsCache.set(shape, bounds)
  49. return bounds
  50. },
  51. hitTest(shape, test) {
  52. return true
  53. },
  54. hitTestBounds(this, shape, brushBounds) {
  55. const shapeBounds = this.getBounds(shape)
  56. return (
  57. boundsContained(shapeBounds, brushBounds) ||
  58. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  59. )
  60. },
  61. rotate(shape) {
  62. return shape
  63. },
  64. translate(shape, delta) {
  65. shape.point = vec.add(shape.point, delta)
  66. return shape
  67. },
  68. scale(shape, scale: number) {
  69. return shape
  70. },
  71. stretch(shape, scaleX: number, scaleY: number) {
  72. return shape
  73. },
  74. transform(shape, bounds) {
  75. return shape
  76. },
  77. canTransform: false,
  78. })
  79. export default ray