Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

arrow.tsx 14KB

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