您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

arrow.tsx 12KB

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