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 3.1KB

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