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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import {
  4. ease,
  5. getSvgPathFromStroke,
  6. rng,
  7. getBoundsFromPoints,
  8. translateBounds,
  9. pointsBetween,
  10. } from 'utils/utils'
  11. import { ArrowShape, Bounds, ShapeHandle, ShapeType } from 'types'
  12. import { registerShapeUtils } from './index'
  13. import { circleFromThreePoints, isAngleBetween } from 'utils/utils'
  14. import { pointInBounds } from 'utils/bounds'
  15. import {
  16. intersectArcBounds,
  17. intersectLineSegmentBounds,
  18. } from 'utils/intersections'
  19. import { pointInCircle } from 'utils/hitTests'
  20. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  21. import getStroke from 'perfect-freehand'
  22. const ctpCache = new WeakMap<ArrowShape['handles'], number[]>()
  23. const pathCache = new WeakMap<ArrowShape, string>([])
  24. function getCtp(shape: ArrowShape) {
  25. if (!ctpCache.has(shape.handles)) {
  26. const { start, end, bend } = shape.handles
  27. ctpCache.set(
  28. shape.handles,
  29. circleFromThreePoints(start.point, end.point, bend.point)
  30. )
  31. }
  32. return ctpCache.get(shape.handles)
  33. }
  34. const arrow = registerShapeUtils<ArrowShape>({
  35. boundsCache: new WeakMap([]),
  36. create(props) {
  37. const {
  38. point = [0, 0],
  39. points = [
  40. [0, 0],
  41. [0, 1],
  42. ],
  43. handles = {
  44. start: {
  45. id: 'start',
  46. index: 0,
  47. point: [0, 0],
  48. },
  49. end: {
  50. id: 'end',
  51. index: 1,
  52. point: [1, 1],
  53. },
  54. bend: {
  55. id: 'bend',
  56. index: 2,
  57. point: [0.5, 0.5],
  58. },
  59. },
  60. } = props
  61. return {
  62. id: uuid(),
  63. seed: Math.random(),
  64. type: ShapeType.Arrow,
  65. isGenerated: false,
  66. name: 'Arrow',
  67. parentId: 'page1',
  68. childIndex: 0,
  69. point,
  70. rotation: 0,
  71. isAspectRatioLocked: false,
  72. isLocked: false,
  73. isHidden: false,
  74. bend: 0,
  75. points,
  76. handles,
  77. decorations: {
  78. start: null,
  79. end: null,
  80. middle: null,
  81. },
  82. ...props,
  83. style: {
  84. ...defaultStyle,
  85. ...props.style,
  86. isFilled: false,
  87. },
  88. }
  89. },
  90. render(shape) {
  91. const { id, bend, handles } = shape
  92. const { start, end, bend: _bend } = handles
  93. const arrowDist = vec.dist(start.point, end.point)
  94. const showCircle = !vec.isEqual(
  95. _bend.point,
  96. vec.med(start.point, end.point)
  97. )
  98. const style = getShapeStyle(shape.style)
  99. let body: JSX.Element
  100. if (showCircle) {
  101. if (!ctpCache.has(handles)) {
  102. ctpCache.set(
  103. handles,
  104. circleFromThreePoints(start.point, end.point, _bend.point)
  105. )
  106. }
  107. const circle = getCtp(shape)
  108. if (!pathCache.has(shape)) {
  109. renderPath(
  110. shape,
  111. vec.angle([circle[0], circle[1]], end.point) -
  112. vec.angle(start.point, end.point) +
  113. (Math.PI / 2) * (bend > 0 ? 0.98 : -0.98)
  114. )
  115. }
  116. const path = pathCache.get(shape)
  117. body = (
  118. <>
  119. <path
  120. d={getArrowArcPath(start, end, circle, bend)}
  121. fill="none"
  122. strokeWidth={+style.strokeWidth * 1.85}
  123. strokeLinecap="round"
  124. />
  125. <path d={path} strokeWidth={+style.strokeWidth * 1.5} />
  126. </>
  127. )
  128. } else {
  129. if (!pathCache.has(shape)) {
  130. renderPath(shape)
  131. }
  132. const path = pathCache.get(shape)
  133. body = <path d={path} />
  134. }
  135. return (
  136. <g id={id}>
  137. {body}
  138. {/* <circle
  139. cx={start.point[0]}
  140. cy={start.point[1]}
  141. r={Math.max(4, +style.strokeWidth)}
  142. fill={style.stroke}
  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. }
  353. function renderPath(shape: ArrowShape, endAngle = 0) {
  354. const { style, id } = shape
  355. const { start, end, bend } = shape.handles
  356. const getRandom = rng(id)
  357. const offsetA = getRandom()
  358. const strokeWidth = +getShapeStyle(style).strokeWidth * 2
  359. const arrowDist = vec.dist(start.point, end.point)
  360. const styles = getShapeStyle(shape.style)
  361. const sw = +styles.strokeWidth
  362. const length = Math.min(arrowDist / 2, 24 + sw * 2)
  363. const u = vec.uni(vec.vec(start.point, end.point))
  364. const v = vec.rot(vec.mul(vec.neg(u), length), endAngle)
  365. // Start
  366. const a = start.point
  367. // Middle
  368. const m = vec.add(
  369. vec.lrp(start.point, end.point, 0.25 + Math.abs(getRandom()) / 2),
  370. [getRandom() * sw, getRandom() * sw]
  371. )
  372. // End
  373. const b = end.point
  374. // Left
  375. let c = vec.add(
  376. end.point,
  377. vec.rot(v, Math.PI / 6 + (Math.PI / 8) * getRandom())
  378. )
  379. // Right
  380. let d = vec.add(
  381. end.point,
  382. vec.rot(v, -(Math.PI / 6) + (Math.PI / 8) * getRandom())
  383. )
  384. if (getRandom() > 0.5) {
  385. ;[c, d] = [d, c]
  386. }
  387. const points = endAngle
  388. ? [
  389. // Just the arrowhead
  390. ...pointsBetween(b, c),
  391. ...pointsBetween(c, b),
  392. ...pointsBetween(b, d),
  393. ...pointsBetween(d, b),
  394. ]
  395. : [
  396. // The shaft too
  397. b,
  398. a,
  399. ...pointsBetween(a, m),
  400. ...pointsBetween(m, b),
  401. ...pointsBetween(b, c),
  402. ...pointsBetween(c, b),
  403. ...pointsBetween(b, d),
  404. ...pointsBetween(d, b),
  405. ]
  406. const stroke = getStroke(points, {
  407. size: 1 + strokeWidth,
  408. thinning: 0.6,
  409. easing: (t) => t * t * t * t,
  410. end: { taper: strokeWidth * 20 },
  411. start: { taper: strokeWidth * 20 },
  412. simulatePressure: false,
  413. })
  414. pathCache.set(shape, getSvgPathFromStroke(stroke))
  415. }