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

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