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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import { EllipseShape, ShapeType } from 'types'
  4. import { registerShapeUtils } from './index'
  5. import { boundsContained, getRotatedEllipseBounds } from 'utils/bounds'
  6. import { intersectEllipseBounds } from 'utils/intersections'
  7. import { pointInEllipse } from 'utils/hitTests'
  8. import {
  9. getBoundsFromPoints,
  10. getRotatedCorners,
  11. rotateBounds,
  12. translateBounds,
  13. } from 'utils/utils'
  14. const ellipse = registerShapeUtils<EllipseShape>({
  15. boundsCache: new WeakMap([]),
  16. create(props) {
  17. return {
  18. id: uuid(),
  19. type: ShapeType.Ellipse,
  20. isGenerated: false,
  21. name: 'Ellipse',
  22. parentId: 'page0',
  23. childIndex: 0,
  24. point: [0, 0],
  25. radiusX: 1,
  26. radiusY: 1,
  27. rotation: 0,
  28. style: {
  29. fill: '#c6cacb',
  30. stroke: '#000',
  31. },
  32. ...props,
  33. }
  34. },
  35. render({ id, radiusX, radiusY, style }) {
  36. return (
  37. <ellipse
  38. id={id}
  39. cx={radiusX}
  40. cy={radiusY}
  41. rx={Math.max(0, radiusX - Number(style.strokeWidth) / 2)}
  42. ry={Math.max(0, radiusY - Number(style.strokeWidth) / 2)}
  43. />
  44. )
  45. },
  46. applyStyles(shape, style) {
  47. Object.assign(shape.style, style)
  48. return this
  49. },
  50. getBounds(shape) {
  51. if (!this.boundsCache.has(shape)) {
  52. const { radiusX, radiusY } = shape
  53. const bounds = {
  54. minX: 0,
  55. minY: 0,
  56. maxX: radiusX * 2,
  57. maxY: radiusY * 2,
  58. width: radiusX * 2,
  59. height: radiusY * 2,
  60. }
  61. this.boundsCache.set(shape, bounds)
  62. }
  63. return translateBounds(this.boundsCache.get(shape), shape.point)
  64. },
  65. getRotatedBounds(shape) {
  66. return getRotatedEllipseBounds(
  67. shape.point[0],
  68. shape.point[1],
  69. shape.radiusX,
  70. shape.radiusY,
  71. shape.rotation
  72. )
  73. },
  74. getCenter(shape) {
  75. return [shape.point[0] + shape.radiusX, shape.point[1] + shape.radiusY]
  76. },
  77. hitTest(shape, point) {
  78. return pointInEllipse(
  79. point,
  80. vec.add(shape.point, [shape.radiusX, shape.radiusY]),
  81. shape.radiusX,
  82. shape.radiusY,
  83. shape.rotation
  84. )
  85. },
  86. hitTestBounds(this, shape, brushBounds) {
  87. const shapeBounds = this.getBounds(shape)
  88. return (
  89. boundsContained(shapeBounds, brushBounds) ||
  90. intersectEllipseBounds(
  91. vec.add(shape.point, [shape.radiusX, shape.radiusY]),
  92. shape.radiusX,
  93. shape.radiusY,
  94. brushBounds,
  95. shape.rotation
  96. ).length > 0
  97. )
  98. },
  99. rotateTo(shape, rotation) {
  100. shape.rotation = rotation
  101. return this
  102. },
  103. translateTo(shape, point) {
  104. shape.point = vec.toPrecision(point)
  105. return this
  106. },
  107. transform(shape, bounds, { scaleX, scaleY, initialShape }) {
  108. shape.point = [bounds.minX, bounds.minY]
  109. shape.radiusX = bounds.width / 2
  110. shape.radiusY = bounds.height / 2
  111. shape.rotation =
  112. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  113. ? -initialShape.rotation
  114. : initialShape.rotation
  115. return this
  116. },
  117. transformSingle(shape, bounds, info) {
  118. return this.transform(shape, bounds, info)
  119. },
  120. setParent(shape, parentId) {
  121. shape.parentId = parentId
  122. return this
  123. },
  124. setChildIndex(shape, childIndex) {
  125. shape.childIndex = childIndex
  126. return this
  127. },
  128. canTransform: true,
  129. canChangeAspectRatio: true,
  130. })
  131. export default ellipse