您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ellipse.tsx 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { EllipseShape, ShapeType } from "types"
  4. import { createShape } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectEllipseBounds } from "utils/intersections"
  7. import { pointInEllipse } from "utils/hitTests"
  8. const ellipse = createShape<EllipseShape>({
  9. boundsCache: new WeakMap([]),
  10. create(props) {
  11. return {
  12. id: uuid(),
  13. type: ShapeType.Ellipse,
  14. name: "Ellipse",
  15. parentId: "page0",
  16. childIndex: 0,
  17. point: [0, 0],
  18. radiusX: 20,
  19. radiusY: 20,
  20. rotation: 0,
  21. style: {},
  22. ...props,
  23. }
  24. },
  25. render({ id, radiusX, radiusY }) {
  26. return (
  27. <ellipse id={id} cx={radiusX} cy={radiusY} rx={radiusX} ry={radiusY} />
  28. )
  29. },
  30. getBounds(shape) {
  31. if (this.boundsCache.has(shape)) {
  32. return this.boundsCache.get(shape)
  33. }
  34. const {
  35. point: [x, y],
  36. radiusX,
  37. radiusY,
  38. } = shape
  39. const bounds = {
  40. minX: x,
  41. maxX: x + radiusX * 2,
  42. minY: y,
  43. maxY: y + radiusY * 2,
  44. width: radiusX * 2,
  45. height: radiusY * 2,
  46. }
  47. this.boundsCache.set(shape, bounds)
  48. return bounds
  49. },
  50. hitTest(shape, point) {
  51. return pointInEllipse(point, shape.point, shape.radiusX, shape.radiusY)
  52. },
  53. hitTestBounds(this, shape, brushBounds) {
  54. const shapeBounds = this.getBounds(shape)
  55. return (
  56. boundsContained(shapeBounds, brushBounds) ||
  57. intersectEllipseBounds(
  58. vec.add(shape.point, [shape.radiusX, shape.radiusY]),
  59. shape.radiusX,
  60. shape.radiusY,
  61. brushBounds
  62. ).length > 0
  63. )
  64. },
  65. rotate(shape) {
  66. return shape
  67. },
  68. translate(shape, delta) {
  69. shape.point = vec.add(shape.point, delta)
  70. return shape
  71. },
  72. scale(shape, scale: number) {
  73. return shape
  74. },
  75. stretch(shape, scaleX: number, scaleY: number) {
  76. return shape
  77. },
  78. transform(shape, bounds) {
  79. shape.point = [bounds.minX, bounds.minY]
  80. shape.radiusX = bounds.width / 2
  81. shape.radiusY = bounds.height / 2
  82. return shape
  83. },
  84. })
  85. export default ellipse