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.

index.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {
  2. Bounds,
  3. BoundsSnapshot,
  4. Shape,
  5. Shapes,
  6. ShapeType,
  7. Corner,
  8. Edge,
  9. } from "types"
  10. import circle from "./circle"
  11. import dot from "./dot"
  12. import polyline from "./polyline"
  13. import rectangle from "./rectangle"
  14. import ellipse from "./ellipse"
  15. import line from "./line"
  16. import ray from "./ray"
  17. /*
  18. Shape Utiliies
  19. A shape utility is an object containing utility methods for each type of shape
  20. in the application. While shapes may be very different, each shape must support
  21. a common set of utility methods, such as hit tests or translations, that
  22. Operations throughout the app will call these utility methods
  23. when performing tests (such as hit tests) or mutations, such as translations.
  24. */
  25. export interface ShapeUtility<K extends Shape> {
  26. // A cache for the computed bounds of this kind of shape.
  27. boundsCache: WeakMap<K, Bounds>
  28. // Create a new shape.
  29. create(props: Partial<K>): K
  30. // Get the bounds of the a shape.
  31. getBounds(this: ShapeUtility<K>, shape: K): Bounds
  32. // Get the routated bounds of the a shape.
  33. getRotatedBounds(this: ShapeUtility<K>, shape: K): Bounds
  34. // Get the center of the shape
  35. getCenter(this: ShapeUtility<K>, shape: K): number[]
  36. // Test whether a point lies within a shape.
  37. hitTest(this: ShapeUtility<K>, shape: K, test: number[]): boolean
  38. // Test whether bounds collide with or contain a shape.
  39. hitTestBounds(this: ShapeUtility<K>, shape: K, bounds: Bounds): boolean
  40. // Apply a rotation to a shape.
  41. rotate(this: ShapeUtility<K>, shape: K): K
  42. // Apply a translation to a shape.
  43. translate(this: ShapeUtility<K>, shape: K, delta: number[]): K
  44. // Transform to fit a new bounding box.
  45. transform(
  46. this: ShapeUtility<K>,
  47. shape: K,
  48. bounds: Bounds,
  49. info: {
  50. type: Edge | Corner
  51. initialShape: K
  52. scaleX: number
  53. scaleY: number
  54. transformOrigin: number[]
  55. }
  56. ): K
  57. transformSingle(
  58. this: ShapeUtility<K>,
  59. shape: K,
  60. bounds: Bounds,
  61. info: {
  62. type: Edge | Corner
  63. initialShape: K
  64. scaleX: number
  65. scaleY: number
  66. transformOrigin: number[]
  67. }
  68. ): K
  69. // Apply a scale to a shape.
  70. scale(this: ShapeUtility<K>, shape: K, scale: number): K
  71. // Render a shape to JSX.
  72. render(this: ShapeUtility<K>, shape: K): JSX.Element
  73. // Whether to show transform controls when this shape is selected.
  74. canTransform: boolean
  75. // Whether the shape's aspect ratio can change
  76. canChangeAspectRatio: boolean
  77. }
  78. // A mapping of shape types to shape utilities.
  79. const shapeUtilityMap: { [key in ShapeType]: ShapeUtility<Shapes[key]> } = {
  80. [ShapeType.Circle]: circle,
  81. [ShapeType.Dot]: dot,
  82. [ShapeType.Polyline]: polyline,
  83. [ShapeType.Rectangle]: rectangle,
  84. [ShapeType.Ellipse]: ellipse,
  85. [ShapeType.Line]: line,
  86. [ShapeType.Ray]: ray,
  87. }
  88. /**
  89. * A helper to retrieve a shape utility based on a shape object.
  90. * @param shape
  91. * @returns
  92. */
  93. export function getShapeUtils(shape: Shape): ShapeUtility<typeof shape> {
  94. return shapeUtilityMap[shape.type]
  95. }
  96. /**
  97. * A factory of shape utilities, with typing enforced.
  98. * @param shape
  99. * @returns
  100. */
  101. export function registerShapeUtils<T extends Shape>(
  102. shape: ShapeUtility<T>
  103. ): ShapeUtility<T> {
  104. return Object.freeze(shape)
  105. }
  106. export default shapeUtilityMap