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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 } 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: 20,
  26. radiusY: 20,
  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. getBounds(shape) {
  41. if (!this.boundsCache.has(shape)) {
  42. const { radiusX, radiusY } = shape
  43. const bounds = {
  44. minX: 0,
  45. maxX: radiusX * 2,
  46. minY: 0,
  47. maxY: radiusY * 2,
  48. width: radiusX * 2,
  49. height: radiusY * 2,
  50. }
  51. this.boundsCache.set(shape, bounds)
  52. }
  53. return translateBounds(this.boundsCache.get(shape), shape.point)
  54. },
  55. getRotatedBounds(shape) {
  56. return getBoundsFromPoints(getRotatedCorners(shape))
  57. },
  58. getCenter(shape) {
  59. return [shape.point[0] + shape.radiusX, shape.point[1] + shape.radiusY]
  60. },
  61. hitTest(shape, point) {
  62. return pointInEllipse(
  63. point,
  64. vec.add(shape.point, [shape.radiusX, shape.radiusY]),
  65. shape.radiusX,
  66. shape.radiusY,
  67. shape.rotation
  68. )
  69. },
  70. hitTestBounds(this, shape, brushBounds) {
  71. // TODO: Account for rotation
  72. const shapeBounds = this.getBounds(shape)
  73. return (
  74. boundsContained(shapeBounds, brushBounds) ||
  75. intersectEllipseBounds(
  76. vec.add(shape.point, [shape.radiusX, shape.radiusY]),
  77. shape.radiusX,
  78. shape.radiusY,
  79. brushBounds,
  80. shape.rotation
  81. ).length > 0
  82. )
  83. },
  84. rotate(shape) {
  85. return shape
  86. },
  87. translate(shape, delta) {
  88. shape.point = vec.add(shape.point, delta)
  89. return shape
  90. },
  91. scale(shape, scale: number) {
  92. return shape
  93. },
  94. transform(shape, bounds) {
  95. shape.point = [bounds.minX, bounds.minY]
  96. shape.radiusX = bounds.width / 2
  97. shape.radiusY = bounds.height / 2
  98. return shape
  99. },
  100. transformSingle(shape, bounds, info) {
  101. return this.transform(shape, bounds, info)
  102. },
  103. canTransform: true,
  104. })
  105. export default ellipse