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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. isAspectRatioLocked: false,
  22. isLocked: false,
  23. isHidden: false,
  24. style: {
  25. fill: '#c6cacb',
  26. strokeWidth: '0',
  27. },
  28. ...props,
  29. }
  30. },
  31. render({ id }) {
  32. return <DotCircle id={id} cx={0} cy={0} r={3} />
  33. },
  34. applyStyles(shape, style) {
  35. Object.assign(shape.style, style)
  36. return this
  37. },
  38. getBounds(shape) {
  39. if (!this.boundsCache.has(shape)) {
  40. const bounds = {
  41. minX: 0,
  42. maxX: 1,
  43. minY: 0,
  44. maxY: 1,
  45. width: 1,
  46. height: 1,
  47. }
  48. this.boundsCache.set(shape, bounds)
  49. }
  50. return translateBounds(this.boundsCache.get(shape), shape.point)
  51. },
  52. getRotatedBounds(shape) {
  53. return this.getBounds(shape)
  54. },
  55. getCenter(shape) {
  56. return shape.point
  57. },
  58. hitTest(shape, test) {
  59. return true
  60. },
  61. hitTestBounds(this, shape, brushBounds) {
  62. const shapeBounds = this.getBounds(shape)
  63. return (
  64. boundsContained(shapeBounds, brushBounds) ||
  65. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  66. )
  67. },
  68. rotateTo(shape) {
  69. return this
  70. },
  71. translateTo(shape, point) {
  72. shape.point = vec.toPrecision(point)
  73. return this
  74. },
  75. transform(shape, bounds) {
  76. shape.point = [bounds.minX, bounds.minY]
  77. return this
  78. },
  79. transformSingle(shape, bounds, info) {
  80. this.transform(shape, bounds, info)
  81. return this
  82. },
  83. setProperty(shape, prop, value) {
  84. shape[prop] = value
  85. return this
  86. },
  87. canTransform: false,
  88. canChangeAspectRatio: false,
  89. canStyleFill: true,
  90. })
  91. export default dot