Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

circle.tsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { CircleShape, ShapeType, Corner, Edge } from "types"
  4. import shapeUtilityMap, { 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. translate(shape, delta) {
  72. shape.point = vec.add(shape.point, delta)
  73. return this
  74. },
  75. rotate(shape) {
  76. return this
  77. },
  78. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  79. shape.radius =
  80. initialShape.radius * Math.min(Math.abs(scaleX), Math.abs(scaleY))
  81. shape.point = [
  82. bounds.minX +
  83. (bounds.width - shape.radius * 2) *
  84. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  85. bounds.minY +
  86. (bounds.height - shape.radius * 2) *
  87. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  88. ]
  89. return this
  90. },
  91. transformSingle(shape, bounds, info) {
  92. shape.radius = Math.min(bounds.width, bounds.height) / 2
  93. shape.point = [bounds.minX, bounds.minY]
  94. return this
  95. },
  96. setParent(shape, parentId) {
  97. shape.parentId = parentId
  98. return this
  99. },
  100. setChildIndex(shape, childIndex) {
  101. shape.childIndex = childIndex
  102. return this
  103. },
  104. canTransform: true,
  105. canChangeAspectRatio: false,
  106. })
  107. export default circle