選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

arrow.tsx 8.0KB

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