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 11KB

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