index.ts 20 KB

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