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.

arrow.tsx 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import * as svg from 'utils/svg'
  4. import {
  5. ArrowShape,
  6. ColorStyle,
  7. DashStyle,
  8. ShapeHandle,
  9. ShapeType,
  10. SizeStyle,
  11. } from 'types'
  12. import { registerShapeUtils } from './index'
  13. import { circleFromThreePoints, clamp, isAngleBetween } from 'utils/utils'
  14. import { pointInBounds } from 'utils/bounds'
  15. import {
  16. intersectArcBounds,
  17. intersectLineSegmentBounds,
  18. } from 'utils/intersections'
  19. import { getBoundsFromPoints, translateBounds } from 'utils/utils'
  20. import { pointInCircle } from 'utils/hitTests'
  21. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  22. const ctpCache = new WeakMap<ArrowShape['handles'], number[]>()
  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: uuid(),
  62. type: ShapeType.Arrow,
  63. isGenerated: false,
  64. name: 'Arrow',
  65. parentId: 'page0',
  66. childIndex: 0,
  67. point,
  68. rotation: 0,
  69. isAspectRatioLocked: false,
  70. isLocked: false,
  71. isHidden: false,
  72. bend: 0,
  73. points,
  74. handles,
  75. decorations: {
  76. start: null,
  77. end: null,
  78. middle: null,
  79. },
  80. ...props,
  81. style: {
  82. ...defaultStyle,
  83. ...props.style,
  84. isFilled: false,
  85. },
  86. }
  87. },
  88. render(shape) {
  89. const { id, bend, handles } = shape
  90. const { start, end, bend: _bend } = handles
  91. const arrowDist = vec.dist(start.point, end.point)
  92. const showCircle = !vec.isEqual(
  93. _bend.point,
  94. vec.med(start.point, end.point)
  95. )
  96. const style = getShapeStyle(shape.style)
  97. let body: JSX.Element
  98. let endAngle: number
  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. body = (
  108. <path
  109. d={getArrowArcPath(start, end, circle, bend)}
  110. fill="none"
  111. strokeLinecap="round"
  112. />
  113. )
  114. const CE =
  115. vec.angle([circle[0], circle[1]], end.point) -
  116. vec.angle(start.point, end.point) +
  117. (Math.PI / 2) * (bend > 0 ? 0.98 : -0.98)
  118. endAngle = CE
  119. } else {
  120. body = (
  121. <polyline
  122. points={[start.point, end.point].join(' ')}
  123. strokeLinecap="round"
  124. />
  125. )
  126. endAngle = 0
  127. }
  128. // Arrowhead
  129. const length = Math.min(arrowDist / 2, 16 + +style.strokeWidth * 2)
  130. const u = vec.uni(vec.vec(start.point, end.point))
  131. const v = vec.rot(vec.mul(vec.neg(u), length), endAngle)
  132. const b = vec.add(end.point, vec.rot(v, Math.PI / 6))
  133. const c = vec.add(end.point, vec.rot(v, -(Math.PI / 6)))
  134. return (
  135. <g id={id}>
  136. {body}
  137. <circle
  138. cx={start.point[0]}
  139. cy={start.point[1]}
  140. r={+style.strokeWidth}
  141. fill={style.stroke}
  142. strokeDasharray="none"
  143. />
  144. <polyline
  145. points={[b, end.point, c].join()}
  146. strokeLinecap="round"
  147. strokeLinejoin="round"
  148. fill="none"
  149. strokeDasharray="none"
  150. />
  151. </g>
  152. )
  153. },
  154. rotateTo(shape, rotation, delta) {
  155. const { start, end, bend } = shape.handles
  156. // const mp = vec.med(start.point, end.point)
  157. // start.point = vec.rotWith(start.point, mp, delta)
  158. // end.point = vec.rotWith(end.point, mp, delta)
  159. // bend.point = vec.rotWith(bend.point, mp, delta)
  160. // this.onHandleChange(shape, shape.handles)
  161. // const bounds = this.getBounds(shape)
  162. // const offset = vec.sub([bounds.minX, bounds.minY], shape.point)
  163. // this.translateTo(shape, vec.add(shape.point, offset))
  164. // start.point = vec.sub(start.point, offset)
  165. // end.point = vec.sub(end.point, offset)
  166. // bend.point = vec.sub(bend.point, offset)
  167. shape.rotation = rotation
  168. return this
  169. },
  170. getBounds(shape) {
  171. if (!this.boundsCache.has(shape)) {
  172. const { start, end } = shape.handles
  173. this.boundsCache.set(shape, getBoundsFromPoints([start.point, end.point]))
  174. }
  175. return translateBounds(this.boundsCache.get(shape), shape.point)
  176. },
  177. getRotatedBounds(shape) {
  178. if (!this.boundsCache.has(shape)) {
  179. this.boundsCache.set(shape, getBoundsFromPoints(shape.points))
  180. }
  181. return translateBounds(this.boundsCache.get(shape), shape.point)
  182. },
  183. hitTest(shape, point) {
  184. const { start, end, bend } = shape.handles
  185. if (shape.bend === 0) {
  186. return (
  187. vec.distanceToLineSegment(
  188. start.point,
  189. end.point,
  190. vec.sub(point, shape.point)
  191. ) < 4
  192. )
  193. }
  194. const [cx, cy, r] = getCtp(shape)
  195. return !pointInCircle(point, vec.add(shape.point, [cx, cy]), r - 4)
  196. },
  197. hitTestBounds(this, shape, brushBounds) {
  198. const { start, end, bend } = shape.handles
  199. const sp = vec.add(shape.point, start.point)
  200. const ep = vec.add(shape.point, end.point)
  201. if (pointInBounds(sp, brushBounds) || pointInBounds(ep, brushBounds)) {
  202. return true
  203. }
  204. if (vec.isEqual(vec.med(start.point, end.point), bend.point)) {
  205. return intersectLineSegmentBounds(sp, ep, brushBounds).length > 0
  206. } else {
  207. const [cx, cy, r] = getCtp(shape)
  208. const cp = vec.add(shape.point, [cx, cy])
  209. return intersectArcBounds(sp, ep, cp, r, brushBounds).length > 0
  210. }
  211. },
  212. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  213. const initialShapeBounds = this.getBounds(initialShape)
  214. shape.point = [bounds.minX, bounds.minY]
  215. shape.points = shape.points.map((_, i) => {
  216. const [x, y] = initialShape.points[i]
  217. let nw = x / initialShapeBounds.width
  218. let nh = y / initialShapeBounds.height
  219. if (i === 1) {
  220. let [x0, y0] = initialShape.points[0]
  221. if (x0 === x) nw = 1
  222. if (y0 === y) nh = 1
  223. }
  224. return [
  225. bounds.width * (scaleX < 0 ? 1 - nw : nw),
  226. bounds.height * (scaleY < 0 ? 1 - nh : nh),
  227. ]
  228. })
  229. const { start, end, bend } = shape.handles
  230. start.point = shape.points[0]
  231. end.point = shape.points[1]
  232. bend.point = getBendPoint(shape)
  233. shape.points = [shape.handles.start.point, shape.handles.end.point]
  234. return this
  235. },
  236. onHandleChange(shape, handles) {
  237. for (let id in handles) {
  238. const handle = handles[id]
  239. shape.handles[handle.id] = handle
  240. if (handle.index < 2) {
  241. shape.points[handle.index] = handle.point
  242. }
  243. const { start, end, bend } = shape.handles
  244. const dist = vec.dist(start.point, end.point)
  245. if (handle.id === 'bend') {
  246. const midPoint = vec.med(start.point, end.point)
  247. const u = vec.uni(vec.vec(start.point, end.point))
  248. const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
  249. const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
  250. bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
  251. shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
  252. const sa = vec.angle(end.point, start.point)
  253. const la = sa - Math.PI / 2
  254. if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
  255. shape.bend *= -1
  256. }
  257. }
  258. }
  259. shape.handles.bend.point = getBendPoint(shape)
  260. return this
  261. },
  262. applyStyles(shape, style) {
  263. Object.assign(shape.style, style)
  264. shape.style.isFilled = false
  265. return this
  266. },
  267. canStyleFill: false,
  268. })
  269. export default arrow
  270. function getArrowArcPath(
  271. start: ShapeHandle,
  272. end: ShapeHandle,
  273. circle: number[],
  274. bend: number
  275. ) {
  276. return [
  277. 'M',
  278. start.point[0],
  279. start.point[1],
  280. 'A',
  281. circle[2],
  282. circle[2],
  283. 0,
  284. 0,
  285. bend < 0 ? 0 : 1,
  286. end.point[0],
  287. end.point[1],
  288. ].join(' ')
  289. }
  290. function getBendPoint(shape: ArrowShape) {
  291. const { start, end } = shape.handles
  292. const dist = vec.dist(start.point, end.point)
  293. const midPoint = vec.med(start.point, end.point)
  294. const bendDist = (dist / 2) * shape.bend * Math.min(1, dist / 128)
  295. const u = vec.uni(vec.vec(start.point, end.point))
  296. return Math.abs(bendDist) < 10
  297. ? midPoint
  298. : vec.add(midPoint, vec.mul(vec.per(u), bendDist))
  299. }