index.ts 24 KB

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