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.9KB

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