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.

circle.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { CircleShape, ShapeType, Corner, Edge } from "types"
  4. import { registerShapeUtils } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. import { pointInCircle } from "utils/hitTests"
  8. import { getTransformAnchor, translateBounds } from "utils/utils"
  9. const circle = registerShapeUtils<CircleShape>({
  10. boundsCache: new WeakMap([]),
  11. create(props) {
  12. return {
  13. id: uuid(),
  14. type: ShapeType.Circle,
  15. isGenerated: false,
  16. name: "Circle",
  17. parentId: "page0",
  18. childIndex: 0,
  19. point: [0, 0],
  20. rotation: 0,
  21. radius: 20,
  22. style: {
  23. fill: "#c6cacb",
  24. stroke: "#000",
  25. },
  26. ...props,
  27. }
  28. },
  29. render({ id, radius }) {
  30. return <circle id={id} cx={radius} cy={radius} r={radius} />
  31. },
  32. getBounds(shape) {
  33. if (!this.boundsCache.has(shape)) {
  34. const { radius } = shape
  35. const bounds = {
  36. minX: 0,
  37. maxX: radius * 2,
  38. minY: 0,
  39. maxY: radius * 2,
  40. width: radius * 2,
  41. height: radius * 2,
  42. }
  43. this.boundsCache.set(shape, bounds)
  44. }
  45. return translateBounds(this.boundsCache.get(shape), shape.point)
  46. },
  47. getRotatedBounds(shape) {
  48. return this.getBounds(shape)
  49. },
  50. getCenter(shape) {
  51. return [shape.point[0] + shape.radius, shape.point[1] + shape.radius]
  52. },
  53. hitTest(shape, point) {
  54. return pointInCircle(
  55. point,
  56. vec.addScalar(shape.point, shape.radius),
  57. shape.radius
  58. )
  59. },
  60. hitTestBounds(shape, bounds) {
  61. const shapeBounds = this.getBounds(shape)
  62. return (
  63. boundsContained(shapeBounds, bounds) ||
  64. intersectCircleBounds(
  65. vec.addScalar(shape.point, shape.radius),
  66. shape.radius,
  67. bounds
  68. ).length > 0
  69. )
  70. },
  71. rotate(shape) {
  72. return shape
  73. },
  74. translate(shape, delta) {
  75. shape.point = vec.add(shape.point, delta)
  76. return shape
  77. },
  78. scale(shape, scale) {
  79. return shape
  80. },
  81. transform(shape, bounds, { type, initialShape, scaleX, scaleY }) {
  82. const anchor = getTransformAnchor(type, scaleX < 0, scaleY < 0)
  83. // Set the new corner or position depending on the anchor
  84. switch (anchor) {
  85. case Corner.TopLeft: {
  86. shape.radius = Math.min(bounds.width, bounds.height) / 2
  87. shape.point = [
  88. bounds.maxX - shape.radius * 2,
  89. bounds.maxY - shape.radius * 2,
  90. ]
  91. break
  92. }
  93. case Corner.TopRight: {
  94. shape.radius = Math.min(bounds.width, bounds.height) / 2
  95. shape.point = [bounds.minX, bounds.maxY - shape.radius * 2]
  96. break
  97. }
  98. case Corner.BottomRight: {
  99. shape.radius = Math.min(bounds.width, bounds.height) / 2
  100. shape.point = [
  101. bounds.maxX - shape.radius * 2,
  102. bounds.maxY - shape.radius * 2,
  103. ]
  104. break
  105. break
  106. }
  107. case Corner.BottomLeft: {
  108. shape.radius = Math.min(bounds.width, bounds.height) / 2
  109. shape.point = [bounds.maxX - shape.radius * 2, bounds.minY]
  110. break
  111. }
  112. case Edge.Top: {
  113. shape.radius = bounds.height / 2
  114. shape.point = [
  115. bounds.minX + (bounds.width / 2 - shape.radius),
  116. bounds.minY,
  117. ]
  118. break
  119. }
  120. case Edge.Right: {
  121. shape.radius = bounds.width / 2
  122. shape.point = [
  123. bounds.maxX - shape.radius * 2,
  124. bounds.minY + (bounds.height / 2 - shape.radius),
  125. ]
  126. break
  127. }
  128. case Edge.Bottom: {
  129. shape.radius = bounds.height / 2
  130. shape.point = [
  131. bounds.minX + (bounds.width / 2 - shape.radius),
  132. bounds.maxY - shape.radius * 2,
  133. ]
  134. break
  135. }
  136. case Edge.Left: {
  137. shape.radius = bounds.width / 2
  138. shape.point = [
  139. bounds.minX,
  140. bounds.minY + (bounds.height / 2 - shape.radius),
  141. ]
  142. break
  143. }
  144. }
  145. return shape
  146. },
  147. transformSingle(shape, bounds, info) {
  148. return this.transform(shape, bounds, info)
  149. },
  150. canTransform: true,
  151. })
  152. export default circle