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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import { getArcLength, uniqueId } from 'utils/utils'
  2. import vec from 'utils/vec'
  3. import {
  4. getSvgPathFromStroke,
  5. rng,
  6. getBoundsFromPoints,
  7. translateBounds,
  8. pointsBetween,
  9. } from 'utils/utils'
  10. import {
  11. ArrowShape,
  12. DashStyle,
  13. Decoration,
  14. ShapeHandle,
  15. ShapeType,
  16. } from 'types'
  17. import { circleFromThreePoints, isAngleBetween } from 'utils/utils'
  18. import { pointInBounds } from 'utils/hitTests'
  19. import {
  20. intersectArcBounds,
  21. intersectLineSegmentBounds,
  22. } from 'utils/intersections'
  23. import { pointInCircle } from 'utils/hitTests'
  24. import { defaultStyle, getShapeStyle } from 'state/shape-styles'
  25. import getStroke from 'perfect-freehand'
  26. import React from 'react'
  27. import { registerShapeUtils } from './register'
  28. import { getPerfectDashProps } from 'utils/dashes'
  29. const pathCache = new WeakMap<ArrowShape, string>([])
  30. // A cache for semi-expensive circles calculated from three points
  31. function getCtp(shape: ArrowShape) {
  32. const { start, end, bend } = shape.handles
  33. return circleFromThreePoints(start.point, end.point, bend.point)
  34. }
  35. const arrow = registerShapeUtils<ArrowShape>({
  36. boundsCache: new WeakMap([]),
  37. create(props) {
  38. const {
  39. point = [0, 0],
  40. handles = {
  41. start: {
  42. id: 'start',
  43. index: 0,
  44. point: [0, 0],
  45. },
  46. end: {
  47. id: 'end',
  48. index: 1,
  49. point: [1, 1],
  50. },
  51. bend: {
  52. id: 'bend',
  53. index: 2,
  54. point: [0.5, 0.5],
  55. },
  56. },
  57. } = props
  58. return {
  59. id: uniqueId(),
  60. seed: Math.random(),
  61. type: ShapeType.Arrow,
  62. isGenerated: false,
  63. name: 'Arrow',
  64. parentId: 'page1',
  65. childIndex: 0,
  66. point,
  67. rotation: 0,
  68. isAspectRatioLocked: false,
  69. isLocked: false,
  70. isHidden: false,
  71. bend: 0,
  72. handles,
  73. decorations: {
  74. start: null,
  75. middle: null,
  76. end: Decoration.Arrow,
  77. },
  78. ...props,
  79. style: {
  80. ...defaultStyle,
  81. ...props.style,
  82. isFilled: false,
  83. },
  84. }
  85. },
  86. render(shape) {
  87. const { id, bend, handles, style } = shape
  88. const { start, end, bend: _bend } = handles
  89. const isStraightLine = vec.isEqual(
  90. _bend.point,
  91. vec.med(start.point, end.point)
  92. )
  93. const styles = getShapeStyle(style)
  94. const strokeWidth = +styles.strokeWidth
  95. const arrowDist = vec.dist(start.point, end.point)
  96. let shaftPath: JSX.Element
  97. let startAngle: number
  98. let endAngle: number
  99. if (isStraightLine) {
  100. if (shape.style.dash === DashStyle.Solid && !pathCache.has(shape)) {
  101. renderFreehandArrowShaft(shape)
  102. }
  103. const path =
  104. shape.style.dash === DashStyle.Solid
  105. ? pathCache.get(shape)
  106. : 'M' + start.point + 'L' + end.point
  107. const { strokeDasharray, strokeDashoffset } =
  108. shape.style.dash === DashStyle.Solid
  109. ? {
  110. strokeDasharray: 'none',
  111. strokeDashoffset: '0',
  112. }
  113. : getPerfectDashProps(
  114. arrowDist,
  115. strokeWidth * 1.618,
  116. shape.style.dash === DashStyle.Dotted ? 'dotted' : 'dashed',
  117. 2
  118. )
  119. startAngle = Math.PI
  120. endAngle = 0
  121. shaftPath = (
  122. <>
  123. <path
  124. d={path}
  125. stroke="transparent"
  126. fill="none"
  127. strokeWidth={Math.max(8, strokeWidth * 2)}
  128. strokeDasharray="none"
  129. strokeDashoffset="none"
  130. strokeLinecap="round"
  131. />
  132. <path
  133. d={path}
  134. fill="none"
  135. strokeWidth={
  136. strokeWidth * (style.dash === DashStyle.Solid ? 1 : 1.618)
  137. }
  138. strokeDasharray={strokeDasharray}
  139. strokeDashoffset={strokeDashoffset}
  140. strokeLinecap="round"
  141. ></path>
  142. </>
  143. )
  144. } else {
  145. const circle = getCtp(shape)
  146. const path = getArrowArcPath(start, end, circle, bend)
  147. const { strokeDasharray, strokeDashoffset } =
  148. shape.style.dash === DashStyle.Solid
  149. ? {
  150. strokeDasharray: 'none',
  151. strokeDashoffset: '0',
  152. }
  153. : getPerfectDashProps(
  154. getArcLength(
  155. [circle[0], circle[1]],
  156. circle[2],
  157. start.point,
  158. end.point
  159. ) - 1,
  160. strokeWidth * 1.618,
  161. shape.style.dash === DashStyle.Dotted ? 'dotted' : 'dashed',
  162. 2
  163. )
  164. startAngle =
  165. vec.angle([circle[0], circle[1]], start.point) -
  166. vec.angle(end.point, start.point) +
  167. (Math.PI / 2) * (bend > 0 ? 0.98 : -0.98)
  168. endAngle =
  169. vec.angle([circle[0], circle[1]], end.point) -
  170. vec.angle(start.point, end.point) +
  171. (Math.PI / 2) * (bend > 0 ? 0.98 : -0.98)
  172. shaftPath = (
  173. <>
  174. <path
  175. d={path}
  176. stroke="transparent"
  177. fill="none"
  178. strokeWidth={Math.max(8, strokeWidth * 2)}
  179. strokeDasharray="none"
  180. strokeDashoffset="none"
  181. strokeLinecap="round"
  182. />
  183. <path
  184. d={path}
  185. fill="none"
  186. strokeWidth={strokeWidth * 2}
  187. strokeDasharray={strokeDasharray}
  188. strokeDashoffset={strokeDashoffset}
  189. strokeLinecap="round"
  190. ></path>
  191. </>
  192. )
  193. }
  194. return (
  195. <g id={id}>
  196. {shaftPath}
  197. {shape.decorations.start === Decoration.Arrow && (
  198. <path
  199. d={getArrowHeadPath(shape, start.point, startAngle)}
  200. strokeWidth={strokeWidth * 1.618}
  201. fill="none"
  202. strokeDashoffset="none"
  203. strokeDasharray="none"
  204. />
  205. )}
  206. {shape.decorations.end === Decoration.Arrow && (
  207. <path
  208. d={getArrowHeadPath(shape, end.point, endAngle)}
  209. strokeWidth={strokeWidth * 1.618}
  210. fill="none"
  211. strokeDashoffset="none"
  212. strokeDasharray="none"
  213. />
  214. )}
  215. </g>
  216. )
  217. },
  218. rotateBy(shape, delta) {
  219. const { start, end, bend } = shape.handles
  220. const mp = vec.med(start.point, end.point)
  221. start.point = vec.rotWith(start.point, mp, delta)
  222. end.point = vec.rotWith(end.point, mp, delta)
  223. bend.point = vec.rotWith(bend.point, mp, delta)
  224. this.onHandleChange(shape, shape.handles)
  225. return this
  226. },
  227. rotateTo(shape, rotation, delta) {
  228. const { start, end, bend } = shape.handles
  229. const mp = vec.med(start.point, end.point)
  230. start.point = vec.rotWith(start.point, mp, delta)
  231. end.point = vec.rotWith(end.point, mp, delta)
  232. bend.point = vec.rotWith(bend.point, mp, delta)
  233. this.onHandleChange(shape, shape.handles)
  234. return this
  235. },
  236. getBounds(shape) {
  237. if (!this.boundsCache.has(shape)) {
  238. const { start, bend, end } = shape.handles
  239. this.boundsCache.set(
  240. shape,
  241. getBoundsFromPoints([start.point, bend.point, end.point])
  242. )
  243. }
  244. return translateBounds(this.boundsCache.get(shape), shape.point)
  245. },
  246. getRotatedBounds(shape) {
  247. const { start, bend, end } = shape.handles
  248. return translateBounds(
  249. getBoundsFromPoints([start.point, bend.point, end.point], shape.rotation),
  250. shape.point
  251. )
  252. },
  253. getCenter(shape) {
  254. const { start, end } = shape.handles
  255. return vec.add(shape.point, vec.med(start.point, end.point))
  256. },
  257. hitTest(shape, point) {
  258. const { start, end } = shape.handles
  259. if (shape.bend === 0) {
  260. return (
  261. vec.distanceToLineSegment(
  262. start.point,
  263. end.point,
  264. vec.sub(point, shape.point)
  265. ) < 4
  266. )
  267. }
  268. const [cx, cy, r] = getCtp(shape)
  269. return !pointInCircle(point, vec.add(shape.point, [cx, cy]), r - 4)
  270. },
  271. hitTestBounds(this, shape, brushBounds) {
  272. const { start, end, bend } = shape.handles
  273. const sp = vec.add(shape.point, start.point)
  274. const ep = vec.add(shape.point, end.point)
  275. if (pointInBounds(sp, brushBounds) || pointInBounds(ep, brushBounds)) {
  276. return true
  277. }
  278. if (vec.isEqual(vec.med(start.point, end.point), bend.point)) {
  279. return intersectLineSegmentBounds(sp, ep, brushBounds).length > 0
  280. } else {
  281. const [cx, cy, r] = getCtp(shape)
  282. const cp = vec.add(shape.point, [cx, cy])
  283. return intersectArcBounds(sp, ep, cp, r, brushBounds).length > 0
  284. }
  285. },
  286. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  287. const initialShapeBounds = this.getBounds(initialShape)
  288. // let nw = initialShape.point[0] / initialShapeBounds.width
  289. // let nh = initialShape.point[1] / initialShapeBounds.height
  290. // shape.point = [
  291. // bounds.width * (scaleX < 0 ? 1 - nw : nw),
  292. // bounds.height * (scaleY < 0 ? 1 - nh : nh),
  293. // ]
  294. shape.point = [bounds.minX, bounds.minY]
  295. const handles = ['start', 'end']
  296. handles.forEach((handle) => {
  297. const [x, y] = initialShape.handles[handle].point
  298. const nw = x / initialShapeBounds.width
  299. const nh = y / initialShapeBounds.height
  300. shape.handles[handle].point = [
  301. bounds.width * (scaleX < 0 ? 1 - nw : nw),
  302. bounds.height * (scaleY < 0 ? 1 - nh : nh),
  303. ]
  304. })
  305. const { start, bend, end } = shape.handles
  306. const dist = vec.dist(start.point, end.point)
  307. const midPoint = vec.med(start.point, end.point)
  308. const bendDist = (dist / 2) * initialShape.bend
  309. const u = vec.uni(vec.vec(start.point, end.point))
  310. const point = vec.add(midPoint, vec.mul(vec.per(u), bendDist))
  311. bend.point = Math.abs(bendDist) < 10 ? midPoint : point
  312. return this
  313. },
  314. onDoublePointHandle(shape, handle) {
  315. switch (handle) {
  316. case 'bend': {
  317. shape.bend = 0
  318. shape.handles.bend.point = getBendPoint(shape)
  319. break
  320. }
  321. case 'start': {
  322. shape.decorations.start = shape.decorations.start
  323. ? null
  324. : Decoration.Arrow
  325. break
  326. }
  327. case 'end': {
  328. shape.decorations.end = shape.decorations.end ? null : Decoration.Arrow
  329. break
  330. }
  331. }
  332. return this
  333. },
  334. onHandleChange(shape, handles) {
  335. for (const id in handles) {
  336. const handle = handles[id]
  337. shape.handles[handle.id] = handle
  338. const { start, end, bend } = shape.handles
  339. const dist = vec.dist(start.point, end.point)
  340. if (handle.id === 'bend') {
  341. const midPoint = vec.med(start.point, end.point)
  342. const u = vec.uni(vec.vec(start.point, end.point))
  343. const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
  344. const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
  345. bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
  346. shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
  347. const sa = vec.angle(end.point, start.point)
  348. const la = sa - Math.PI / 2
  349. if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
  350. shape.bend *= -1
  351. }
  352. }
  353. }
  354. shape.handles.bend.point = getBendPoint(shape)
  355. // const newBounds = this.getRotatedBounds(shape)
  356. // const newCenter = getBoundsCenter(newBounds)
  357. // shape.point = vec.add(shape.point, vec.neg(vec.sub(newCenter, prevCenter)))
  358. return this
  359. },
  360. onSessionComplete(shape) {
  361. const bounds = this.getBounds(shape)
  362. const offset = vec.sub([bounds.minX, bounds.minY], shape.point)
  363. this.translateTo(shape, vec.add(shape.point, offset))
  364. const { start, end, bend } = shape.handles
  365. start.point = vec.sub(start.point, offset)
  366. end.point = vec.sub(end.point, offset)
  367. bend.point = vec.sub(bend.point, offset)
  368. shape.handles = { ...shape.handles }
  369. return this
  370. },
  371. applyStyles(shape, style) {
  372. Object.assign(shape.style, style)
  373. shape.style.isFilled = false
  374. return this
  375. },
  376. canStyleFill: false,
  377. })
  378. export default arrow
  379. function getArrowArcPath(
  380. start: ShapeHandle,
  381. end: ShapeHandle,
  382. circle: number[],
  383. bend: number
  384. ) {
  385. return [
  386. 'M',
  387. start.point[0],
  388. start.point[1],
  389. 'A',
  390. circle[2],
  391. circle[2],
  392. 0,
  393. 0,
  394. bend < 0 ? 0 : 1,
  395. end.point[0],
  396. end.point[1],
  397. ].join(' ')
  398. }
  399. function getBendPoint(shape: ArrowShape) {
  400. const { start, end } = shape.handles
  401. const dist = vec.dist(start.point, end.point)
  402. const midPoint = vec.med(start.point, end.point)
  403. const bendDist = (dist / 2) * shape.bend
  404. const u = vec.uni(vec.vec(start.point, end.point))
  405. return Math.abs(bendDist) < 10
  406. ? midPoint
  407. : vec.add(midPoint, vec.mul(vec.per(u), bendDist))
  408. }
  409. function renderFreehandArrowShaft(shape: ArrowShape) {
  410. const { style, id } = shape
  411. const { start, end } = shape.handles
  412. const getRandom = rng(id)
  413. const strokeWidth = +getShapeStyle(style).strokeWidth * 2
  414. const m = vec.add(
  415. vec.lrp(start.point, end.point, 0.25 + Math.abs(getRandom()) / 2),
  416. [getRandom() * strokeWidth, getRandom() * strokeWidth]
  417. )
  418. const stroke = getStroke(
  419. [
  420. ...pointsBetween(start.point, m),
  421. ...pointsBetween(m, end.point),
  422. end.point,
  423. end.point,
  424. end.point,
  425. ],
  426. {
  427. size: 1 + strokeWidth,
  428. thinning: 0.6,
  429. easing: (t) => t * t * t * t,
  430. end: { taper: strokeWidth * 2 },
  431. start: { taper: strokeWidth * 2 },
  432. simulatePressure: false,
  433. }
  434. )
  435. pathCache.set(shape, getSvgPathFromStroke(stroke))
  436. }
  437. function getArrowHeadPath(shape: ArrowShape, point: number[], angle = 0) {
  438. const { left, right } = getArrowHeadPoints(shape, point, angle)
  439. return ['M', left, 'L', point, right].join(' ')
  440. }
  441. function getArrowHeadPoints(shape: ArrowShape, point: number[], angle = 0) {
  442. const { start, end } = shape.handles
  443. const stroke = +getShapeStyle(shape.style).strokeWidth * 2
  444. const arrowDist = vec.dist(start.point, end.point)
  445. const arrowHeadlength = Math.min(arrowDist / 3, stroke * 4)
  446. // Unit vector from start to end
  447. const u = vec.uni(vec.vec(start.point, end.point))
  448. // The end of the arrowhead wings
  449. const v = vec.rot(vec.mul(vec.neg(u), arrowHeadlength), angle)
  450. // Use the shape's random seed to create minor offsets for the angles
  451. const getRandom = rng(shape.id)
  452. return {
  453. left: vec.add(point, vec.rot(v, Math.PI / 6 + (Math.PI / 8) * getRandom())),
  454. right: vec.add(
  455. point,
  456. vec.rot(v, -(Math.PI / 6) + (Math.PI / 8) * getRandom())
  457. ),
  458. }
  459. }