Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ray.tsx 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. name: "Ray",
  14. parentId: "page0",
  15. childIndex: 0,
  16. point: [0, 0],
  17. vector: [0, 0],
  18. rotation: 0,
  19. style: {},
  20. ...props,
  21. }
  22. },
  23. render({ id }) {
  24. return <circle id={id} cx={4} cy={4} r={4} />
  25. },
  26. getBounds(shape) {
  27. if (this.boundsCache.has(shape)) {
  28. return this.boundsCache.get(shape)
  29. }
  30. const {
  31. point: [x, y],
  32. } = shape
  33. const bounds = {
  34. minX: x,
  35. maxX: x + 8,
  36. minY: y,
  37. maxY: y + 8,
  38. width: 8,
  39. height: 8,
  40. }
  41. this.boundsCache.set(shape, bounds)
  42. return bounds
  43. },
  44. hitTest(shape, test) {
  45. return vec.dist(shape.point, test) < 4
  46. },
  47. hitTestBounds(this, shape, brushBounds) {
  48. const shapeBounds = this.getBounds(shape)
  49. return (
  50. boundsContained(shapeBounds, brushBounds) ||
  51. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  52. )
  53. },
  54. rotate(shape) {
  55. return shape
  56. },
  57. translate(shape, delta) {
  58. shape.point = vec.add(shape.point, delta)
  59. return shape
  60. },
  61. scale(shape, scale: number) {
  62. return shape
  63. },
  64. stretch(shape, scaleX: number, scaleY: number) {
  65. return shape
  66. },
  67. transform(shape, bounds) {
  68. return shape
  69. },
  70. })
  71. export default ray