Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ray.tsx 2.4KB

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