Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

arrow.tsx 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import * as svg from 'utils/svg'
  4. import {
  5. ArrowShape,
  6. ColorStyle,
  7. DashStyle,
  8. ShapeHandle,
  9. ShapeType,
  10. SizeStyle,
  11. } from 'types'
  12. import { registerShapeUtils } from './index'
  13. import { circleFromThreePoints, clamp, isAngleBetween } from 'utils/utils'
  14. import { pointInBounds } from 'utils/bounds'
  15. import {
  16. intersectArcBounds,
  17. intersectLineSegmentBounds,
  18. } from 'utils/intersections'
  19. import { getBoundsFromPoints, translateBounds } from 'utils/utils'
  20. import { pointInCircle } from 'utils/hitTests'
  21. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  22. const ctpCache = new WeakMap<ArrowShape['handles'], number[]>()
  23. function getCtp(shape: ArrowShape) {
  24. if (!ctpCache.has(shape.handles)) {
  25. const { start, end, bend } = shape.handles
  26. ctpCache.set(
  27. shape.handles,
  28. circleFromThreePoints(start.point, end.point, bend.point)
  29. )
  30. }
  31. return ctpCache.get(shape.handles)
  32. }
  33. const arrow = registerShapeUtils<ArrowShape>({
  34. boundsCache: new WeakMap([]),
  35. create(props) {
  36. const {
  37. point = [0, 0],
  38. points = [
  39. [0, 0],
  40. [0, 1],
  41. ],
  42. handles = {
  43. start: {
  44. id: 'start',
  45. index: 0,
  46. point: [0, 0],
  47. },
  48. end: {
  49. id: 'end',
  50. index: 1,
  51. point: [1, 1],
  52. },
  53. bend: {
  54. id: 'bend',
  55. index: 2,
  56. point: [0.5, 0.5],
  57. },
  58. },
  59. } = props
  60. return {
  61. id: uuid(),
  62. type: ShapeType.Arrow,
  63. isGenerated: false,
  64. name: 'Arrow',
  65. parentId: 'page0',
  66. childIndex: 0,
  67. point,
  68. rotation: 0,
  69. isAspectRatioLocked: false,
  70. isLocked: false,
  71. isHidden: false,
  72. bend: 0,
  73. points,
  74. handles,
  75. decorations: {
  76. start: null,
  77. end: null,
  78. middle: null,
  79. },
  80. ...props,
  81. style: {
  82. ...defaultStyle,
  83. ...props.style,
  84. isFilled: false,
  85. },
  86. }
  87. },
  88. render(shape) {
  89. const { id, bend, points, handles } = shape
  90. const { start, end, bend: _bend } = handles
  91. const arrowDist = vec.dist(start.point, end.point)
  92. const showCircle = !vec.isEqual(
  93. _bend.point,
  94. vec.med(start.point, end.point)
  95. )
  96. const style = getShapeStyle(shape.style)
  97. let body: JSX.Element
  98. let endAngle: number
  99. if (showCircle) {
  100. if (!ctpCache.has(handles)) {
  101. ctpCache.set(
  102. handles,
  103. circleFromThreePoints(start.point, end.point, _bend.point)
  104. )
  105. }
  106. const circle = getCtp(shape)
  107. body = (
  108. <path
  109. d={getArrowArcPath(start, end, circle, bend)}
  110. fill="none"
  111. strokeLinecap="round"
  112. />
  113. )
  114. const CE =
  115. vec.angle([circle[0], circle[1]], end.point) -
  116. vec.angle(start.point, end.point) +
  117. (Math.PI / 2) * (bend > 0 ? 0.98 : -0.98)
  118. endAngle = CE
  119. } else {
  120. body = (
  121. <polyline
  122. points={[start.point, end.point].join(' ')}
  123. strokeLinecap="round"
  124. />
  125. )
  126. endAngle = 0
  127. }
  128. // Arrowhead
  129. const length = Math.min(arrowDist / 2, 16 + +style.strokeWidth * 2)
  130. const u = vec.uni(vec.vec(start.point, end.point))
  131. const v = vec.rot(vec.mul(vec.neg(u), length), endAngle)
  132. const b = vec.add(points[1], vec.rot(v, Math.PI / 6))
  133. const c = vec.add(points[1], vec.rot(v, -(Math.PI / 6)))
  134. return (
  135. <g id={id}>
  136. {body}
  137. <circle
  138. cx={start.point[0]}
  139. cy={start.point[1]}
  140. r={+style.strokeWidth}
  141. fill={style.stroke}
  142. strokeDasharray="none"
  143. />
  144. <polyline
  145. points={[b, points[1], c].join()}
  146. strokeLinecap="round"
  147. strokeLinejoin="round"
  148. fill="none"
  149. strokeDasharray="none"
  150. />
  151. </g>
  152. )
  153. },
  154. getBounds(shape) {
  155. if (!this.boundsCache.has(shape)) {
  156. this.boundsCache.set(shape, getBoundsFromPoints(shape.points))
  157. }
  158. return translateBounds(this.boundsCache.get(shape), shape.point)
  159. },
  160. hitTest(shape, point) {
  161. const { start, end, bend } = shape.handles
  162. if (shape.bend === 0) {
  163. return (
  164. vec.distanceToLineSegment(
  165. start.point,
  166. end.point,
  167. vec.sub(point, shape.point)
  168. ) < 4
  169. )
  170. }
  171. const [cx, cy, r] = getCtp(shape)
  172. return !pointInCircle(point, vec.add(shape.point, [cx, cy]), r - 4)
  173. },
  174. hitTestBounds(this, shape, brushBounds) {
  175. const { start, end, bend } = shape.handles
  176. const sp = vec.add(shape.point, start.point)
  177. const ep = vec.add(shape.point, end.point)
  178. if (pointInBounds(sp, brushBounds) || pointInBounds(ep, brushBounds)) {
  179. return true
  180. }
  181. if (vec.isEqual(vec.med(start.point, end.point), bend.point)) {
  182. return intersectLineSegmentBounds(sp, ep, brushBounds).length > 0
  183. } else {
  184. const [cx, cy, r] = getCtp(shape)
  185. const cp = vec.add(shape.point, [cx, cy])
  186. return intersectArcBounds(sp, ep, cp, r, brushBounds).length > 0
  187. }
  188. },
  189. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  190. const initialShapeBounds = this.getBounds(initialShape)
  191. shape.point = [bounds.minX, bounds.minY]
  192. shape.points = shape.points.map((_, i) => {
  193. const [x, y] = initialShape.points[i]
  194. let nw = x / initialShapeBounds.width
  195. let nh = y / initialShapeBounds.height
  196. if (i === 1) {
  197. let [x0, y0] = initialShape.points[0]
  198. if (x0 === x) nw = 1
  199. if (y0 === y) nh = 1
  200. }
  201. return [
  202. bounds.width * (scaleX < 0 ? 1 - nw : nw),
  203. bounds.height * (scaleY < 0 ? 1 - nh : nh),
  204. ]
  205. })
  206. const { start, end, bend } = shape.handles
  207. start.point = shape.points[0]
  208. end.point = shape.points[1]
  209. bend.point = getBendPoint(shape)
  210. shape.points = [shape.handles.start.point, shape.handles.end.point]
  211. return this
  212. },
  213. onHandleMove(shape, handles) {
  214. for (let id in handles) {
  215. const handle = handles[id]
  216. shape.handles[handle.id] = handle
  217. if (handle.index < 2) {
  218. shape.points[handle.index] = handle.point
  219. }
  220. const { start, end, bend } = shape.handles
  221. const dist = vec.dist(start.point, end.point)
  222. if (handle.id === 'bend') {
  223. const midPoint = vec.med(start.point, end.point)
  224. const u = vec.uni(vec.vec(start.point, end.point))
  225. const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
  226. const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
  227. bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
  228. shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
  229. const sa = vec.angle(end.point, start.point)
  230. const la = sa - Math.PI / 2
  231. if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
  232. shape.bend *= -1
  233. }
  234. }
  235. }
  236. shape.handles.bend.point = getBendPoint(shape)
  237. return this
  238. },
  239. applyStyles(shape, style) {
  240. Object.assign(shape.style, style)
  241. shape.style.isFilled = false
  242. return this
  243. },
  244. canStyleFill: false,
  245. })
  246. export default arrow
  247. function getArrowArcPath(
  248. start: ShapeHandle,
  249. end: ShapeHandle,
  250. circle: number[],
  251. bend: number
  252. ) {
  253. return [
  254. 'M',
  255. start.point[0],
  256. start.point[1],
  257. 'A',
  258. circle[2],
  259. circle[2],
  260. 0,
  261. 0,
  262. bend < 0 ? 0 : 1,
  263. end.point[0],
  264. end.point[1],
  265. ].join(' ')
  266. }
  267. function getBendPoint(shape: ArrowShape) {
  268. const { start, end } = shape.handles
  269. const dist = vec.dist(start.point, end.point)
  270. const midPoint = vec.med(start.point, end.point)
  271. const bendDist = (dist / 2) * shape.bend * Math.min(1, dist / 128)
  272. const u = vec.uni(vec.vec(start.point, end.point))
  273. return Math.abs(bendDist) < 10
  274. ? midPoint
  275. : vec.add(midPoint, vec.mul(vec.per(u), bendDist))
  276. }