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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 { translateBounds } from 'utils/utils'
  8. import { defaultStyle } from 'lib/shape-styles'
  9. const dot = registerShapeUtils<DotShape>({
  10. boundsCache: new WeakMap([]),
  11. create(props) {
  12. return {
  13. id: uuid(),
  14. seed: Math.random(),
  15. type: ShapeType.Dot,
  16. isGenerated: false,
  17. name: 'Dot',
  18. parentId: 'page1',
  19. childIndex: 0,
  20. point: [0, 0],
  21. rotation: 0,
  22. isAspectRatioLocked: false,
  23. isLocked: false,
  24. isHidden: false,
  25. ...props,
  26. style: {
  27. ...defaultStyle,
  28. ...props.style,
  29. isFilled: false,
  30. },
  31. }
  32. },
  33. render({ id }) {
  34. return <use id={id} href="#dot" fill="black" />
  35. },
  36. getBounds(shape) {
  37. if (!this.boundsCache.has(shape)) {
  38. const bounds = {
  39. minX: 0,
  40. maxX: 1,
  41. minY: 0,
  42. maxY: 1,
  43. width: 1,
  44. height: 1,
  45. }
  46. this.boundsCache.set(shape, bounds)
  47. }
  48. return translateBounds(this.boundsCache.get(shape), shape.point)
  49. },
  50. getRotatedBounds(shape) {
  51. return this.getBounds(shape)
  52. },
  53. getCenter(shape) {
  54. return shape.point
  55. },
  56. hitTest(shape, test) {
  57. return true
  58. },
  59. hitTestBounds(this, shape, brushBounds) {
  60. const shapeBounds = this.getBounds(shape)
  61. return (
  62. boundsContained(shapeBounds, brushBounds) ||
  63. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  64. )
  65. },
  66. transform(shape, bounds) {
  67. shape.point = [bounds.minX, bounds.minY]
  68. return this
  69. },
  70. canTransform: false,
  71. canChangeAspectRatio: false,
  72. })
  73. export default dot