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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. name: "Dot",
  14. parentId: "page0",
  15. childIndex: 0,
  16. point: [0, 0],
  17. rotation: 0,
  18. style: {},
  19. ...props,
  20. }
  21. },
  22. render({ id }) {
  23. return <circle id={id} cx={0} cy={0} r={4} />
  24. },
  25. getBounds(shape) {
  26. if (this.boundsCache.has(shape)) {
  27. return this.boundsCache.get(shape)
  28. }
  29. const {
  30. point: [x, y],
  31. } = shape
  32. const bounds = {
  33. minX: x,
  34. maxX: x + 1,
  35. minY: y,
  36. maxY: y + 1,
  37. width: 1,
  38. height: 1,
  39. }
  40. this.boundsCache.set(shape, bounds)
  41. return bounds
  42. },
  43. hitTest(shape, test) {
  44. return vec.dist(shape.point, test) < 4
  45. },
  46. hitTestBounds(this, shape, brushBounds) {
  47. const shapeBounds = this.getBounds(shape)
  48. return (
  49. boundsContained(shapeBounds, brushBounds) ||
  50. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  51. )
  52. },
  53. rotate(shape) {
  54. return shape
  55. },
  56. translate(shape, delta) {
  57. shape.point = vec.add(shape.point, delta)
  58. return shape
  59. },
  60. scale(shape, scale: number) {
  61. return shape
  62. },
  63. stretch(shape, scaleX: number, scaleY: number) {
  64. return shape
  65. },
  66. transform(shape, bounds) {
  67. shape.point = [bounds.minX, bounds.minY]
  68. return shape
  69. },
  70. })
  71. export default dot