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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import { CircleShape, ColorStyle, DashStyle, ShapeType, SizeStyle } 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 { translateBounds } from 'utils/utils'
  9. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  10. const circle = registerShapeUtils<CircleShape>({
  11. boundsCache: new WeakMap([]),
  12. create(props) {
  13. return {
  14. id: uuid(),
  15. type: ShapeType.Circle,
  16. isGenerated: false,
  17. name: 'Circle',
  18. parentId: 'page0',
  19. childIndex: 0,
  20. point: [0, 0],
  21. rotation: 0,
  22. radius: 1,
  23. isAspectRatioLocked: false,
  24. isLocked: false,
  25. isHidden: false,
  26. style: defaultStyle,
  27. ...props,
  28. }
  29. },
  30. render({ id, radius, style }) {
  31. const styles = getShapeStyle(style)
  32. return (
  33. <circle
  34. id={id}
  35. cx={radius}
  36. cy={radius}
  37. r={Math.max(0, radius - Number(styles.strokeWidth) / 2)}
  38. />
  39. )
  40. },
  41. applyStyles(shape, style) {
  42. Object.assign(shape.style, style)
  43. return this
  44. },
  45. getBounds(shape) {
  46. if (!this.boundsCache.has(shape)) {
  47. const { radius } = shape
  48. const bounds = {
  49. minX: 0,
  50. maxX: radius * 2,
  51. minY: 0,
  52. maxY: radius * 2,
  53. width: radius * 2,
  54. height: radius * 2,
  55. }
  56. this.boundsCache.set(shape, bounds)
  57. }
  58. return translateBounds(this.boundsCache.get(shape), shape.point)
  59. },
  60. getRotatedBounds(shape) {
  61. return this.getBounds(shape)
  62. },
  63. getCenter(shape) {
  64. return [shape.point[0] + shape.radius, shape.point[1] + shape.radius]
  65. },
  66. hitTest(shape, point) {
  67. return pointInCircle(
  68. point,
  69. vec.addScalar(shape.point, shape.radius),
  70. shape.radius
  71. )
  72. },
  73. hitTestBounds(shape, bounds) {
  74. const shapeBounds = this.getBounds(shape)
  75. return (
  76. boundsContained(shapeBounds, bounds) ||
  77. intersectCircleBounds(
  78. vec.addScalar(shape.point, shape.radius),
  79. shape.radius,
  80. bounds
  81. ).length > 0
  82. )
  83. },
  84. rotateTo(shape, rotation) {
  85. shape.rotation = rotation
  86. return this
  87. },
  88. translateTo(shape, point) {
  89. shape.point = vec.toPrecision(point)
  90. return this
  91. },
  92. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  93. shape.radius =
  94. initialShape.radius * Math.min(Math.abs(scaleX), Math.abs(scaleY))
  95. shape.point = [
  96. bounds.minX +
  97. (bounds.width - shape.radius * 2) *
  98. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  99. bounds.minY +
  100. (bounds.height - shape.radius * 2) *
  101. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  102. ]
  103. return this
  104. },
  105. transformSingle(shape, bounds, info) {
  106. shape.radius = Math.min(bounds.width, bounds.height) / 2
  107. shape.point = [bounds.minX, bounds.minY]
  108. return this
  109. },
  110. setProperty(shape, prop, value) {
  111. shape[prop] = value
  112. return this
  113. },
  114. canTransform: true,
  115. canChangeAspectRatio: false,
  116. canStyleFill: true,
  117. })
  118. export default circle