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.

dot.tsx 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { DotShape, ShapeType } from "types"
  4. import { registerShapeUtils } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. import { DotCircle } from "components/canvas/misc"
  8. import { translateBounds } from "utils/utils"
  9. const dot = registerShapeUtils<DotShape>({
  10. boundsCache: new WeakMap([]),
  11. create(props) {
  12. return {
  13. id: uuid(),
  14. type: ShapeType.Dot,
  15. isGenerated: false,
  16. name: "Dot",
  17. parentId: "page0",
  18. childIndex: 0,
  19. point: [0, 0],
  20. rotation: 0,
  21. style: {
  22. fill: "#c6cacb",
  23. strokeWidth: "0",
  24. },
  25. ...props,
  26. }
  27. },
  28. render({ id }) {
  29. return <DotCircle id={id} cx={0} cy={0} r={4} />
  30. },
  31. getBounds(shape) {
  32. if (!this.boundsCache.has(shape)) {
  33. const bounds = {
  34. minX: 0,
  35. maxX: 1,
  36. minY: 0,
  37. maxY: 1,
  38. width: 1,
  39. height: 1,
  40. }
  41. this.boundsCache.set(shape, bounds)
  42. }
  43. return translateBounds(this.boundsCache.get(shape), shape.point)
  44. },
  45. getRotatedBounds(shape) {
  46. return this.getBounds(shape)
  47. },
  48. getCenter(shape) {
  49. return shape.point
  50. },
  51. hitTest(shape, test) {
  52. return true
  53. },
  54. hitTestBounds(this, shape, brushBounds) {
  55. const shapeBounds = this.getBounds(shape)
  56. return (
  57. boundsContained(shapeBounds, brushBounds) ||
  58. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  59. )
  60. },
  61. rotate(shape) {
  62. return this
  63. },
  64. translate(shape, delta) {
  65. shape.point = vec.add(shape.point, delta)
  66. return this
  67. },
  68. transform(shape, bounds) {
  69. shape.point = [bounds.minX, bounds.minY]
  70. return this
  71. },
  72. transformSingle(shape, bounds, info) {
  73. this.transform(shape, bounds, info)
  74. return this
  75. },
  76. setParent(shape, parentId) {
  77. shape.parentId = parentId
  78. return this
  79. },
  80. setChildIndex(shape, childIndex) {
  81. shape.childIndex = childIndex
  82. return this
  83. },
  84. canTransform: false,
  85. canChangeAspectRatio: false,
  86. })
  87. export default dot