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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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, points, handles } = shape
  90. const { start, end, bend: _bend } = handles
  91. const arrowDist = vec.dist(start.point, end.point)
  92. const bendDist = arrowDist * bend
  93. const showCircle = Math.abs(bendDist) > 20
  94. const style = getShapeStyle(shape.style)
  95. // Arrowhead
  96. const length = Math.min(arrowDist / 2, 16 + +style.strokeWidth * 2)
  97. const angle = showCircle ? bend * (Math.PI * 0.48) : 0
  98. const u = vec.uni(vec.vec(start.point, end.point))
  99. const v = vec.rot(vec.mul(vec.neg(u), length), angle)
  100. const b = vec.add(points[1], vec.rot(v, Math.PI / 6))
  101. const c = vec.add(points[1], vec.rot(v, -(Math.PI / 6)))
  102. if (showCircle && !ctpCache.has(handles)) {
  103. ctpCache.set(
  104. handles,
  105. circleFromThreePoints(start.point, end.point, _bend.point)
  106. )
  107. }
  108. const circle = showCircle && getCtp(shape)
  109. return (
  110. <g id={id}>
  111. {circle ? (
  112. <>
  113. <path
  114. d={getArrowArcPath(start, end, circle, bend)}
  115. fill="none"
  116. strokeLinecap="round"
  117. />
  118. </>
  119. ) : (
  120. <polyline
  121. points={[start.point, end.point].join(' ')}
  122. strokeLinecap="round"
  123. />
  124. )}
  125. <circle
  126. cx={start.point[0]}
  127. cy={start.point[1]}
  128. r={+style.strokeWidth}
  129. fill={style.stroke}
  130. strokeDasharray="none"
  131. />
  132. <polyline
  133. points={[b, points[1], c].join()}
  134. strokeLinecap="round"
  135. strokeLinejoin="round"
  136. fill="none"
  137. strokeDasharray="none"
  138. />
  139. </g>
  140. )
  141. },
  142. applyStyles(shape, style) {
  143. Object.assign(shape.style, style)
  144. shape.style.isFilled = false
  145. return this
  146. },
  147. getBounds(shape) {
  148. if (!this.boundsCache.has(shape)) {
  149. this.boundsCache.set(shape, getBoundsFromPoints(shape.points))
  150. }
  151. return translateBounds(this.boundsCache.get(shape), shape.point)
  152. },
  153. getRotatedBounds(shape) {
  154. return this.getBounds(shape)
  155. },
  156. getCenter(shape) {
  157. const bounds = this.getBounds(shape)
  158. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  159. },
  160. hitTest(shape, point) {
  161. const { start, end, bend } = shape.handles
  162. if (shape.bend === 0) {
  163. return (
  164. vec.distanceToLineSegment(
  165. start.point,
  166. end.point,
  167. vec.sub(point, shape.point)
  168. ) < 4
  169. )
  170. }
  171. const [cx, cy, r] = getCtp(shape)
  172. return !pointInCircle(point, vec.add(shape.point, [cx, cy]), r - 4)
  173. },
  174. hitTestBounds(this, shape, brushBounds) {
  175. const { start, end, bend } = shape.handles
  176. const sp = vec.add(shape.point, start.point)
  177. const ep = vec.add(shape.point, end.point)
  178. if (pointInBounds(sp, brushBounds) || pointInBounds(ep, brushBounds)) {
  179. return true
  180. }
  181. if (vec.isEqual(vec.med(start.point, end.point), bend.point)) {
  182. return intersectLineSegmentBounds(sp, ep, brushBounds).length > 0
  183. } else {
  184. const [cx, cy, r] = getCtp(shape)
  185. const cp = vec.add(shape.point, [cx, cy])
  186. return intersectArcBounds(sp, ep, cp, r, brushBounds).length > 0
  187. }
  188. },
  189. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  190. const initialShapeBounds = this.getBounds(initialShape)
  191. shape.point = [bounds.minX, bounds.minY]
  192. shape.points = shape.points.map((_, i) => {
  193. const [x, y] = initialShape.points[i]
  194. let nw = x / initialShapeBounds.width
  195. let nh = y / initialShapeBounds.height
  196. if (i === 1) {
  197. let [x0, y0] = initialShape.points[0]
  198. if (x0 === x) nw = 1
  199. if (y0 === y) nh = 1
  200. }
  201. return [
  202. bounds.width * (scaleX < 0 ? 1 - nw : nw),
  203. bounds.height * (scaleY < 0 ? 1 - nh : nh),
  204. ]
  205. })
  206. const { start, end, bend } = shape.handles
  207. start.point = shape.points[0]
  208. end.point = shape.points[1]
  209. bend.point = getBendPoint(shape)
  210. shape.points = [shape.handles.start.point, shape.handles.end.point]
  211. return this
  212. },
  213. transformSingle(shape, bounds, info) {
  214. this.transform(shape, bounds, info)
  215. return this
  216. },
  217. setProperty(shape, prop, value) {
  218. shape[prop] = value
  219. return this
  220. },
  221. onHandleMove(shape, handles) {
  222. for (let id in handles) {
  223. const handle = handles[id]
  224. shape.handles[handle.id] = handle
  225. if (handle.index < 2) {
  226. shape.points[handle.index] = handle.point
  227. }
  228. const { start, end, bend } = shape.handles
  229. const dist = vec.dist(start.point, end.point)
  230. if (handle.id === 'bend') {
  231. const midPoint = vec.med(start.point, end.point)
  232. const u = vec.uni(vec.vec(start.point, end.point))
  233. const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
  234. const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
  235. bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
  236. shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
  237. const sa = vec.angle(end.point, start.point)
  238. const la = sa - Math.PI / 2
  239. if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
  240. shape.bend *= -1
  241. }
  242. }
  243. }
  244. shape.handles.bend.point = getBendPoint(shape)
  245. return this
  246. },
  247. canTransform: true,
  248. canChangeAspectRatio: true,
  249. canStyleFill: false,
  250. })
  251. export default arrow
  252. function getArrowArcPath(
  253. start: ShapeHandle,
  254. end: ShapeHandle,
  255. circle: number[],
  256. bend: number
  257. ) {
  258. return [
  259. 'M',
  260. start.point[0],
  261. start.point[1],
  262. 'A',
  263. circle[2],
  264. circle[2],
  265. 0,
  266. 0,
  267. bend < 0 ? 0 : 1,
  268. end.point[0],
  269. end.point[1],
  270. ].join(' ')
  271. }
  272. function getBendPoint(shape: ArrowShape) {
  273. const { start, end, bend } = shape.handles
  274. const dist = vec.dist(start.point, end.point)
  275. const midPoint = vec.med(start.point, end.point)
  276. const bendDist = (dist / 2) * shape.bend
  277. const u = vec.uni(vec.vec(start.point, end.point))
  278. return Math.abs(bendDist) < 10
  279. ? midPoint
  280. : vec.add(midPoint, vec.mul(vec.per(u), bendDist))
  281. }