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

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