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.

arrow.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. Bounds,
  7. ColorStyle,
  8. DashStyle,
  9. ShapeHandle,
  10. ShapeType,
  11. SizeStyle,
  12. } from 'types'
  13. import { registerShapeUtils } from './index'
  14. import {
  15. circleFromThreePoints,
  16. clamp,
  17. getBoundsCenter,
  18. isAngleBetween,
  19. rotateBounds,
  20. } from 'utils/utils'
  21. import { pointInBounds } from 'utils/bounds'
  22. import {
  23. intersectArcBounds,
  24. intersectLineSegmentBounds,
  25. } from 'utils/intersections'
  26. import { getBoundsFromPoints, translateBounds } from 'utils/utils'
  27. import { pointInCircle } from 'utils/hitTests'
  28. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  29. const ctpCache = new WeakMap<ArrowShape['handles'], number[]>()
  30. function getCtp(shape: ArrowShape) {
  31. if (!ctpCache.has(shape.handles)) {
  32. const { start, end, bend } = shape.handles
  33. ctpCache.set(
  34. shape.handles,
  35. circleFromThreePoints(start.point, end.point, bend.point)
  36. )
  37. }
  38. return ctpCache.get(shape.handles)
  39. }
  40. const arrow = registerShapeUtils<ArrowShape>({
  41. boundsCache: new WeakMap([]),
  42. create(props) {
  43. const {
  44. point = [0, 0],
  45. points = [
  46. [0, 0],
  47. [0, 1],
  48. ],
  49. handles = {
  50. start: {
  51. id: 'start',
  52. index: 0,
  53. point: [0, 0],
  54. },
  55. end: {
  56. id: 'end',
  57. index: 1,
  58. point: [1, 1],
  59. },
  60. bend: {
  61. id: 'bend',
  62. index: 2,
  63. point: [0.5, 0.5],
  64. },
  65. },
  66. } = props
  67. return {
  68. id: uuid(),
  69. type: ShapeType.Arrow,
  70. isGenerated: false,
  71. name: 'Arrow',
  72. parentId: 'page0',
  73. childIndex: 0,
  74. point,
  75. rotation: 0,
  76. isAspectRatioLocked: false,
  77. isLocked: false,
  78. isHidden: false,
  79. bend: 0,
  80. points,
  81. handles,
  82. decorations: {
  83. start: null,
  84. end: null,
  85. middle: null,
  86. },
  87. ...props,
  88. style: {
  89. ...defaultStyle,
  90. ...props.style,
  91. isFilled: false,
  92. },
  93. }
  94. },
  95. render(shape) {
  96. const { id, bend, handles } = shape
  97. const { start, end, bend: _bend } = handles
  98. const arrowDist = vec.dist(start.point, end.point)
  99. const showCircle = !vec.isEqual(
  100. _bend.point,
  101. vec.med(start.point, end.point)
  102. )
  103. const style = getShapeStyle(shape.style)
  104. let body: JSX.Element
  105. let endAngle: number
  106. if (showCircle) {
  107. if (!ctpCache.has(handles)) {
  108. ctpCache.set(
  109. handles,
  110. circleFromThreePoints(start.point, end.point, _bend.point)
  111. )
  112. }
  113. const circle = getCtp(shape)
  114. body = (
  115. <path
  116. d={getArrowArcPath(start, end, circle, bend)}
  117. fill="none"
  118. strokeLinecap="round"
  119. />
  120. )
  121. const CE =
  122. vec.angle([circle[0], circle[1]], end.point) -
  123. vec.angle(start.point, end.point) +
  124. (Math.PI / 2) * (bend > 0 ? 0.98 : -0.98)
  125. endAngle = CE
  126. } else {
  127. body = (
  128. <polyline
  129. points={[start.point, end.point].join(' ')}
  130. strokeLinecap="round"
  131. />
  132. )
  133. endAngle = 0
  134. }
  135. // Arrowhead
  136. const length = Math.min(arrowDist / 2, 16 + +style.strokeWidth * 2)
  137. const u = vec.uni(vec.vec(start.point, end.point))
  138. const v = vec.rot(vec.mul(vec.neg(u), length), endAngle)
  139. const b = vec.add(end.point, vec.rot(v, Math.PI / 6))
  140. const c = vec.add(end.point, vec.rot(v, -(Math.PI / 6)))
  141. return (
  142. <g id={id}>
  143. {body}
  144. <circle
  145. cx={start.point[0]}
  146. cy={start.point[1]}
  147. r={+style.strokeWidth}
  148. fill={style.stroke}
  149. strokeDasharray="none"
  150. />
  151. <polyline
  152. points={[b, end.point, c].join()}
  153. strokeLinecap="round"
  154. strokeLinejoin="round"
  155. fill="none"
  156. strokeDasharray="none"
  157. />
  158. </g>
  159. )
  160. },
  161. rotateBy(shape, delta) {
  162. const { start, end, bend } = shape.handles
  163. const mp = vec.med(start.point, end.point)
  164. start.point = vec.rotWith(start.point, mp, delta)
  165. end.point = vec.rotWith(end.point, mp, delta)
  166. bend.point = vec.rotWith(bend.point, mp, delta)
  167. this.onHandleChange(shape, shape.handles)
  168. return this
  169. },
  170. rotateTo(shape, rotation, delta) {
  171. const { start, end, bend } = shape.handles
  172. const mp = vec.med(start.point, end.point)
  173. start.point = vec.rotWith(start.point, mp, delta)
  174. end.point = vec.rotWith(end.point, mp, delta)
  175. bend.point = vec.rotWith(bend.point, mp, delta)
  176. this.onHandleChange(shape, shape.handles)
  177. return this
  178. },
  179. getBounds(shape) {
  180. if (!this.boundsCache.has(shape)) {
  181. const { start, end } = shape.handles
  182. this.boundsCache.set(shape, getBoundsFromPoints([start.point, end.point]))
  183. }
  184. return translateBounds(this.boundsCache.get(shape), shape.point)
  185. },
  186. getRotatedBounds(shape) {
  187. const { start, end } = shape.handles
  188. return translateBounds(
  189. getBoundsFromPoints([start.point, end.point], shape.rotation),
  190. shape.point
  191. )
  192. },
  193. getCenter(shape) {
  194. const { start, end } = shape.handles
  195. return vec.add(shape.point, vec.med(start.point, end.point))
  196. },
  197. hitTest(shape, point) {
  198. const { start, end, bend } = shape.handles
  199. if (shape.bend === 0) {
  200. return (
  201. vec.distanceToLineSegment(
  202. start.point,
  203. end.point,
  204. vec.sub(point, shape.point)
  205. ) < 4
  206. )
  207. }
  208. const [cx, cy, r] = getCtp(shape)
  209. return !pointInCircle(point, vec.add(shape.point, [cx, cy]), r - 4)
  210. },
  211. hitTestBounds(this, shape, brushBounds) {
  212. const { start, end, bend } = shape.handles
  213. const sp = vec.add(shape.point, start.point)
  214. const ep = vec.add(shape.point, end.point)
  215. if (pointInBounds(sp, brushBounds) || pointInBounds(ep, brushBounds)) {
  216. return true
  217. }
  218. if (vec.isEqual(vec.med(start.point, end.point), bend.point)) {
  219. return intersectLineSegmentBounds(sp, ep, brushBounds).length > 0
  220. } else {
  221. const [cx, cy, r] = getCtp(shape)
  222. const cp = vec.add(shape.point, [cx, cy])
  223. return intersectArcBounds(sp, ep, cp, r, brushBounds).length > 0
  224. }
  225. },
  226. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  227. const initialShapeBounds = this.getBounds(initialShape)
  228. shape.point = [bounds.minX, bounds.minY]
  229. shape.points = shape.points.map((_, i) => {
  230. const [x, y] = initialShape.points[i]
  231. let nw = x / initialShapeBounds.width
  232. let nh = y / initialShapeBounds.height
  233. if (i === 1) {
  234. let [x0, y0] = initialShape.points[0]
  235. if (x0 === x) nw = 1
  236. if (y0 === y) nh = 1
  237. }
  238. return [
  239. bounds.width * (scaleX < 0 ? 1 - nw : nw),
  240. bounds.height * (scaleY < 0 ? 1 - nh : nh),
  241. ]
  242. })
  243. const { start, end, bend } = shape.handles
  244. start.point = shape.points[0]
  245. end.point = shape.points[1]
  246. bend.point = getBendPoint(shape)
  247. shape.points = [shape.handles.start.point, shape.handles.end.point]
  248. return this
  249. },
  250. onHandleChange(shape, handles) {
  251. // const oldBounds = this.getRotatedBounds(shape)
  252. // const prevCenter = getBoundsCenter(oldBounds)
  253. for (let id in handles) {
  254. const handle = handles[id]
  255. shape.handles[handle.id] = handle
  256. if (handle.index < 2) {
  257. shape.points[handle.index] = handle.point
  258. }
  259. const { start, end, bend } = shape.handles
  260. const dist = vec.dist(start.point, end.point)
  261. if (handle.id === 'bend') {
  262. const midPoint = vec.med(start.point, end.point)
  263. const u = vec.uni(vec.vec(start.point, end.point))
  264. const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
  265. const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
  266. bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
  267. shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
  268. const sa = vec.angle(end.point, start.point)
  269. const la = sa - Math.PI / 2
  270. if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
  271. shape.bend *= -1
  272. }
  273. }
  274. }
  275. shape.handles.bend.point = getBendPoint(shape)
  276. // const newBounds = this.getRotatedBounds(shape)
  277. // const newCenter = getBoundsCenter(newBounds)
  278. // shape.point = vec.add(shape.point, vec.neg(vec.sub(newCenter, prevCenter)))
  279. return this
  280. },
  281. onSessionComplete(shape) {
  282. const bounds = this.getBounds(shape)
  283. const offset = vec.sub([bounds.minX, bounds.minY], shape.point)
  284. this.translateTo(shape, vec.add(shape.point, offset))
  285. const { start, end, bend } = shape.handles
  286. start.point = vec.sub(start.point, offset)
  287. end.point = vec.sub(end.point, offset)
  288. bend.point = vec.sub(bend.point, offset)
  289. return this
  290. },
  291. applyStyles(shape, style) {
  292. Object.assign(shape.style, style)
  293. shape.style.isFilled = false
  294. return this
  295. },
  296. canStyleFill: false,
  297. })
  298. export default arrow
  299. function getArrowArcPath(
  300. start: ShapeHandle,
  301. end: ShapeHandle,
  302. circle: number[],
  303. bend: number
  304. ) {
  305. return [
  306. 'M',
  307. start.point[0],
  308. start.point[1],
  309. 'A',
  310. circle[2],
  311. circle[2],
  312. 0,
  313. 0,
  314. bend < 0 ? 0 : 1,
  315. end.point[0],
  316. end.point[1],
  317. ].join(' ')
  318. }
  319. function getBendPoint(shape: ArrowShape) {
  320. const { start, end } = shape.handles
  321. const dist = vec.dist(start.point, end.point)
  322. const midPoint = vec.med(start.point, end.point)
  323. const bendDist = (dist / 2) * shape.bend * Math.min(1, dist / 128)
  324. const u = vec.uni(vec.vec(start.point, end.point))
  325. return Math.abs(bendDist) < 10
  326. ? midPoint
  327. : vec.add(midPoint, vec.mul(vec.per(u), bendDist))
  328. }
  329. function getResizeOffset(a: Bounds, b: Bounds) {
  330. const { minX: x0, minY: y0, width: w0, height: h0 } = a
  331. const { minX: x1, minY: y1, width: w1, height: h1 } = b
  332. let delta: number[]
  333. if (h0 === h1 && w0 !== w1) {
  334. if (x0 !== x1) {
  335. // moving left edge, pin right edge
  336. delta = vec.sub([x1, y1 + h1 / 2], [x0, y0 + h0 / 2])
  337. } else {
  338. // moving right edge, pin left edge
  339. delta = vec.sub([x1 + w1, y1 + h1 / 2], [x0 + w0, y0 + h0 / 2])
  340. }
  341. } else if (h0 !== h1 && w0 === w1) {
  342. if (y0 !== y1) {
  343. // moving top edge, pin bottom edge
  344. delta = vec.sub([x1 + w1 / 2, y1], [x0 + w0 / 2, y0])
  345. } else {
  346. // moving bottom edge, pin top edge
  347. delta = vec.sub([x1 + w1 / 2, y1 + h1], [x0 + w0 / 2, y0 + h0])
  348. }
  349. } else if (x0 !== x1) {
  350. if (y0 !== y1) {
  351. // moving top left, pin bottom right
  352. delta = vec.sub([x1, y1], [x0, y0])
  353. } else {
  354. // moving bottom left, pin top right
  355. delta = vec.sub([x1, y1 + h1], [x0, y0 + h0])
  356. }
  357. } else if (y0 !== y1) {
  358. // moving top right, pin bottom left
  359. delta = vec.sub([x1 + w1, y1], [x0 + w0, y0])
  360. } else {
  361. // moving bottom right, pin top left
  362. delta = vec.sub([x1 + w1, y1 + h1], [x0 + w0, y0 + h0])
  363. }
  364. return delta
  365. }