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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 styled from "styles"
  8. import { DotCircle } from "components/canvas/misc"
  9. import { translateBounds } from "utils/utils"
  10. const dot = registerShapeUtils<DotShape>({
  11. boundsCache: new WeakMap([]),
  12. create(props) {
  13. return {
  14. id: uuid(),
  15. type: ShapeType.Dot,
  16. isGenerated: false,
  17. name: "Dot",
  18. parentId: "page0",
  19. childIndex: 0,
  20. point: [0, 0],
  21. rotation: 0,
  22. style: {
  23. fill: "#c6cacb",
  24. stroke: "#000",
  25. },
  26. ...props,
  27. }
  28. },
  29. render({ id }) {
  30. return <DotCircle id={id} cx={0} cy={0} r={4} />
  31. },
  32. getBounds(shape) {
  33. if (!this.boundsCache.has(shape)) {
  34. const bounds = {
  35. minX: 0,
  36. maxX: 1,
  37. minY: 0,
  38. maxY: 1,
  39. width: 1,
  40. height: 1,
  41. }
  42. this.boundsCache.set(shape, bounds)
  43. }
  44. return translateBounds(this.boundsCache.get(shape), shape.point)
  45. },
  46. getRotatedBounds(shape) {
  47. return this.getBounds(shape)
  48. },
  49. getCenter(shape) {
  50. return shape.point
  51. },
  52. hitTest(shape, test) {
  53. return true
  54. },
  55. hitTestBounds(this, shape, brushBounds) {
  56. const shapeBounds = this.getBounds(shape)
  57. return (
  58. boundsContained(shapeBounds, brushBounds) ||
  59. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  60. )
  61. },
  62. rotate(shape) {
  63. return shape
  64. },
  65. scale(shape, scale: number) {
  66. return shape
  67. },
  68. translate(shape, delta) {
  69. shape.point = vec.add(shape.point, delta)
  70. return shape
  71. },
  72. transform(shape, bounds) {
  73. shape.point = [bounds.minX, bounds.minY]
  74. return shape
  75. },
  76. transformSingle(shape, bounds, info) {
  77. return this.transform(shape, bounds, info)
  78. },
  79. canTransform: false,
  80. canChangeAspectRatio: false,
  81. })
  82. export default dot