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

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