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.

ellipse.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 }) {
  36. return (
  37. <ellipse id={id} cx={radiusX} cy={radiusY} rx={radiusX} ry={radiusY} />
  38. )
  39. },
  40. applyStyles(shape, style) {
  41. Object.assign(shape.style, style)
  42. return this
  43. },
  44. getBounds(shape) {
  45. if (!this.boundsCache.has(shape)) {
  46. const { radiusX, radiusY } = shape
  47. const bounds = {
  48. minX: 0,
  49. minY: 0,
  50. maxX: radiusX * 2,
  51. maxY: radiusY * 2,
  52. width: radiusX * 2,
  53. height: radiusY * 2,
  54. }
  55. this.boundsCache.set(shape, bounds)
  56. }
  57. return translateBounds(this.boundsCache.get(shape), shape.point)
  58. },
  59. getRotatedBounds(shape) {
  60. return getRotatedEllipseBounds(
  61. shape.point[0],
  62. shape.point[1],
  63. shape.radiusX,
  64. shape.radiusY,
  65. shape.rotation
  66. )
  67. },
  68. getCenter(shape) {
  69. return [shape.point[0] + shape.radiusX, shape.point[1] + shape.radiusY]
  70. },
  71. hitTest(shape, point) {
  72. return pointInEllipse(
  73. point,
  74. vec.add(shape.point, [shape.radiusX, shape.radiusY]),
  75. shape.radiusX,
  76. shape.radiusY,
  77. shape.rotation
  78. )
  79. },
  80. hitTestBounds(this, shape, brushBounds) {
  81. const shapeBounds = this.getBounds(shape)
  82. return (
  83. boundsContained(shapeBounds, brushBounds) ||
  84. intersectEllipseBounds(
  85. vec.add(shape.point, [shape.radiusX, shape.radiusY]),
  86. shape.radiusX,
  87. shape.radiusY,
  88. brushBounds,
  89. shape.rotation
  90. ).length > 0
  91. )
  92. },
  93. rotateTo(shape, rotation) {
  94. shape.rotation = rotation
  95. return this
  96. },
  97. translateTo(shape, point) {
  98. shape.point = point
  99. return this
  100. },
  101. transform(shape, bounds, { scaleX, scaleY, initialShape }) {
  102. shape.point = [bounds.minX, bounds.minY]
  103. shape.radiusX = bounds.width / 2
  104. shape.radiusY = bounds.height / 2
  105. shape.rotation =
  106. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  107. ? -initialShape.rotation
  108. : initialShape.rotation
  109. return this
  110. },
  111. transformSingle(shape, bounds, info) {
  112. return this.transform(shape, bounds, info)
  113. },
  114. setParent(shape, parentId) {
  115. shape.parentId = parentId
  116. return this
  117. },
  118. setChildIndex(shape, childIndex) {
  119. shape.childIndex = childIndex
  120. return this
  121. },
  122. canTransform: true,
  123. canChangeAspectRatio: true,
  124. })
  125. export default ellipse