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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { DotShape, ShapeType } from "types"
  4. import { boundsCache } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. import { createShape } from "./base-shape"
  8. const dot = createShape<DotShape>({
  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={4} cy={4} r={4} />
  24. },
  25. getBounds(shape) {
  26. if (boundsCache.has(shape)) {
  27. return boundsCache.get(shape)
  28. }
  29. const {
  30. point: [x, y],
  31. } = shape
  32. const bounds = {
  33. minX: x,
  34. maxX: x + 8,
  35. minY: y,
  36. maxY: y + 8,
  37. width: 8,
  38. height: 8,
  39. }
  40. 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. })
  67. export default dot