index.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /**
  2. * 一行谱动画
  3. */
  4. import { watch, ref, Ref, h, render } from "vue"
  5. import { getAudioCurrentTime } from "/src/view/audio-list"
  6. import state from "/src/state"
  7. import "./index.less"
  8. import Bird from "./bird"
  9. type pointsPosType = { x: number; y: number; MeasureNumberXML: number }[]
  10. type smoothAnimationType = {
  11. isShow: Ref<boolean>
  12. canvasDom: null | HTMLCanvasElement
  13. canvasCtx: null | undefined | CanvasRenderingContext2D
  14. canvasDomWith: number
  15. canvasDomHeight: number
  16. smoothAnimationBoxDom: null | HTMLElement
  17. smoothBotDom: null | HTMLElement
  18. osmdCanvasPageDom: null | HTMLElement
  19. osdmScrollDom: null | HTMLElement
  20. osdmScrollDomWith: number
  21. osdmScrollDomOffsetLeft: number
  22. selectionBoxDom: null | HTMLElement
  23. pointsPos: pointsPosType
  24. translateXNum: number
  25. aveSpeed: number
  26. }
  27. const _numberOfSegments = 58 // 中间切割线的个数
  28. export const smoothAnimationState = {
  29. isShow: ref(false), // 是否显示
  30. canvasDom: null,
  31. canvasCtx: null,
  32. canvasDomWith: 0,
  33. canvasDomHeight: 80,
  34. smoothAnimationBoxDom: null,
  35. smoothBotDom: null,
  36. osmdCanvasPageDom: null,
  37. osdmScrollDom: null,
  38. osdmScrollDomWith: 0,
  39. osdmScrollDomOffsetLeft: 0,
  40. selectionBoxDom: null,
  41. pointsPos: [], // 计算之后的点坐标数组
  42. translateXNum: 0, // 当前谱面的translateX的距离 谱面的位置信息 由translateX和scrollLeft的偏移一起决定
  43. aveSpeed: 0 // 谱面的一帧的平均速度
  44. } as smoothAnimationType
  45. // 监听显示与隐藏
  46. watch(smoothAnimationState.isShow, () => {
  47. if (smoothAnimationState.isShow.value) {
  48. smoothAnimationState.smoothAnimationBoxDom?.classList.remove("smoothAnimationBoxHide")
  49. } else {
  50. smoothAnimationState.smoothAnimationBoxDom?.classList.add("smoothAnimationBoxHide")
  51. }
  52. })
  53. /**
  54. * 初始化
  55. */
  56. export function initSmoothAnimation() {
  57. // 创建dom
  58. createSmoothAnimation()
  59. // 初始化动画数据
  60. const batePos = getPointsPosByBatePos()
  61. console.log(batePos, "batePos")
  62. smoothAnimationState.pointsPos = createSmoothCurvePoints(batePos, undefined, undefined, _numberOfSegments)
  63. // 谱面的平均速度(因为可能有反复的情况所以实际距离要加上反复的距离)
  64. const canvasDomPath = batePos.reduce((path, item, index, arr) => {
  65. if (index !== 0) {
  66. if (Math.abs(item.MeasureNumberXML - arr[index - 1].MeasureNumberXML) <= 1) {
  67. path += item.x - arr[index - 1].x
  68. }
  69. }
  70. return path
  71. }, 0)
  72. // 20 是屏幕多长时间刷新一次的时间,本来是16.67的,但是有些手机帧率比较低,所以这里给小一点的值,宁愿慢一点偏屏幕左边一点
  73. smoothAnimationState.aveSpeed = (canvasDomPath / (state.times[state.times.length - 1].time - state.times[0].time) / 1000) * 20
  74. // 当前屏幕的宽度
  75. calcClientWidth()
  76. window.addEventListener("resize", calcClientWidth)
  77. // 初始化 只有练习模式 才显示
  78. state.modeType === "practise" && (smoothAnimationState.isShow.value = state.melodyLine)
  79. // 多分轨合并显示、打击乐、节奏练习的曲子不显示旋律线
  80. if (state.isCombineRender || state.isPercussion) {
  81. smoothAnimationState.isShow.value = false
  82. }
  83. console.log(smoothAnimationState, "一行谱小鸟数据")
  84. }
  85. /**
  86. * 销毁
  87. */
  88. export function destroySmoothAnimation() {
  89. smoothAnimationState.isShow.value = false
  90. window.removeEventListener("resize", calcClientWidth)
  91. smoothAnimationState.smoothAnimationBoxDom?.remove()
  92. Object.assign(smoothAnimationState, {
  93. canvasDom: null,
  94. canvasCtx: null,
  95. canvasDomWith: 0,
  96. canvasDomHeight: 80,
  97. smoothAnimationBoxDom: null,
  98. smoothBotDom: null,
  99. osmdCanvasPageDom: null,
  100. osdmScrollDom: null,
  101. osdmScrollDomWith: 0,
  102. osdmScrollDomOffsetLeft: 0,
  103. selectionBoxDom: null,
  104. pointsPos: [],
  105. translateXNum: 0,
  106. aveSpeed: 0
  107. })
  108. }
  109. /**
  110. * 根据播放时间进度移动处理
  111. */
  112. export function moveSmoothAnimationByPlayTime(time?: number) {
  113. // 暂停之后不进行移动了
  114. if (state.playState === "paused") {
  115. return
  116. }
  117. const currentTime = time || getAudioCurrentTime()
  118. if (currentTime <= state.fixtime) return
  119. if (currentTime > state.times.last()?.endtime) return
  120. // 当休止小节,可能当前音符在谱面上没有实际的音符(没有bbox),所以往后找谱面上有的音符
  121. let nextIndex = state.activeNoteIndex + 1
  122. let nextBBox = state.times[nextIndex]?.bbox
  123. while (!nextBBox && nextIndex < state.times.length) {
  124. nextIndex += 1
  125. nextBBox = state.times[nextIndex]?.bbox
  126. }
  127. // 当前的音符和下一个音符之间的时值 (当是最后一个音符的时候,下一个音符的时间取当前音符的endtime)
  128. const noteDuration =
  129. (nextIndex > state.times.length - 1 ? state.times[state.activeNoteIndex]?.endtime : state.times[nextIndex].time) -
  130. state.times[state.activeNoteIndex]?.time
  131. // 妙极客曲目 有可能2个音符时值一样
  132. if (noteDuration <= 0) {
  133. return
  134. }
  135. // 当前时值在该区间的占比
  136. let playProgress = (currentTime - state.times[state.activeNoteIndex]?.time) / noteDuration
  137. // 华为手机 fixtime 有个默认0.08的值,所以进度可能为负数 ,这里兼容一下
  138. playProgress < 0 && (playProgress = 0)
  139. moveSmoothAnimation(playProgress, state.activeNoteIndex)
  140. }
  141. /**
  142. * 移动处理
  143. * progress 当前音符到下一个音符的距离百分比
  144. * activeIndex 当前
  145. */
  146. export function moveSmoothAnimation(progress: number, activeIndex: number, isMoveOsmd = true) {
  147. // if (!smoothAnimationState.isShow.value) {
  148. // return
  149. // }
  150. // 计算 下一个音符index 在pointsPos 中的距离
  151. const nextPointsIndex = (activeIndex + 1) * (_numberOfSegments + 1) - 1
  152. // 百分比转为当前的index 距离个数
  153. const progressCalcIndex = Math.round(progress * _numberOfSegments)
  154. // // 当前的index
  155. let nowIndex = nextPointsIndex - _numberOfSegments + progressCalcIndex
  156. const nowPointsPos = smoothAnimationState.pointsPos[nowIndex]
  157. smoothAnimationState.canvasCtx?.clearRect(0, 0, smoothAnimationState.canvasDomWith, smoothAnimationState.canvasDomHeight)
  158. // 移动
  159. smoothAnimationMove(
  160. {
  161. x: nowPointsPos.x - 18,
  162. y: nowPointsPos.y - 23
  163. },
  164. smoothAnimationState.pointsPos,
  165. smoothAnimationState.pointsPos.slice(0, nowIndex)
  166. )
  167. // 当移动到屏幕最右边时候 就不进行移动了 存在移动到屏幕最右边时候 有反复的情况需要屏幕移动。所以这里注释掉了
  168. // if (
  169. // (smoothAnimationState.osdmScrollDom?.scrollLeft || 0) + smoothAnimationState.translateXNum + smoothAnimationState.osdmScrollDomWith >=
  170. // smoothAnimationState.canvasDomWith
  171. // ) {
  172. // return
  173. // }
  174. isMoveOsmd && move_osmd(nowPointsPos)
  175. }
  176. /**
  177. * 谱面移动逻辑
  178. */
  179. function move_osmd(nowPointsPos: pointsPosType[0]) {
  180. // 评测移动太快看不到前面小节的分数,评测改成0.5倍速移动谱面
  181. const speed = (state.modeType === 'evaluating' ? smoothAnimationState.aveSpeed * 0.5 : smoothAnimationState.aveSpeed) * (state.speed / 60)
  182. // 视口宽度
  183. const clientWidth = smoothAnimationState.osdmScrollDomWith
  184. const clientMidWidth = clientWidth / 2
  185. let { left, right, width } = smoothAnimationState.smoothBotDom!.getBoundingClientRect()
  186. left -= smoothAnimationState.osdmScrollDomOffsetLeft
  187. right -= smoothAnimationState.osdmScrollDomOffsetLeft
  188. const midBotNum = left + width / 2
  189. // 分阶段移动
  190. if (right > clientWidth) {
  191. // 移动超过屏幕时候
  192. smoothAnimationState.translateXNum = 0
  193. smoothAnimationState.osdmScrollDom!.scrollLeft = nowPointsPos.x - clientWidth * 0.9
  194. } else if (left < 0) {
  195. // 移动小于屏幕时候
  196. smoothAnimationState.translateXNum = 0
  197. smoothAnimationState.osdmScrollDom!.scrollLeft = nowPointsPos.x - clientWidth * 0.1
  198. } else if (midBotNum > clientMidWidth - clientWidth * 0.3 && midBotNum <= clientMidWidth - clientWidth * 0.25) {
  199. smoothAnimationState.translateXNum += speed * 0.5
  200. } else if (midBotNum > clientMidWidth - clientWidth * 0.25 && midBotNum <= clientMidWidth - clientWidth * 0.2) {
  201. smoothAnimationState.translateXNum += speed * 0.6
  202. } else if (midBotNum > clientMidWidth - clientWidth * 0.2 && midBotNum <= clientMidWidth - clientWidth * 0.15) {
  203. smoothAnimationState.translateXNum += speed * 0.7
  204. } else if (midBotNum > clientMidWidth - clientWidth * 0.15 && midBotNum <= clientMidWidth - clientWidth * 0.1) {
  205. smoothAnimationState.translateXNum += speed * 0.8
  206. } else if (midBotNum > clientMidWidth - clientWidth * 0.1 && midBotNum <= clientMidWidth - clientWidth * 0.05) {
  207. smoothAnimationState.translateXNum += speed * 0.9
  208. } else if (midBotNum > clientMidWidth - clientWidth * 0.05 && midBotNum <= clientMidWidth) {
  209. smoothAnimationState.translateXNum += speed
  210. } else if (midBotNum > clientMidWidth && midBotNum <= clientMidWidth + clientWidth * 0.05) {
  211. smoothAnimationState.translateXNum += speed * 1.2
  212. } else if (midBotNum > clientMidWidth + clientWidth * 0.05 && midBotNum <= clientMidWidth + clientWidth * 0.1) {
  213. smoothAnimationState.translateXNum += speed * 1.4
  214. } else if (midBotNum > clientMidWidth + clientWidth * 0.1 && midBotNum <= clientMidWidth + clientWidth * 0.15) {
  215. smoothAnimationState.translateXNum += speed * 1.7
  216. } else if (midBotNum > clientMidWidth + clientWidth * 0.15 && midBotNum <= clientMidWidth + clientWidth * 0.2) {
  217. smoothAnimationState.translateXNum += speed * 2
  218. } else if (midBotNum > clientMidWidth + clientWidth * 0.2 && midBotNum <= clientMidWidth + clientWidth * 0.25) {
  219. smoothAnimationState.translateXNum += speed * 2.4
  220. } else if (midBotNum > clientMidWidth + clientWidth * 0.25 && midBotNum <= clientMidWidth + clientWidth * 0.3) {
  221. smoothAnimationState.translateXNum += speed * 2.8
  222. } else if (midBotNum > clientMidWidth + clientWidth * 0.3 && midBotNum <= clientMidWidth + clientWidth * 0.35) {
  223. smoothAnimationState.translateXNum += speed * 3.3
  224. } else if (midBotNum > clientMidWidth + clientWidth * 0.35 && midBotNum <= clientMidWidth + clientWidth * 0.4) {
  225. smoothAnimationState.translateXNum += speed * 3.8
  226. } else if (midBotNum > clientMidWidth + clientWidth * 0.4 && midBotNum <= clientMidWidth + clientWidth * 0.45) {
  227. smoothAnimationState.translateXNum += speed * 4.4
  228. } else if (midBotNum > clientMidWidth + clientWidth * 0.45 && midBotNum <= clientMidWidth + clientWidth * 0.5) {
  229. smoothAnimationState.translateXNum += speed * 5
  230. }
  231. // 最多移动的位置
  232. const osdmScrollDomScrollLeft = smoothAnimationState.osdmScrollDom?.scrollLeft || 0
  233. const maxTranslateXNum = smoothAnimationState.canvasDomWith - smoothAnimationState.osdmScrollDomWith - osdmScrollDomScrollLeft
  234. if (smoothAnimationState.translateXNum > maxTranslateXNum) {
  235. smoothAnimationState.translateXNum = maxTranslateXNum
  236. }
  237. moveTranslateXNum(smoothAnimationState.translateXNum)
  238. }
  239. /**
  240. * 根据 translateXNum 滚动到位置
  241. */
  242. export function moveTranslateXNum(translateXNum: number) {
  243. smoothAnimationState.osmdCanvasPageDom && (smoothAnimationState.osmdCanvasPageDom.style.transform = `translateX(-${translateXNum}px)`)
  244. smoothAnimationState.selectionBoxDom && (smoothAnimationState.selectionBoxDom.style.transform = `translateX(-${translateXNum}px)`)
  245. }
  246. /**
  247. * 进度条和块移动方法
  248. */
  249. function smoothAnimationMove(pos: { x: number; y: number }, pointsPos: pointsPosType, progresspointsPos?: pointsPosType) {
  250. smoothAnimationState.smoothBotDom && (smoothAnimationState.smoothBotDom.style.transform = `translate(${pos.x}px, ${pos.y}px)`)
  251. smoothAnimationState.canvasCtx && drawSmoothCurve(smoothAnimationState.canvasCtx, pointsPos, progresspointsPos)
  252. }
  253. /**
  254. * 计算视口宽度
  255. */
  256. export function calcClientWidth() {
  257. smoothAnimationState.osdmScrollDomWith = smoothAnimationState.osdmScrollDom?.offsetWidth || 0
  258. smoothAnimationState.osdmScrollDomOffsetLeft = smoothAnimationState.osdmScrollDom?.getBoundingClientRect().left || 0
  259. }
  260. /**
  261. * 创建dom
  262. */
  263. function createSmoothAnimation() {
  264. // osdmScrollDom
  265. const osdmScrollDom = document.querySelector("#musicAndSelection") as HTMLElement
  266. smoothAnimationState.osdmScrollDom = osdmScrollDom
  267. // osmdCanvasPage
  268. const osmdCanvasPageDom = document.querySelector("#osmdCanvasPage1") as HTMLElement
  269. smoothAnimationState.osmdCanvasPageDom = osmdCanvasPageDom
  270. // selectionBox
  271. setTimeout(() => {
  272. const selectionBoxDom = document.querySelector("#selectionBox") as HTMLElement
  273. smoothAnimationState.selectionBoxDom = selectionBoxDom
  274. }, 0)
  275. // box
  276. const smoothAnimationBoxDom = document.createElement("div")
  277. smoothAnimationBoxDom.className = "smoothAnimationBox smoothAnimationBoxHide"
  278. smoothAnimationState.smoothAnimationBoxDom = smoothAnimationBoxDom
  279. // con
  280. const smoothAnimationConDom = document.createElement("div")
  281. smoothAnimationConDom.className = "smoothAnimationCon"
  282. //canvas
  283. const smoothCanvasDom = document.createElement("canvas")
  284. smoothCanvasDom.className = "smoothCanvas"
  285. smoothAnimationState.canvasDom = smoothCanvasDom
  286. smoothAnimationState.canvasDomWith = osmdCanvasPageDom?.offsetWidth || 0
  287. smoothCanvasDom.width = smoothAnimationState.canvasDomWith
  288. smoothCanvasDom.height = smoothAnimationState.canvasDomHeight
  289. smoothAnimationState.canvasCtx = smoothCanvasDom.getContext("2d")
  290. // bot
  291. const smoothBotDom = document.createElement("div")
  292. smoothBotDom.className = "smoothBot"
  293. smoothAnimationState.smoothBotDom = smoothBotDom
  294. // 添加小鸟
  295. render(h(Bird), smoothBotDom)
  296. smoothAnimationConDom.appendChild(smoothCanvasDom)
  297. smoothAnimationConDom.appendChild(smoothBotDom)
  298. smoothAnimationBoxDom.appendChild(smoothAnimationConDom)
  299. // 添加到 osmdCanvasPage1
  300. osmdCanvasPageDom?.insertBefore(smoothAnimationBoxDom, osmdCanvasPageDom.firstChild)
  301. }
  302. /**
  303. * 根据音符获取坐标
  304. */
  305. function getPointsPosByBatePos(): pointsPosType {
  306. let totalAvInde = 0
  307. // 取平均值
  308. const totalAv =
  309. state.times.reduce((total, item) => {
  310. if (item.frequency !== -1) {
  311. // -1 为休止符
  312. total += item.frequency
  313. totalAvInde++
  314. }
  315. return total
  316. }, 0) / totalAvInde
  317. const pointsPos = state.times.reduce((posArr: any[], item) => {
  318. // 当休止小节,可能当前音符在谱面上没有实际的音符(没有bbox),所以往后找谱面上有的音符
  319. if (item.bbox) {
  320. posArr.push({
  321. MeasureNumberXML: item.MeasureNumberXML,
  322. x: item.bbox.x,
  323. // 当为休止符的时候 取最下面的位置*0.9,确保能显示完整
  324. y:
  325. smoothAnimationState.canvasDomHeight / 2 -
  326. ((((item.frequency === -1 ? 2 * totalAv * 0.1 : item.frequency) - totalAv) / totalAv) * smoothAnimationState.canvasDomHeight) / 2
  327. })
  328. }
  329. return posArr
  330. }, [])
  331. // 最后一个音符延长(这里建立一个虚拟的音符延长)
  332. const extendPoint = {
  333. ...pointsPos[pointsPos.length - 1]
  334. }
  335. extendPoint.MeasureNumberXML++
  336. // 当总长度减30小于最后一个音符时候,取最后一个音符加15
  337. extendPoint.x = smoothAnimationState.canvasDomWith - 30 > extendPoint.x ? smoothAnimationState.canvasDomWith - 30 : extendPoint.x + 15
  338. pointsPos.push(extendPoint)
  339. return pointsPos
  340. }
  341. /**
  342. * 使用传入的曲线的顶点坐标创建平滑曲线的顶点。
  343. * @param {Array} points 曲线顶点坐标数组,
  344. * @param {Float} tension 密集程度,默认为 0.5
  345. * @param {Boolean} closed 是否创建闭合曲线,默认为 false
  346. * @param {Int} numberOfSegments 平滑曲线 2 个顶点间的线段数,默认为 20
  347. * @return {Array} 平滑曲线的顶点坐标数组
  348. */
  349. function createSmoothCurvePoints(pointsPos: pointsPosType, tension?: number, closed?: boolean, numberOfSegments?: number) {
  350. if (pointsPos.length <= 2) {
  351. return pointsPos
  352. }
  353. tension = tension ? tension : 0.5
  354. closed = closed ? true : false
  355. numberOfSegments = numberOfSegments ? numberOfSegments : 20
  356. let ps = pointsPos.slice(0),
  357. result = [],
  358. x,
  359. y,
  360. t1x,
  361. t2x,
  362. t1y,
  363. t2y,
  364. c1,
  365. c2,
  366. c3,
  367. c4,
  368. st,
  369. t,
  370. i
  371. if (closed) {
  372. ps.unshift(pointsPos[pointsPos.length - 1])
  373. ps.unshift(pointsPos[pointsPos.length - 1])
  374. ps.push(pointsPos[0])
  375. } else {
  376. ps.unshift(pointsPos[0])
  377. ps.push(pointsPos[pointsPos.length - 1])
  378. }
  379. for (i = 1; i < ps.length - 2; i += 1) {
  380. //console.log(ps[i + 1].MeasureNumberXML, ps[i - 1].MeasureNumberXML, ps[i + 2].MeasureNumberXML, ps[i].MeasureNumberXML)
  381. t1x = (ps[i + 1].x - ps[i - 1].x) * tension
  382. t2x = (ps[i + 2].x - ps[i].x) * tension
  383. t1y = (ps[i + 1].y - ps[i - 1].y) * tension
  384. t2y = (ps[i + 2].y - ps[i].y) * tension
  385. // 当中途出现反复 刚开始反复时候 53 52 22 52 (22)中途值会变小 这里强行拉大 防止算法平均值出现很大偏差
  386. if (ps[i + 1].MeasureNumberXML - ps[i + 2].MeasureNumberXML > 1) {
  387. const nowNumberXML = ps[i + 1].MeasureNumberXML + 1
  388. //在当前值的情况下 向前一位
  389. let index = ps.findIndex(item => {
  390. return nowNumberXML === item.MeasureNumberXML
  391. })
  392. // 查询不到index时候取当前值
  393. index === -1 && (index = i + 1)
  394. t2x = (ps[index].x - ps[i].x) * tension
  395. }
  396. // 当中途出现反复 结束反复时候 22 53 22 22 (53)中途值会变大 这里强行缩小 防止算法平均值出现很大偏差
  397. if (ps[i - 1].MeasureNumberXML - ps[i].MeasureNumberXML > 1) {
  398. //在当前值的情况下 向后一位
  399. const nowNumberXML = ps[i].MeasureNumberXML - 1
  400. let index = ps.findIndex((item, index) => {
  401. return nowNumberXML === item.MeasureNumberXML && nowNumberXML !== ps[index + 1]?.MeasureNumberXML
  402. })
  403. // 查询不到index时候取当前值
  404. index === -1 && (index = i)
  405. t1x = (ps[i + 1].x - ps[index].x) * tension
  406. }
  407. // 当中途出现跳房子 刚开始跳房子时候 35 35 54 35 (54)中途值会变大 这里强行缩小 防止算法平均值出现很大偏差
  408. if (ps[i + 1].MeasureNumberXML - ps[i + 2].MeasureNumberXML < -1) {
  409. const nowNumberXML = ps[i + 1].MeasureNumberXML + 1
  410. //在当前值的情况下 向前一位
  411. let index = ps.findIndex(item => {
  412. return nowNumberXML === item.MeasureNumberXML
  413. })
  414. // 查询不到index时候取当前值
  415. index === -1 && (index = i + 1)
  416. t2x = (ps[index].x - ps[i].x) * tension
  417. }
  418. // 当中途出现跳房子 结束跳房子时候 54 35 54 54 (35)中途值会变小 这里强行拉大 防止算法平均值出现很大偏差
  419. if (ps[i - 1].MeasureNumberXML - ps[i].MeasureNumberXML < -1) {
  420. const nowNumberXML = ps[i].MeasureNumberXML - 1
  421. let index = ps.findIndex((item, index) => {
  422. return nowNumberXML === item.MeasureNumberXML && nowNumberXML !== ps[index + 1]?.MeasureNumberXML
  423. })
  424. // 查询不到index时候取当前值
  425. index === -1 && (index = i)
  426. t1x = (ps[i + 1].x - ps[index].x) * tension
  427. }
  428. const nowMeasureNumberXML = pointsPos[i - 1].MeasureNumberXML
  429. const nextMeasureNumberXML = pointsPos[i].MeasureNumberXML
  430. for (t = 0; t <= numberOfSegments; t++) {
  431. // 小于1时候是反复 大于1是跳房子 不画曲线 停留
  432. if (nextMeasureNumberXML - nowMeasureNumberXML < 0 || nextMeasureNumberXML - nowMeasureNumberXML > 1) {
  433. //console.log(x, y)
  434. result.push({
  435. x: x as number,
  436. y: y as number,
  437. MeasureNumberXML: nowMeasureNumberXML
  438. })
  439. continue
  440. }
  441. st = t / numberOfSegments
  442. c1 = 2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1
  443. c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2)
  444. c3 = Math.pow(st, 3) - 2 * Math.pow(st, 2) + st
  445. c4 = Math.pow(st, 3) - Math.pow(st, 2)
  446. x = c1 * ps[i].x + c2 * ps[i + 1].x + c3 * t1x + c4 * t2x
  447. y = c1 * ps[i].y + c2 * ps[i + 1].y + c3 * t1y + c4 * t2y
  448. //console.log(x, y)
  449. result.push({
  450. x,
  451. y,
  452. MeasureNumberXML: t === numberOfSegments ? nextMeasureNumberXML : nowMeasureNumberXML
  453. })
  454. }
  455. }
  456. return result
  457. }
  458. /**
  459. * 根据坐标划线
  460. */
  461. function drawSmoothCurve(context: CanvasRenderingContext2D, pointsPos: pointsPosType, progresspointsPos?: pointsPosType) {
  462. context.lineWidth = 2
  463. context.lineJoin = "round" // 优化锯齿
  464. context.lineCap = "round" // 优化锯齿
  465. context.strokeStyle = "rgba(255,255,255,0.6)"
  466. drawLines(context, pointsPos)
  467. if (progresspointsPos?.length) {
  468. context.strokeStyle = "#FFC121"
  469. drawLines(context, progresspointsPos)
  470. }
  471. }
  472. function drawLines(context: CanvasRenderingContext2D, points: pointsPosType) {
  473. context.beginPath()
  474. context.moveTo(points[0].x, points[0].y)
  475. for (let i = 1; i < points.length - 1; i++) {
  476. if (Math.abs(points[i].MeasureNumberXML - points[i - 1].MeasureNumberXML) > 1) {
  477. // 取消反复和跳房子连线
  478. context.stroke()
  479. context.beginPath()
  480. context.moveTo(points[i + 1].x, points[i + 1].y)
  481. continue
  482. }
  483. context.lineTo(points[i].x, points[i].y)
  484. }
  485. context.stroke()
  486. }