index.ts 24 KB

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