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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. getBounds(shape) {
  143. if (!this.boundsCache.has(shape)) {
  144. this.boundsCache.set(shape, getBoundsFromPoints(shape.points))
  145. }
  146. return translateBounds(this.boundsCache.get(shape), shape.point)
  147. },
  148. hitTest(shape, point) {
  149. const { start, end, bend } = shape.handles
  150. if (shape.bend === 0) {
  151. return (
  152. vec.distanceToLineSegment(
  153. start.point,
  154. end.point,
  155. vec.sub(point, shape.point)
  156. ) < 4
  157. )
  158. }
  159. const [cx, cy, r] = getCtp(shape)
  160. return !pointInCircle(point, vec.add(shape.point, [cx, cy]), r - 4)
  161. },
  162. hitTestBounds(this, shape, brushBounds) {
  163. const { start, end, bend } = shape.handles
  164. const sp = vec.add(shape.point, start.point)
  165. const ep = vec.add(shape.point, end.point)
  166. if (pointInBounds(sp, brushBounds) || pointInBounds(ep, brushBounds)) {
  167. return true
  168. }
  169. if (vec.isEqual(vec.med(start.point, end.point), bend.point)) {
  170. return intersectLineSegmentBounds(sp, ep, brushBounds).length > 0
  171. } else {
  172. const [cx, cy, r] = getCtp(shape)
  173. const cp = vec.add(shape.point, [cx, cy])
  174. return intersectArcBounds(sp, ep, cp, r, brushBounds).length > 0
  175. }
  176. },
  177. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  178. const initialShapeBounds = this.getBounds(initialShape)
  179. shape.point = [bounds.minX, bounds.minY]
  180. shape.points = shape.points.map((_, i) => {
  181. const [x, y] = initialShape.points[i]
  182. let nw = x / initialShapeBounds.width
  183. let nh = y / initialShapeBounds.height
  184. if (i === 1) {
  185. let [x0, y0] = initialShape.points[0]
  186. if (x0 === x) nw = 1
  187. if (y0 === y) nh = 1
  188. }
  189. return [
  190. bounds.width * (scaleX < 0 ? 1 - nw : nw),
  191. bounds.height * (scaleY < 0 ? 1 - nh : nh),
  192. ]
  193. })
  194. const { start, end, bend } = shape.handles
  195. start.point = shape.points[0]
  196. end.point = shape.points[1]
  197. bend.point = getBendPoint(shape)
  198. shape.points = [shape.handles.start.point, shape.handles.end.point]
  199. return this
  200. },
  201. onHandleMove(shape, handles) {
  202. for (let id in handles) {
  203. const handle = handles[id]
  204. shape.handles[handle.id] = handle
  205. if (handle.index < 2) {
  206. shape.points[handle.index] = handle.point
  207. }
  208. const { start, end, bend } = shape.handles
  209. const dist = vec.dist(start.point, end.point)
  210. if (handle.id === 'bend') {
  211. const midPoint = vec.med(start.point, end.point)
  212. const u = vec.uni(vec.vec(start.point, end.point))
  213. const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
  214. const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
  215. bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
  216. shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
  217. const sa = vec.angle(end.point, start.point)
  218. const la = sa - Math.PI / 2
  219. if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
  220. shape.bend *= -1
  221. }
  222. }
  223. }
  224. shape.handles.bend.point = getBendPoint(shape)
  225. return this
  226. },
  227. applyStyles(shape, style) {
  228. Object.assign(shape.style, style)
  229. shape.style.isFilled = false
  230. return this
  231. },
  232. canStyleFill: false,
  233. })
  234. export default arrow
  235. function getArrowArcPath(
  236. start: ShapeHandle,
  237. end: ShapeHandle,
  238. circle: number[],
  239. bend: number
  240. ) {
  241. return [
  242. 'M',
  243. start.point[0],
  244. start.point[1],
  245. 'A',
  246. circle[2],
  247. circle[2],
  248. 0,
  249. 0,
  250. bend < 0 ? 0 : 1,
  251. end.point[0],
  252. end.point[1],
  253. ].join(' ')
  254. }
  255. function getBendPoint(shape: ArrowShape) {
  256. const { start, end, bend } = shape.handles
  257. const dist = vec.dist(start.point, end.point)
  258. const midPoint = vec.med(start.point, end.point)
  259. const bendDist = (dist / 2) * shape.bend
  260. const u = vec.uni(vec.vec(start.point, end.point))
  261. return Math.abs(bendDist) < 10
  262. ? midPoint
  263. : vec.add(midPoint, vec.mul(vec.per(u), bendDist))
  264. }