您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

line.tsx 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { LineShape, ShapeType } from "types"
  4. import { registerShapeUtils } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. import { DotCircle } from "components/canvas/misc"
  8. import { translateBounds } from "utils/utils"
  9. const line = registerShapeUtils<LineShape>({
  10. boundsCache: new WeakMap([]),
  11. create(props) {
  12. return {
  13. id: uuid(),
  14. type: ShapeType.Line,
  15. isGenerated: false,
  16. name: "Line",
  17. parentId: "page0",
  18. childIndex: 0,
  19. point: [0, 0],
  20. direction: [0, 0],
  21. rotation: 0,
  22. style: {
  23. fill: "#c6cacb",
  24. stroke: "#000",
  25. },
  26. ...props,
  27. }
  28. },
  29. render({ id, direction }) {
  30. const [x1, y1] = vec.add([0, 0], vec.mul(direction, 100000))
  31. const [x2, y2] = vec.sub([0, 0], vec.mul(direction, 100000))
  32. return (
  33. <g id={id}>
  34. <line x1={x1} y1={y1} x2={x2} y2={y2} />
  35. <DotCircle cx={0} cy={0} r={3} />
  36. </g>
  37. )
  38. },
  39. getBounds(shape) {
  40. if (!this.boundsCache.has(shape)) {
  41. const bounds = {
  42. minX: 0,
  43. maxX: 1,
  44. minY: 0,
  45. maxY: 1,
  46. width: 1,
  47. height: 1,
  48. }
  49. this.boundsCache.set(shape, bounds)
  50. }
  51. return translateBounds(this.boundsCache.get(shape), shape.point)
  52. },
  53. getRotatedBounds(shape) {
  54. return this.getBounds(shape)
  55. },
  56. getCenter(shape) {
  57. return shape.point
  58. },
  59. hitTest(shape, test) {
  60. return true
  61. },
  62. hitTestBounds(this, shape, brushBounds) {
  63. const shapeBounds = this.getBounds(shape)
  64. return (
  65. boundsContained(shapeBounds, brushBounds) ||
  66. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  67. )
  68. },
  69. rotate(shape) {
  70. return this
  71. },
  72. translate(shape, delta) {
  73. shape.point = vec.add(shape.point, delta)
  74. return this
  75. },
  76. transform(shape, bounds) {
  77. shape.point = [bounds.minX, bounds.minY]
  78. return this
  79. },
  80. transformSingle(shape, bounds, info) {
  81. return this.transform(shape, bounds, info)
  82. },
  83. setParent(shape, parentId) {
  84. shape.parentId = parentId
  85. return this
  86. },
  87. setChildIndex(shape, childIndex) {
  88. shape.childIndex = childIndex
  89. return this
  90. },
  91. canTransform: false,
  92. canChangeAspectRatio: false,
  93. })
  94. export default line