Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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