Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

circle.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { CircleShape, ShapeType, TransformCorner, TransformEdge } from "types"
  4. import { createShape } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. import { pointInCircle } from "utils/hitTests"
  8. import { translateBounds } from "utils/utils"
  9. const circle = createShape<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, { anchor }) {
  82. // Set the new corner or position depending on the anchor
  83. switch (anchor) {
  84. case TransformCorner.TopLeft: {
  85. shape.radius = Math.min(bounds.width, bounds.height) / 2
  86. shape.point = [
  87. bounds.maxX - shape.radius * 2,
  88. bounds.maxY - shape.radius * 2,
  89. ]
  90. break
  91. }
  92. case TransformCorner.TopRight: {
  93. shape.radius = Math.min(bounds.width, bounds.height) / 2
  94. shape.point = [bounds.minX, bounds.maxY - shape.radius * 2]
  95. break
  96. }
  97. case TransformCorner.BottomRight: {
  98. shape.radius = Math.min(bounds.width, bounds.height) / 2
  99. shape.point = [bounds.minX, bounds.minY]
  100. break
  101. }
  102. case TransformCorner.BottomLeft: {
  103. shape.radius = Math.min(bounds.width, bounds.height) / 2
  104. shape.point = [bounds.maxX - shape.radius * 2, bounds.minY]
  105. break
  106. }
  107. case TransformEdge.Top: {
  108. shape.radius = bounds.height / 2
  109. shape.point = [
  110. bounds.minX + (bounds.width / 2 - shape.radius),
  111. bounds.minY,
  112. ]
  113. break
  114. }
  115. case TransformEdge.Right: {
  116. shape.radius = bounds.width / 2
  117. shape.point = [
  118. bounds.maxX - shape.radius * 2,
  119. bounds.minY + (bounds.height / 2 - shape.radius),
  120. ]
  121. break
  122. }
  123. case TransformEdge.Bottom: {
  124. shape.radius = bounds.height / 2
  125. shape.point = [
  126. bounds.minX + (bounds.width / 2 - shape.radius),
  127. bounds.maxY - shape.radius * 2,
  128. ]
  129. break
  130. }
  131. case TransformEdge.Left: {
  132. shape.radius = bounds.width / 2
  133. shape.point = [
  134. bounds.minX,
  135. bounds.minY + (bounds.height / 2 - shape.radius),
  136. ]
  137. break
  138. }
  139. }
  140. return shape
  141. },
  142. transformSingle(shape, bounds, info) {
  143. return this.transform(shape, bounds, info)
  144. },
  145. canTransform: true,
  146. })
  147. export default circle