index.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. osdmScrollDomOffsetWidth: 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. osdmScrollDomOffsetWidth: 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 = true)
  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. osdmScrollDomOffsetWidth: 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. // 当前时值在该区间的占比
  132. let playProgress = (currentTime - state.times[state.activeNoteIndex]?.time) / noteDuration
  133. // 华为手机 fixtime 有个默认0.08的值,所以进度可能为负数 ,这里兼容一下
  134. playProgress < 0 && (playProgress = 0)
  135. moveSmoothAnimation(playProgress, state.activeNoteIndex)
  136. }
  137. /**
  138. * 移动处理
  139. * progress 当前音符到下一个音符的距离百分比
  140. * activeIndex 当前
  141. */
  142. export function moveSmoothAnimation(progress: number, activeIndex: number, isMoveOsmd = true) {
  143. // if (!smoothAnimationState.isShow.value) {
  144. // return
  145. // }
  146. // 计算 下一个音符index 在pointsPos 中的距离
  147. const nextPointsIndex = (activeIndex + 1) * (_numberOfSegments + 1) - 1
  148. // 百分比转为当前的index 距离个数
  149. const progressCalcIndex = Math.round(progress * _numberOfSegments)
  150. // // 当前的index
  151. let nowIndex = nextPointsIndex - _numberOfSegments + progressCalcIndex
  152. const nowPointsPos = smoothAnimationState.pointsPos[nowIndex]
  153. smoothAnimationState.canvasCtx?.clearRect(0, 0, smoothAnimationState.canvasDomWith, smoothAnimationState.canvasDomHeight)
  154. // 移动
  155. smoothAnimationMove(
  156. {
  157. x: nowPointsPos.x - 18,
  158. y: nowPointsPos.y - 23
  159. },
  160. smoothAnimationState.pointsPos,
  161. smoothAnimationState.pointsPos.slice(0, nowIndex)
  162. )
  163. // 当移动到屏幕最右边时候 就不进行移动了 存在移动到屏幕最右边时候 有反复的情况需要屏幕移动。所以这里注释掉了
  164. // if (
  165. // (smoothAnimationState.osdmScrollDom?.scrollLeft || 0) + smoothAnimationState.translateXNum + smoothAnimationState.osdmScrollDomWith >=
  166. // smoothAnimationState.canvasDomWith
  167. // ) {
  168. // return
  169. // }
  170. isMoveOsmd && move_osmd(nowPointsPos)
  171. }
  172. /**
  173. * 谱面移动逻辑
  174. */
  175. function move_osmd(nowPointsPos: pointsPosType[0]) {
  176. const speed = smoothAnimationState.aveSpeed * (state.speed / 60)
  177. // 视口宽度
  178. const clientWidth = smoothAnimationState.osdmScrollDomWith
  179. const clientMidWidth = clientWidth / 2
  180. let { left, right, width } = smoothAnimationState.smoothBotDom!.getBoundingClientRect()
  181. left -= smoothAnimationState.osdmScrollDomOffsetWidth
  182. right -= smoothAnimationState.osdmScrollDomOffsetWidth
  183. const midBotNum = left + width / 2
  184. // 分阶段移动
  185. if (right > clientWidth) {
  186. // 移动超过屏幕时候
  187. smoothAnimationState.translateXNum = 0
  188. smoothAnimationState.osdmScrollDom!.scrollLeft = nowPointsPos.x - clientWidth * 0.9
  189. } else if (left < 0) {
  190. // 移动小于屏幕时候
  191. smoothAnimationState.translateXNum = 0
  192. smoothAnimationState.osdmScrollDom!.scrollLeft = nowPointsPos.x - clientWidth * 0.1
  193. } else if (midBotNum > clientMidWidth - clientWidth * 0.3 && midBotNum <= clientMidWidth - clientWidth * 0.25) {
  194. smoothAnimationState.translateXNum += speed * 0.5
  195. } else if (midBotNum > clientMidWidth - clientWidth * 0.25 && midBotNum <= clientMidWidth - clientWidth * 0.2) {
  196. smoothAnimationState.translateXNum += speed * 0.6
  197. } else if (midBotNum > clientMidWidth - clientWidth * 0.2 && midBotNum <= clientMidWidth - clientWidth * 0.15) {
  198. smoothAnimationState.translateXNum += speed * 0.7
  199. } else if (midBotNum > clientMidWidth - clientWidth * 0.15 && midBotNum <= clientMidWidth - clientWidth * 0.1) {
  200. smoothAnimationState.translateXNum += speed * 0.8
  201. } else if (midBotNum > clientMidWidth - clientWidth * 0.1 && midBotNum <= clientMidWidth - clientWidth * 0.05) {
  202. smoothAnimationState.translateXNum += speed * 0.9
  203. } else if (midBotNum > clientMidWidth - clientWidth * 0.05 && midBotNum <= clientMidWidth) {
  204. smoothAnimationState.translateXNum += speed
  205. } else if (midBotNum > clientMidWidth && midBotNum <= clientMidWidth + clientWidth * 0.05) {
  206. smoothAnimationState.translateXNum += speed * 1.2
  207. } else if (midBotNum > clientMidWidth + clientWidth * 0.05 && midBotNum <= clientMidWidth + clientWidth * 0.1) {
  208. smoothAnimationState.translateXNum += speed * 1.4
  209. } else if (midBotNum > clientMidWidth + clientWidth * 0.1 && midBotNum <= clientMidWidth + clientWidth * 0.15) {
  210. smoothAnimationState.translateXNum += speed * 1.7
  211. } else if (midBotNum > clientMidWidth + clientWidth * 0.15 && midBotNum <= clientMidWidth + clientWidth * 0.2) {
  212. smoothAnimationState.translateXNum += speed * 2
  213. } else if (midBotNum > clientMidWidth + clientWidth * 0.2 && midBotNum <= clientMidWidth + clientWidth * 0.25) {
  214. smoothAnimationState.translateXNum += speed * 2.4
  215. } else if (midBotNum > clientMidWidth + clientWidth * 0.25 && midBotNum <= clientMidWidth + clientWidth * 0.3) {
  216. smoothAnimationState.translateXNum += speed * 2.8
  217. } else if (midBotNum > clientMidWidth + clientWidth * 0.3 && midBotNum <= clientMidWidth + clientWidth * 0.35) {
  218. smoothAnimationState.translateXNum += speed * 3.3
  219. } else if (midBotNum > clientMidWidth + clientWidth * 0.35 && midBotNum <= clientMidWidth + clientWidth * 0.4) {
  220. smoothAnimationState.translateXNum += speed * 3.8
  221. } else if (midBotNum > clientMidWidth + clientWidth * 0.4 && midBotNum <= clientMidWidth + clientWidth * 0.45) {
  222. smoothAnimationState.translateXNum += speed * 4.4
  223. } else if (midBotNum > clientMidWidth + clientWidth * 0.45 && midBotNum <= clientMidWidth + clientWidth * 0.5) {
  224. smoothAnimationState.translateXNum += speed * 5
  225. }
  226. // 最多移动的位置
  227. const maxTranslateXNum =
  228. smoothAnimationState.canvasDomWith - smoothAnimationState.osdmScrollDomWith - (smoothAnimationState.osdmScrollDom?.scrollLeft || 0)
  229. if (smoothAnimationState.translateXNum > maxTranslateXNum) {
  230. smoothAnimationState.translateXNum = maxTranslateXNum
  231. }
  232. moveTranslateXNum(smoothAnimationState.translateXNum)
  233. }
  234. /**
  235. * 根据 translateXNum 滚动到位置
  236. */
  237. export function moveTranslateXNum(translateXNum: number) {
  238. smoothAnimationState.osmdCanvasPageDom && (smoothAnimationState.osmdCanvasPageDom.style.transform = `translateX(-${translateXNum}px)`)
  239. smoothAnimationState.selectionBoxDom && (smoothAnimationState.selectionBoxDom.style.transform = `translateX(-${translateXNum}px)`)
  240. }
  241. /**
  242. * 进度条和块移动方法
  243. */
  244. function smoothAnimationMove(pos: { x: number; y: number }, pointsPos: pointsPosType, progresspointsPos?: pointsPosType) {
  245. smoothAnimationState.smoothBotDom && (smoothAnimationState.smoothBotDom.style.transform = `translate(${pos.x}px, ${pos.y}px)`)
  246. smoothAnimationState.canvasCtx && drawSmoothCurve(smoothAnimationState.canvasCtx, pointsPos, progresspointsPos)
  247. }
  248. /**
  249. * 计算视口宽度
  250. */
  251. export function calcClientWidth() {
  252. smoothAnimationState.osdmScrollDomWith = smoothAnimationState.osdmScrollDom?.offsetWidth || 0
  253. smoothAnimationState.osdmScrollDomOffsetWidth = smoothAnimationState.osdmScrollDom?.offsetLeft || 0
  254. }
  255. /**
  256. * 创建dom
  257. */
  258. function createSmoothAnimation() {
  259. // osdmScrollDom
  260. const osdmScrollDom = document.querySelector("#musicAndSelection") as HTMLElement
  261. smoothAnimationState.osdmScrollDom = osdmScrollDom
  262. // osmdCanvasPage
  263. const osmdCanvasPageDom = document.querySelector("#osmdCanvasPage1") as HTMLElement
  264. smoothAnimationState.osmdCanvasPageDom = osmdCanvasPageDom
  265. // selectionBox
  266. setTimeout(() => {
  267. const selectionBoxDom = document.querySelector("#selectionBox") as HTMLElement
  268. smoothAnimationState.selectionBoxDom = selectionBoxDom
  269. }, 0)
  270. // box
  271. const smoothAnimationBoxDom = document.createElement("div")
  272. smoothAnimationBoxDom.className = "smoothAnimationBox smoothAnimationBoxHide"
  273. smoothAnimationState.smoothAnimationBoxDom = smoothAnimationBoxDom
  274. // con
  275. const smoothAnimationConDom = document.createElement("div")
  276. smoothAnimationConDom.className = "smoothAnimationCon"
  277. //canvas
  278. const smoothCanvasDom = document.createElement("canvas")
  279. smoothCanvasDom.className = "smoothCanvas"
  280. smoothAnimationState.canvasDom = smoothCanvasDom
  281. smoothAnimationState.canvasDomWith = osmdCanvasPageDom?.offsetWidth || 0
  282. smoothCanvasDom.width = smoothAnimationState.canvasDomWith
  283. smoothCanvasDom.height = smoothAnimationState.canvasDomHeight
  284. smoothAnimationState.canvasCtx = smoothCanvasDom.getContext("2d")
  285. // bot
  286. const smoothBotDom = document.createElement("div")
  287. smoothBotDom.className = "smoothBot"
  288. smoothAnimationState.smoothBotDom = smoothBotDom
  289. // 添加小鸟
  290. render(h(Bird), smoothBotDom)
  291. smoothAnimationConDom.appendChild(smoothCanvasDom)
  292. smoothAnimationConDom.appendChild(smoothBotDom)
  293. smoothAnimationBoxDom.appendChild(smoothAnimationConDom)
  294. // 添加到 osmdCanvasPage1
  295. osmdCanvasPageDom?.insertBefore(smoothAnimationBoxDom, osmdCanvasPageDom.firstChild)
  296. }
  297. /**
  298. * 根据音符获取坐标
  299. */
  300. function getPointsPosByBatePos(): pointsPosType {
  301. let totalAvInde = 0
  302. // 取平均值
  303. const totalAv =
  304. state.times.reduce((total, item) => {
  305. if (item.frequency !== -1) {
  306. // -1 为休止符
  307. total += item.frequency
  308. totalAvInde++
  309. }
  310. return total
  311. }, 0) / totalAvInde
  312. const pointsPos = state.times.reduce((posArr: any[], item) => {
  313. // 当休止小节,可能当前音符在谱面上没有实际的音符(没有bbox),所以往后找谱面上有的音符
  314. if (item.bbox) {
  315. posArr.push({
  316. MeasureNumberXML: item.MeasureNumberXML,
  317. x: item.bbox.x,
  318. // 当为休止符的时候 取最下面的位置*0.9,确保能显示完整
  319. y:
  320. smoothAnimationState.canvasDomHeight / 2 -
  321. ((((item.frequency === -1 ? 2 * totalAv * 0.1 : item.frequency) - totalAv) / totalAv) * smoothAnimationState.canvasDomHeight) / 2
  322. })
  323. }
  324. return posArr
  325. }, [])
  326. // 最后一个音符延长(这里建立一个虚拟的音符延长)
  327. const extendPoint = {
  328. ...pointsPos[pointsPos.length - 1]
  329. }
  330. extendPoint.MeasureNumberXML++
  331. // 当总长度减30小于最后一个音符时候,取最后一个音符加15
  332. extendPoint.x = smoothAnimationState.canvasDomWith - 30 > extendPoint.x ? smoothAnimationState.canvasDomWith - 30 : extendPoint.x + 15
  333. pointsPos.push(extendPoint)
  334. return pointsPos
  335. }
  336. /**
  337. * 使用传入的曲线的顶点坐标创建平滑曲线的顶点。
  338. * @param {Array} points 曲线顶点坐标数组,
  339. * @param {Float} tension 密集程度,默认为 0.5
  340. * @param {Boolean} closed 是否创建闭合曲线,默认为 false
  341. * @param {Int} numberOfSegments 平滑曲线 2 个顶点间的线段数,默认为 20
  342. * @return {Array} 平滑曲线的顶点坐标数组
  343. */
  344. function createSmoothCurvePoints(pointsPos: pointsPosType, tension?: number, closed?: boolean, numberOfSegments?: number) {
  345. if (pointsPos.length <= 2) {
  346. return pointsPos
  347. }
  348. tension = tension ? tension : 0.5
  349. closed = closed ? true : false
  350. numberOfSegments = numberOfSegments ? numberOfSegments : 20
  351. let ps = pointsPos.slice(0),
  352. result = [],
  353. x,
  354. y,
  355. t1x,
  356. t2x,
  357. t1y,
  358. t2y,
  359. c1,
  360. c2,
  361. c3,
  362. c4,
  363. st,
  364. t,
  365. i
  366. if (closed) {
  367. ps.unshift(pointsPos[pointsPos.length - 1])
  368. ps.unshift(pointsPos[pointsPos.length - 1])
  369. ps.push(pointsPos[0])
  370. } else {
  371. ps.unshift(pointsPos[0])
  372. ps.push(pointsPos[pointsPos.length - 1])
  373. }
  374. for (i = 1; i < ps.length - 2; i += 1) {
  375. //console.log(ps[i + 1].MeasureNumberXML, ps[i - 1].MeasureNumberXML, ps[i + 2].MeasureNumberXML, ps[i].MeasureNumberXML)
  376. t1x = (ps[i + 1].x - ps[i - 1].x) * tension
  377. t2x = (ps[i + 2].x - ps[i].x) * tension
  378. t1y = (ps[i + 1].y - ps[i - 1].y) * tension
  379. t2y = (ps[i + 2].y - ps[i].y) * tension
  380. // 当中途出现反复 刚开始反复时候 53 52 22 52 (22)中途值会变小 这里强行拉大 防止算法平均值出现很大偏差
  381. if (ps[i + 1].MeasureNumberXML - ps[i + 2].MeasureNumberXML > 1) {
  382. const nowNumberXML = ps[i + 1].MeasureNumberXML + 1
  383. //在当前值的情况下 向前一位
  384. let index = ps.findIndex(item => {
  385. return nowNumberXML === item.MeasureNumberXML
  386. })
  387. // 查询不到index时候取当前值
  388. index === -1 && (index = i + 1)
  389. t2x = (ps[index].x - ps[i].x) * tension
  390. }
  391. // 当中途出现反复 结束反复时候 22 53 22 22 (53)中途值会变大 这里强行缩小 防止算法平均值出现很大偏差
  392. if (ps[i - 1].MeasureNumberXML - ps[i].MeasureNumberXML > 1) {
  393. //在当前值的情况下 向后一位
  394. const nowNumberXML = ps[i].MeasureNumberXML - 1
  395. let index = ps.findIndex((item, index) => {
  396. return nowNumberXML === item.MeasureNumberXML && nowNumberXML !== ps[index + 1]?.MeasureNumberXML
  397. })
  398. // 查询不到index时候取当前值
  399. index === -1 && (index = i)
  400. t1x = (ps[i + 1].x - ps[index].x) * tension
  401. }
  402. // 当中途出现跳房子 刚开始跳房子时候 35 35 54 35 (54)中途值会变大 这里强行缩小 防止算法平均值出现很大偏差
  403. if (ps[i + 1].MeasureNumberXML - ps[i + 2].MeasureNumberXML < -1) {
  404. const nowNumberXML = ps[i + 1].MeasureNumberXML + 1
  405. //在当前值的情况下 向前一位
  406. let index = ps.findIndex(item => {
  407. return nowNumberXML === item.MeasureNumberXML
  408. })
  409. // 查询不到index时候取当前值
  410. index === -1 && (index = i + 1)
  411. t2x = (ps[index].x - ps[i].x) * tension
  412. }
  413. // 当中途出现跳房子 结束跳房子时候 54 35 54 54 (35)中途值会变小 这里强行拉大 防止算法平均值出现很大偏差
  414. if (ps[i - 1].MeasureNumberXML - ps[i].MeasureNumberXML < -1) {
  415. const nowNumberXML = ps[i].MeasureNumberXML - 1
  416. let index = ps.findIndex((item, index) => {
  417. return nowNumberXML === item.MeasureNumberXML && nowNumberXML !== ps[index + 1]?.MeasureNumberXML
  418. })
  419. // 查询不到index时候取当前值
  420. index === -1 && (index = i)
  421. t1x = (ps[i + 1].x - ps[index].x) * tension
  422. }
  423. const nowMeasureNumberXML = pointsPos[i - 1].MeasureNumberXML
  424. const nextMeasureNumberXML = pointsPos[i].MeasureNumberXML
  425. for (t = 0; t <= numberOfSegments; t++) {
  426. // 小于1时候是反复 大于1是跳房子 不画曲线 停留
  427. if (nextMeasureNumberXML - nowMeasureNumberXML < 0 || nextMeasureNumberXML - nowMeasureNumberXML > 1) {
  428. //console.log(x, y)
  429. result.push({
  430. x: x as number,
  431. y: y as number,
  432. MeasureNumberXML: nowMeasureNumberXML
  433. })
  434. continue
  435. }
  436. st = t / numberOfSegments
  437. c1 = 2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1
  438. c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2)
  439. c3 = Math.pow(st, 3) - 2 * Math.pow(st, 2) + st
  440. c4 = Math.pow(st, 3) - Math.pow(st, 2)
  441. x = c1 * ps[i].x + c2 * ps[i + 1].x + c3 * t1x + c4 * t2x
  442. y = c1 * ps[i].y + c2 * ps[i + 1].y + c3 * t1y + c4 * t2y
  443. //console.log(x, y)
  444. result.push({
  445. x,
  446. y,
  447. MeasureNumberXML: t === numberOfSegments ? nextMeasureNumberXML : nowMeasureNumberXML
  448. })
  449. }
  450. }
  451. return result
  452. }
  453. /**
  454. * 根据坐标划线
  455. */
  456. function drawSmoothCurve(context: CanvasRenderingContext2D, pointsPos: pointsPosType, progresspointsPos?: pointsPosType) {
  457. context.lineWidth = 2
  458. context.lineJoin = 'round';// 优化锯齿
  459. context.lineCap = 'round'; // 优化锯齿
  460. context.strokeStyle = "rgba(255,255,255,0.6)"
  461. drawLines(context, pointsPos)
  462. if (progresspointsPos?.length) {
  463. context.strokeStyle = "#FFC121"
  464. drawLines(context, progresspointsPos)
  465. }
  466. }
  467. function drawLines(context: CanvasRenderingContext2D, points: pointsPosType) {
  468. context.beginPath()
  469. context.moveTo(points[0].x, points[0].y)
  470. for (let i = 1; i < points.length - 1; i++) {
  471. if (Math.abs(points[i].MeasureNumberXML - points[i - 1].MeasureNumberXML) > 1) {
  472. // 取消反复和跳房子连线
  473. context.stroke()
  474. context.beginPath()
  475. context.moveTo(points[i + 1].x, points[i + 1].y)
  476. continue
  477. }
  478. context.lineTo(points[i].x, points[i].y)
  479. }
  480. context.stroke()
  481. }