Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

dot.tsx 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. scale(shape, scale: number) {
  58. return shape
  59. },
  60. translate(shape, delta) {
  61. shape.point = vec.add(shape.point, delta)
  62. return shape
  63. },
  64. transform(shape, bounds) {
  65. shape.point = [bounds.minX, bounds.minY]
  66. return shape
  67. },
  68. canTransform: false,
  69. })
  70. export default dot