customMusicScore.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. import { ref } from "vue";
  2. import state, { customData } from "../state"
  3. import { getQuery } from "/src/utils/queryString";
  4. import { setGlobalData } from "/src/utils";
  5. const query: any = getQuery();
  6. interface IItem {
  7. id?: string
  8. y?: number
  9. isLast?: boolean
  10. childIndex?: number[]
  11. }
  12. interface IItemList {
  13. parts: string[],
  14. tieId?: string[],
  15. staveSection?: IItem[],
  16. vfmodifiers?: IItem[],
  17. voltas?: number
  18. vfcurve?: IItem[]
  19. stavenote?: IItem[]
  20. }
  21. interface IMusicList {
  22. [_key: string]: IItemList[]
  23. }
  24. const container = ref();
  25. /** 曲谱配置: 重叠 */
  26. export const resetGivenFormate = () => {
  27. interface IItem {
  28. id?: string
  29. y?: number
  30. isLast?: boolean
  31. childIndex?: number[]
  32. }
  33. interface IItemList {
  34. parts: string[],
  35. tieId?: string[],
  36. staveSection?: IItem[],
  37. vfmodifiers?: IItem[],
  38. voltas?: number
  39. vfcurve?: IItem[]
  40. stavenote?: IItem[]
  41. }
  42. interface IMusicList {
  43. [_key: string]: IItemList[]
  44. }
  45. const musicList: IMusicList = {
  46. '12200': [
  47. {parts: ['0', '1'], tieId: ['1483']},
  48. {parts: ['2'], tieId: ['1463']},
  49. {parts: ['10'], tieId: ['1246']},
  50. {parts: ['11'], tieId: ['2455']},
  51. {parts: ['13'], tieId: ['1488', '1688']},
  52. {parts: ['14', '15'], tieId: ['1272']},
  53. {parts: ['16'], tieId: ['1264', '1368'], staveSection: [{id: 'section-0', y: -10}]},
  54. ],
  55. '12420': [
  56. {parts: ['0'], tieId: ['1298', '1405', '1998', '2598', '3229', '2731', '2617']}
  57. ],
  58. '7729': [
  59. {parts: ['3'], tieId: ['1498', '1660']}
  60. ],
  61. '7439': [
  62. {parts: ['23'], vfmodifiers: [{id: 'modifiers-130', y: -18, isLast: true}]}
  63. ],
  64. '12711': [
  65. { parts: ['0'], voltas: -12},
  66. { parts: ['4'],voltas: -8},
  67. ],
  68. '3581': [
  69. { parts: ['0'], voltas: -8},
  70. ],
  71. '6244': [
  72. { parts: ['15'], stavenote: [{id: 'vf-auto1608', y: -15}]},
  73. ],
  74. '7473': [
  75. { parts: ['0'], voltas: -8},
  76. ]
  77. }
  78. const tieList = musicList[state.cbsExamSongId as string]
  79. if (tieList) {
  80. const partIndex = state.partIndex + ""
  81. const tie = tieList.find((item) => item.parts.includes(partIndex))
  82. if (!tie) return
  83. // 延音线和连线重叠
  84. if (tie.tieId && tie.tieId.length) {
  85. for(let tieIndex = 0; tieIndex < tie.tieId.length; tieIndex++){
  86. const vftie: any = document.querySelector(`#vf-auto${tie.tieId[tieIndex]}-tie`)
  87. const vfcurve = vftie?.parentNode?.parentNode?.querySelectorAll('.vf-curve')
  88. if (vfcurve && vfcurve.length){
  89. for(let i = 0; i < vfcurve.length; i++){
  90. const result = collisionDetection(vftie, vfcurve[i])
  91. if (result.isCollision){
  92. vfcurve[i].style.transform = `translateY(-8px)`;
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. // 小节数字
  100. if (tie.staveSection && tie.staveSection.length) {
  101. const sectionList = document.querySelectorAll('.vf-StaveSection')
  102. sectionList.forEach((node, index) => {
  103. node.classList.add(`section-${index}`)
  104. })
  105. for(let i = 0; i < tie.staveSection.length; i++){
  106. const item: any = document.querySelector( '.' + tie.staveSection[i].id)
  107. if (item){
  108. item.style.transform = `translateY(${tie.staveSection[i].y}px)`;
  109. }
  110. }
  111. }
  112. // modifiers 里面的符号
  113. if(tie.vfmodifiers && tie.vfmodifiers.length){
  114. const modifierList = document.querySelectorAll('.vf-modifiers')
  115. modifierList.forEach((node, index) => {
  116. node.classList.add(`modifiers-${index}`)
  117. })
  118. for(let i = 0; i < tie.vfmodifiers.length; i++){
  119. const modifier = tie.vfmodifiers[i]
  120. const item: SVGAElement = document.querySelector( '.' + modifier.id)!
  121. if (item){
  122. if (modifier.isLast){
  123. const lastEle: any = Array.from(item.childNodes).at(-1)
  124. if (lastEle){
  125. lastEle.style.transform = `translateY(${modifier.y}px)`;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. // 房子
  132. if (tie.voltas){
  133. const modifierList = document.querySelectorAll('.vf-Volta') as unknown as HTMLElement[]
  134. modifierList.forEach((node, index) => {
  135. node.style.transform = `translateY(${tie.voltas}px)`;
  136. })
  137. }
  138. // 单个音符
  139. if (tie.stavenote && tie.stavenote.length) {
  140. for(let i = 0; i < tie.stavenote.length; i++){
  141. const item = tie.stavenote[i]
  142. const ele = document.querySelector('#' + item.id)! as unknown as HTMLElement
  143. ele && (ele.style.transform = `translateY(${item.y}px)`)
  144. }
  145. }
  146. }
  147. };
  148. const initNoteCoord = () => {
  149. const allNoteDot: any = Array.from(document.querySelectorAll('.node-dot'));
  150. state.noteCoords = allNoteDot.map((note: any) => {
  151. const note_bbox = note?.getBoundingClientRect?.() || { x: 0, y: 0 };
  152. return {
  153. x: note_bbox.x,
  154. y: note_bbox.y
  155. }
  156. })
  157. console.log(11111,state.noteCoords)
  158. }
  159. export const moveGracePosition = (needTrans?: boolean) => {
  160. /**
  161. * TODO:曲目:摇篮曲(节奏练习)-倚音位置 特殊处理
  162. */
  163. const specialIds = ['1788850864767643649','1788502467554750466','1789839575249596417','1788501975122489346','1796006876341813249'];
  164. if (specialIds.includes(state.cbsExamSongId) || needTrans) {
  165. const lastCurve: any = Array.from(document.getElementsByClassName('vf-curve'))?.last();
  166. if (lastCurve) {
  167. lastCurve.style.display = 'none';
  168. }
  169. if (state.musicRenderType === 'staff') {
  170. // const transNoteDom = document.getElementById('vf-auto2182')?.getElementsByClassName('vf-modifiers')?.[0];
  171. // const transBeamDom = document.getElementById('auto3167')?.parentNode?.getElementsByClassName('vf-beams')?.[0];
  172. // if (transNoteDom) {
  173. // transNoteDom.style.transform = 'translateX(-0.5rem)';
  174. // }
  175. // if (transBeamDom) {
  176. // transBeamDom.style.transform = 'translateX(-0.5rem)';
  177. // }
  178. } else {
  179. // vf-auto2172 , vf-auto2384
  180. const signatureDom = document.getElementById('auto2670');
  181. const signatureDom2 = document.getElementById('auto2710');
  182. const signatureDom3 = document.getElementById('auto3099');
  183. const signatureDom4 = document.getElementById('auto3339');
  184. const needTransLateDom: any = state.cbsExamSongId == '1789839575249596417' && document.getElementById('vf-auto1554')?.getElementsByClassName('vf-modifier')?.[0];
  185. const arrowDom = state.cbsExamSongId == '1789839575249596417' && document.getElementById('vf-auto1554-lines');
  186. const needTransLateDom2: any = state.cbsExamSongId == '1788501975122489346' && document.getElementById('vf-auto2116')?.getElementsByClassName('vf-modifier')?.[0];
  187. const arrowDom2 = state.cbsExamSongId == '1788501975122489346' && document.getElementById('vf-auto2116-lines');
  188. const needTransLateDom3: any = state.cbsExamSongId == '1788502467554750466' && document.getElementById('vf-auto2122')?.getElementsByClassName('vf-modifier')?.[0];
  189. const arrowDom3 = state.cbsExamSongId == '1788502467554750466' && document.getElementById('vf-auto2122-lines');
  190. if (signatureDom) signatureDom.style.display = 'none';
  191. if (signatureDom2) signatureDom2.style.display = 'none';
  192. if (signatureDom3) signatureDom3.style.display = 'none';
  193. if (signatureDom4) signatureDom4.style.display = 'none';
  194. if (needTransLateDom) needTransLateDom.style.transform = 'translateX(-0.65rem)';
  195. if (needTransLateDom2) needTransLateDom2.style.transform = 'translateX(-0.65rem)';
  196. if (needTransLateDom3) needTransLateDom3.style.transform = 'translateX(-0.65rem)';
  197. if (arrowDom) arrowDom.style.transform = 'translateX(-0.65rem)';
  198. if (arrowDom2) arrowDom2.style.transform = 'translateX(-0.65rem)';
  199. if (arrowDom3) arrowDom3.style.transform = 'translateX(-0.65rem)';
  200. if (arrowDom || arrowDom2 || arrowDom3) {
  201. const path: any = arrowDom ? arrowDom.querySelector('path') : arrowDom2 ? arrowDom2.querySelector('path') : arrowDom3 ? arrowDom3.querySelector('path') : null;
  202. let d = path?.getAttribute("d");
  203. if (d) {
  204. const patchStr = d.split('L')?.last()?.split(" ")?.[0];
  205. let startX = d.split("M")?.[1]?.split(" ")[0] || 0;
  206. startX = startX ? Number(startX) : 0;
  207. let endX = d.split("L")?.last().split(" ")[0] || 0;
  208. endX = endX ? Number(endX) : 0;
  209. const distanceX = endX - startX;
  210. const transX = startX - distanceX;
  211. d = d.replace(`L${patchStr}`,`L${transX}`);
  212. path.setAttribute("d", d);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. // 谱面优化
  219. export const resetFormate = () => {
  220. container.value = document.getElementById('scrollContainer')
  221. // if (state.extStyleConfigJson || !container.value) return;
  222. if (!container.value) return;
  223. moveGracePosition();
  224. // setTimeout(() => {
  225. // initNoteCoord();
  226. // }, 0);
  227. const stafflines: SVGAElement[] = Array.from((container.value as HTMLElement).querySelectorAll(".staffline"));
  228. const baseStep = 4; // 两个元素相间,的间距
  229. const musicalDistance = 28; // 音阶与第一条线谱的间距,默认设置为28
  230. for (let i = 0, len = stafflines.length; i < len; i++) {
  231. const staffline = stafflines[i];
  232. const stafflineBox = staffline.getBBox();
  233. const stafflineCenter = stafflineBox.y + stafflineBox.height / 2;
  234. const vfmeasures: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure"));
  235. const vfcurve: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-curve"));
  236. const vfvoices: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-voices"));
  237. const vfbeams: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-beams"));
  238. const vfties: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-ties"));
  239. const vflines: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-line"));
  240. const texts: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave text"));
  241. const rects: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave rect[fill=none]"));
  242. const staveSection: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure .vf-staveSection"));
  243. const paths: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave path"));
  244. const dotModifiers: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure .vf-stopDot"));
  245. const staves: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave"));
  246. // 获取第一个线谱的y轴坐标
  247. const firstLinePathY = paths[0]?.getBBox().y || 0
  248. // 反复标记 和 小节碰撞
  249. const repetWord = ["To Coda", "D.S. al Coda", "Coda"];
  250. texts
  251. .filter((n) => repetWord.includes(n.textContent || ""))
  252. .forEach((t) => {
  253. vfbeams.forEach((curve) => {
  254. const result = collisionDetection(t, curve);
  255. const prePath: SVGAElement = t?.previousSibling as unknown as SVGAElement;
  256. if (result.isCollision) {
  257. const shift_y = Number(t.getAttribute("y")) - (result.b1 - result.t2) - baseStep + "";
  258. t.setAttribute("y", shift_y);
  259. // console.log('音阶间距',shift_y)
  260. if (prePath && prePath.getAttribute("stroke-width") === "0.3" && prePath.getAttribute("stroke") === "none" && (prePath.getAttribute("d")?.length || 0) > 3000) {
  261. prePath.style.transform = `translateY(${-(result.b1 - result.t2 + baseStep)}px)`;
  262. }
  263. }
  264. });
  265. vfvoices.forEach((curve) => {
  266. const result = collisionDetection(t, curve);
  267. const prePath: SVGAElement = t?.previousSibling as unknown as SVGAElement;
  268. if (result.isCollision) {
  269. const shift_y = Number(t.getAttribute("y")) - (result.b1 - result.t2) - baseStep + "";
  270. t.setAttribute("y", shift_y);
  271. // console.log('音阶间距',shift_y)
  272. if (prePath && prePath.getAttribute("stroke-width") === "0.3" && prePath.getAttribute("stroke") === "none" && (prePath.getAttribute("d")?.length || 0) > 3000) {
  273. prePath.style.transform = `translateY(${-(result.b1 - result.t2 + baseStep)}px)`;
  274. }
  275. }
  276. });
  277. });
  278. // 文字方框和飞线碰撞
  279. staveSection.forEach((t) => {
  280. let shift_y = 0;
  281. [...vfcurve, ...vfties, ...vfvoices].forEach((curve) => {
  282. const result = collisionDetection(t, curve);
  283. if (result.isCollision) {
  284. shift_y = Math.min(shift_y, result.t2 - result.b1 - baseStep);
  285. }
  286. });
  287. t.style.transform = `translateY(${shift_y}px)`;
  288. });
  289. // 文字和小节碰撞
  290. let vftexts = Array.from(staffline.querySelectorAll(".vf-text > text")).filter((n: any) => n.getBBox().y < stafflineCenter);
  291. for (let i = 0; i < vftexts.length; i++) {
  292. const _text = vftexts[i];
  293. for (let j = 0; j < vftexts.length; j++) {
  294. if (_text.parentNode === vftexts[j].parentNode) continue;
  295. const result = collisionDetection(_text as SVGAElement, vftexts[j] as SVGAElement);
  296. if (result.isCollision) {
  297. if (_text.textContent === vftexts[j].textContent) {
  298. vftexts[j].parentNode?.removeChild(vftexts[j]);
  299. continue;
  300. }
  301. }
  302. }
  303. }
  304. vftexts = Array.from(staffline.querySelectorAll(".vf-text > text")).filter((n: any) => n.getBBox().y < stafflineCenter);
  305. let maxY = 0;
  306. let _vftexts: SVGAElement[] = [];
  307. vftexts.forEach((vftext: any) => {
  308. const textBox = vftext.getBBox();
  309. if (textBox.y < stafflineCenter) {
  310. maxY = Math.max(maxY, textBox.y + textBox.height);
  311. //console.log('音阶间距',textBox.y, textBox.height)
  312. _vftexts.push(vftext as SVGAElement);
  313. }
  314. });
  315. if (maxY !== 0 && _vftexts.length > 1) {
  316. _vftexts.forEach((vftext) => {
  317. vftext.setAttribute("y", maxY + "");
  318. //console.log('音阶间距',maxY)
  319. });
  320. }
  321. vftexts.forEach((vftext) => {
  322. [...vfcurve, ...vfmeasures, ...vflines].forEach((vfmeasure) => {
  323. let result = collisionDetection(vftext as SVGAElement, vfmeasure);
  324. if (result.isCollision && result.b1 < result.b2 && result.t1 < result.b2 - (result.b2 - result.t2) / 2) {
  325. const shift_y = Number(vftext.getAttribute("y")) - (result.b1 - result.t2) - baseStep + "";
  326. vftext.setAttribute("y", shift_y);
  327. //console.log('音阶间距',shift_y)
  328. }
  329. });
  330. });
  331. vftexts.forEach((vftext) => {
  332. vftexts.forEach((text) => {
  333. if (vftext.parentNode !== text.parentNode && !["marcato", "legato"].includes(vftext.textContent as string)) {
  334. if (["marcato", "legato"].includes(text.textContent as string)) {
  335. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement, 30, 30);
  336. if (result.isCollision) {
  337. const textBBox = (vftext as SVGAElement).getBBox();
  338. text.setAttribute("x", textBBox.x + textBBox.width + 5 + "");
  339. text.setAttribute("y", textBBox.y + textBBox.height - 5 + "");
  340. //console.log('音阶间距',textBBox.y + textBBox.height - 5 + "")
  341. }
  342. } else {
  343. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement);
  344. if (result.isCollision) {
  345. const _y = Number(vftext.getAttribute("y"));
  346. const shift_y = result.b2 - result.t2 < 24 ? 24 : result.b2 - result.t2;
  347. text.setAttribute("y", _y - shift_y - 0.5 + "");
  348. //console.log('音阶间距',_y - shift_y - 0.5 + "")
  349. }
  350. }
  351. }
  352. });
  353. });
  354. // 修改音阶和线谱的间距
  355. const clefList = ['C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#', 'G#', 'F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb', 'Fb', 'D#', 'A#', 'E#']
  356. const btransList = ['Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb', 'Fb']
  357. const jtrsnsList = ['F#', 'C#', 'G#', 'D#', 'A#', 'E#', 'B#']
  358. vftexts.forEach((label: any) => {
  359. const labelText = label.textContent as string
  360. if (clefList.includes(labelText)){
  361. const _y = Number(label.getAttribute("y"))
  362. const endY = firstLinePathY ? firstLinePathY - musicalDistance : _y
  363. label.setAttribute("y", endY)
  364. }
  365. if (btransList.includes(labelText)) {
  366. label.textContent = labelText.replace('b','♭')
  367. }
  368. if (jtrsnsList.includes(labelText)) {
  369. label.textContent = labelText.replace('#','♯')
  370. }
  371. });
  372. dotModifiers.forEach((group: any) => {
  373. if (state.musicRenderType === 'fixedTone') {
  374. group.setAttribute('transform', 'translate(3,-12)')
  375. } else {
  376. group.setAttribute('transform', 'translate(3,-7)')
  377. }
  378. });
  379. const vftextBottom = Array.from(staffline.querySelectorAll(".vf-text > text")).filter((n: any) => n.getBBox().y > stafflineCenter);
  380. const vflineBottom = Array.from(staffline.querySelectorAll(".vf-line")).filter((n: any) => n.getBBox().y > stafflineCenter);
  381. // 去重
  382. for (let i = 0; i < vftextBottom.length; i++) {
  383. const _text = vftextBottom[i];
  384. for (let j = 0; j < vftextBottom.length; j++) {
  385. if (_text.parentNode === vftextBottom[j].parentNode) continue;
  386. const result = collisionDetection(_text as SVGAElement, vftextBottom[j] as SVGAElement);
  387. if (result.isCollision) {
  388. if (_text.textContent === vftextBottom[j].textContent) {
  389. vftextBottom[j].parentNode?.removeChild(vftextBottom[j]);
  390. continue;
  391. }
  392. }
  393. }
  394. }
  395. // 1,2线谱底部文字重叠问题
  396. vftextBottom.forEach((vftext) => {
  397. [...vfmeasures].forEach((n) => {
  398. let result = collisionDetection(vftext as SVGAElement, n);
  399. if (result.isCollision) {
  400. vftext.setAttribute("y", result.b2 + Math.abs(result.t1 - Number(vftext.getAttribute("y"))) + "");
  401. //console.log('音阶间距', result.b2 + Math.abs(result.t1 - Number(vftext.getAttribute("y"))) + "")
  402. }
  403. });
  404. });
  405. // 如果渐弱渐强有平行的文字
  406. vflineBottom.forEach((line) => {
  407. const texts: any[] = [];
  408. if (line.nextElementSibling?.classList.contains("vf-line")) {
  409. vftextBottom.forEach((text) => {
  410. let result = collisionDetection(line as SVGAElement, text as SVGAElement, 20, 20);
  411. if (result.isCollision) {
  412. texts.push({
  413. text: text as SVGAElement,
  414. result,
  415. });
  416. }
  417. });
  418. }
  419. if (texts.length === 1) {
  420. const result = texts[0].result;
  421. const text = texts[0].text;
  422. if (result.x2 + result.w2 < result.x1) {
  423. // 左
  424. if (Math.abs(result.y2 - result.y1) > 10) {
  425. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  426. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  427. }
  428. } else if (result.x2 > result.x1 + result.w1) {
  429. // 右
  430. if (Math.abs(result.y2 - result.y1) > 10) {
  431. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  432. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  433. }
  434. } else {
  435. if (Math.abs(result.x2 - result.x1) < Math.abs(result.x2 + result.w2 - result.x1 - result.w1)) {
  436. // console.log(text, '有交集', '靠左')
  437. text.setAttribute("x", result.x1 - result.w2 - 5 + "");
  438. if (Math.abs(result.y2 - result.y1) > 10) {
  439. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  440. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  441. }
  442. } else {
  443. // console.log(text, '有交集', '靠右')
  444. text.setAttribute("x", result.x1 + result.w1 + 5 + "");
  445. if (Math.abs(result.y2 - result.y1) > 10) {
  446. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  447. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  448. }
  449. }
  450. }
  451. } else if (texts.length === 2) {
  452. const result1 = texts[0].result;
  453. const text1 = texts[0].text;
  454. const result2 = texts[1].result;
  455. const text2 = texts[1].text;
  456. text1.setAttribute("x", result1.x1 - result1.w2 - 5 + "");
  457. if (Math.abs(result1.y2 - result1.y1) > 10) {
  458. text1.setAttribute("y", result1.y1 + result1.h2 / 2 + "");
  459. //console.log('音阶间距', result1.y1 + result1.h2 / 2 + "")
  460. }
  461. text2.setAttribute("x", result2.x1 + result2.w1 + 5 + "");
  462. if (Math.abs(result2.y2 - result2.y1) > 10) {
  463. text2.setAttribute("y", result2.y1 + result2.h2 / 2 + "");
  464. //console.log('音阶间距', result2.y1 + result2.h2 / 2 + "")
  465. }
  466. } else if (texts.length === 3) {
  467. // console.log(texts)
  468. }
  469. });
  470. vftextBottom.forEach((vftext) => {
  471. vftextBottom.forEach((text) => {
  472. if (vftext.parentNode !== text.parentNode && !["marcato", "legato", "cresc.", "Cantabile"].includes(vftext.textContent as string)) {
  473. if (["marcato", "legato", "cresc.", "Cantabile"].includes(text.textContent as string)) {
  474. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement, 30, 30);
  475. if (result.isCollision) {
  476. const textBBox = (vftext as SVGAElement).getBBox();
  477. text.setAttribute("x", textBBox.x + textBBox.width + 5 + "");
  478. text.setAttribute("y", textBBox.y + textBBox.height - 5 + "");
  479. //console.log('音阶间距', textBBox.y + textBBox.height - 5 + "")
  480. }
  481. } else {
  482. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement);
  483. if (result.isCollision) {
  484. text.setAttribute("y", result.y1 + result.h1 + result.h2 + "");
  485. //console.log('音阶间距', result.y1 + result.h1 + result.h2 + "")
  486. }
  487. }
  488. }
  489. });
  490. });
  491. // 同一行内,多个飞线碰撞
  492. for (let index = 0; index < vfcurve.length; index++) {
  493. let nextIndex = index + 1;
  494. const cur = vfcurve[index];
  495. let next = vfcurve[nextIndex];
  496. let checkDone = false;
  497. while (nextIndex <= vfcurve.length - 1 && !checkDone) {
  498. let result = collisionDetection(cur, next);
  499. if (result.isCollision) {
  500. checkDone = true;
  501. next.style.transform = `translateY(-12px)`;
  502. } else {
  503. nextIndex = nextIndex + 1;
  504. next = vfcurve[nextIndex];
  505. }
  506. }
  507. };
  508. // 给小节添加背景色
  509. staves.forEach((stave: any) => {
  510. const list = [
  511. Array.from(stave?.querySelectorAll(".vf-clef") || []),
  512. Array.from(stave?.querySelectorAll(".vf-keysignature") || []),
  513. Array.from(stave?.getElementsByTagName("text") || []),
  514. Array.from(stave?.querySelectorAll(".vf-StaveSection") || []),
  515. ].flat();
  516. try {
  517. if (list.length) {
  518. list.forEach((_el: any) => {
  519. _el?.style?.setProperty("display", "none");
  520. });
  521. }
  522. } catch (error) {}
  523. const bbox = stave?.getBBox() || {};
  524. const rect = `<rect class="vf-custom-bg" x="${bbox.x}" y="${bbox.y}" width="${bbox.width}" height="${bbox.height}" fill="#609FCF" />`
  525. const rectBottom = `<rect class="vf-custom-bot" x="${bbox.x}" y="${bbox.y+bbox.height}" width="${bbox.width}" height="12" fill="#2B70A5" />`
  526. const customG = `<g>${rect}${rectBottom}</g>`
  527. try {
  528. if (list.length) {
  529. list.forEach((_el: any) => {
  530. _el?.style?.removeProperty("display");
  531. });
  532. }
  533. } catch (error) {}
  534. stave.innerHTML = customG + stave.innerHTML;
  535. });
  536. state.vfmeasures = state.vfmeasures.concat(vfmeasures);
  537. }
  538. // setTimeout(() => this.resetGlobalText());
  539. };
  540. // 技巧文本
  541. const resetGlobalText = () => {
  542. const svg = container.value.querySelector("svg");
  543. if (!svg) return;
  544. const svgBBox = svg.getBBox();
  545. let vfstavetempo: SVGAElement[] = Array.from(container.value.querySelectorAll(".vf-stavetempo")).reduce((eles: SVGAElement[], value: any) => {
  546. if (eles.find((n) => n.outerHTML === value.outerHTML)) value?.parentNode?.removeChild(value);
  547. else eles.push(value);
  548. return eles;
  549. }, []);
  550. const staffline: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline"));
  551. const vfmeasures: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline > .vf-measure"));
  552. const vftexts: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline > .vf-text"));
  553. const vfcurves: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline > .vf-curve"));
  554. vfstavetempo.forEach((child: SVGAElement) => {
  555. let _y = 0;
  556. [...vfmeasures, ...vftexts, ...vfcurves].forEach((ele) => {
  557. const result = collisionDetection(child as SVGAElement, ele);
  558. if (result.isCollision && (result.b1 < result.b2 || result.r1 > result.l2 || result.l1 < result.r2)) {
  559. _y = Math.min(_y, result.t2 - result.b1);
  560. }
  561. });
  562. if (_y !== 0) {
  563. child.style.transform = `translateY(${_y}px)`;
  564. }
  565. const childBBox = child.getBBox();
  566. const rightY = (childBBox.x + childBBox.width) * 0.7 - Number(svg.getAttribute("width"));
  567. if (rightY > 0) {
  568. [...staffline, ...vfstavetempo].forEach((tempo) => {
  569. if (child != tempo) {
  570. const result = collisionDetection(child as SVGAElement, tempo, Math.abs(rightY), Math.abs(_y));
  571. if (result.isCollision) {
  572. _y = result.t2 - result.b1;
  573. }
  574. }
  575. });
  576. child.style.transform = `translate(-${rightY / 0.7}px,${_y}px)`;
  577. }
  578. });
  579. if (svgBBox.y < 0) {
  580. svg.setAttribute("height", Number(svg.getAttribute("height")) - svgBBox.y + 10);
  581. }
  582. };
  583. // 碰撞检测
  584. const collisionDetection = (a: SVGAElement, b: SVGAElement, distance: number = 0, distance_y: number = 0) => {
  585. const abbox = a.getBBox();
  586. const bbbox = b.getBBox();
  587. let t1 = abbox.y - distance_y;
  588. let l1 = abbox.x - distance;
  589. let r1 = abbox.x + abbox.width + distance;
  590. let b1 = abbox.y + abbox.height + distance_y;
  591. let t2 = bbbox.y;
  592. let l2 = bbbox.x;
  593. let r2 = bbbox.x + bbbox.width;
  594. let b2 = bbbox.y + bbbox.height;
  595. if (b1 < t2 || l1 > r2 || t1 > b2 || r1 < l2) {
  596. // 表示没碰上
  597. return {
  598. isCollision: false,
  599. t1,
  600. l1,
  601. r1,
  602. b1,
  603. t2,
  604. l2,
  605. r2,
  606. b2,
  607. x1: abbox.x,
  608. y1: abbox.y,
  609. x2: bbbox.x,
  610. y2: bbbox.y,
  611. h1: abbox.height,
  612. h2: bbbox.height,
  613. w1: abbox.width,
  614. w2: bbbox.width,
  615. };
  616. } else {
  617. return {
  618. isCollision: true,
  619. t1,
  620. l1,
  621. r1,
  622. b1,
  623. t2,
  624. l2,
  625. r2,
  626. b2,
  627. x1: abbox.x,
  628. y1: abbox.y,
  629. x2: bbbox.x,
  630. y2: bbbox.y,
  631. h1: abbox.height,
  632. h2: bbbox.height,
  633. w1: abbox.width,
  634. w2: bbbox.width,
  635. };
  636. }
  637. };
  638. /** 全局曲谱配置 */
  639. export const setGlobalMusicSheet = () => {
  640. const partIndex = state.partIndex + ""
  641. /** 延音线方向问题 start */
  642. const stavetieList = [
  643. {id: '12644', part_index: '25', direction: 1}
  644. ]
  645. const tieItem = stavetieList.find(({id, part_index}) => {
  646. return id == state.cbsExamSongId && part_index == partIndex
  647. })
  648. setGlobalData('tieDirection', tieItem ? tieItem.direction : undefined)
  649. /** 延音线方向问题 end */
  650. const graceList = [
  651. {id: '3509', part_index: '16', direction: 1}
  652. ]
  653. const graceItem = graceList.find(({id, part_index}) => {
  654. return id == state.cbsExamSongId && part_index == partIndex
  655. })
  656. if (graceItem){
  657. setGlobalData('graceCustom', {direction: graceItem.direction})
  658. }
  659. const bassDrumList = [
  660. {id: '3030', part_index: '17', line: 4},
  661. {id: '12704', part_index: '23', line: 3}
  662. ]
  663. const bassDrumItem = bassDrumList.find(({id, part_index}) => {
  664. return id == state.cbsExamSongId && part_index == partIndex
  665. })
  666. if (bassDrumItem){
  667. setGlobalData('customBassDrum', bassDrumItem.line)
  668. }
  669. /** 打击乐多声部,双声部休止符重叠 end */
  670. /** 符杆朝向 */
  671. const stemDirectionList = [
  672. {
  673. id: '11654',
  674. part_index: '16',
  675. stemNotes: [
  676. {id: 124, direction: 0},
  677. {id: 125, direction: 0},
  678. {id: 126, direction: 0},
  679. {id: 127, direction: 0},
  680. {id: 128, direction: 0}
  681. ]
  682. },
  683. {
  684. id: '3581',
  685. part_index: '4',
  686. stemNotes: [
  687. {id: 380, direction: 1},
  688. ]
  689. },
  690. {
  691. id: '3470',
  692. part_index: '0',
  693. stemNotes: [
  694. {id: 36, direction: 1},
  695. {id: 37, direction: 1},
  696. ]
  697. },
  698. {
  699. id: '3470',
  700. part_index: '11',
  701. stemNotes: [
  702. {id: 33, direction: 1},
  703. {id: 56, direction: 1},
  704. ]
  705. },
  706. {
  707. id: '12644',
  708. part_index: '22',
  709. stemNotes: [
  710. {id: 22, direction: 1},
  711. {id: 26, direction: 1},
  712. {id: 135, direction: 1},
  713. {id: 163, direction: 1},
  714. {id: 199, direction: 1},
  715. {id: 204, direction: 1},
  716. {id: 206, direction: 1},
  717. {id: 208, direction: 1},
  718. {id: 210, direction: 1},
  719. {id: 213, direction: 1},
  720. ]
  721. },
  722. {
  723. id: '12303',
  724. part_index: '18',
  725. stemNotes: [
  726. {id: 1, direction: 1},
  727. {id: 4, direction: 1},
  728. {id: 6, direction: 1},
  729. {id: 9, direction: 1},
  730. {id: 12, direction: 1},
  731. {id: 14, direction: 1},
  732. ]
  733. },
  734. {
  735. id: '12669',
  736. part_index: '24',
  737. stemNotes: [
  738. {id: 65, direction: 1},
  739. {id: 296, direction: 1},
  740. {id: 298, direction: 1},
  741. {id: 300, direction: 1},
  742. {id: 338, direction: 1},
  743. ]
  744. },
  745. {
  746. id: '12420',
  747. part_index: '21',
  748. stemNotes: [
  749. {id: 614, direction: 0},
  750. {id: 617, direction: 0},
  751. {id: 619, direction: 0},
  752. {id: 621, direction: 0},
  753. ]
  754. },
  755. {
  756. id: '12711',
  757. part_index: '22',
  758. stemNotes: []
  759. },
  760. {
  761. id: '12973',
  762. part_index: '21',
  763. stemNotes: [
  764. {id: 619, direction: 1},
  765. {id: 622, direction: 1},
  766. {id: 745, direction: 1},
  767. ]
  768. },
  769. ]
  770. const stemDirectionItem = stemDirectionList.find(({id, part_index}) => {
  771. return id == state.cbsExamSongId && part_index == partIndex
  772. })
  773. if (stemDirectionItem) {
  774. setGlobalData('stemDirectionNote', stemDirectionItem.stemNotes)
  775. }
  776. /** vfcure */
  777. const vfcurveList = [
  778. {
  779. id: '12711',
  780. part_index: '4',
  781. vfcurve: [
  782. {MeasureNumberXML: 25, index: 1, bezierEndControlPt: {y: -2}},
  783. {MeasureNumberXML: 33, index: 1, bezierEndControlPt: {y: -2}},
  784. ]
  785. },
  786. {
  787. id: '12059',
  788. part_index: '0',
  789. vfcurve: [
  790. {MeasureNumberXML: 15, bezierEndControlPt: {y: 2.8}, bezierEndPt:{y: 1.1}},
  791. {MeasureNumberXML: 16, bezierEndControlPt: {y: -1}},
  792. {MeasureNumberXML: 19, index: 1, bezierEndControlPt: {y: 2}},
  793. {MeasureNumberXML: 20, bezierEndControlPt: {y: -1}},
  794. {MeasureNumberXML: 42, index: 1, bezierEndControlPt: {y: -1.5}, bezierStartControlPt: {y: -1.5}},
  795. {MeasureNumberXML: 46, index: 3, bezierEndControlPt: {y: -1.5}, bezierStartControlPt: {y: -1.5}},
  796. ]
  797. },
  798. {
  799. id: '12668',
  800. part_index: '11',
  801. vfcurve: [
  802. {MeasureNumberXML: 8, index: 2, bezierEndControlPt: {y: -3}, bezierStartControlPt:{y: -3}, bezierEndPt:{y: -1}},
  803. ]
  804. },
  805. {
  806. id: '11976',
  807. part_index: '0',
  808. vfcurve: [
  809. {MeasureNumberXML: 14, index: 4, bezierEndControlPt: {y: -3}},
  810. {MeasureNumberXML: 14, index: 1, bezierEndPt: {y: 1.5}, bezierEndControlPt: {y: 1}},
  811. ]
  812. },
  813. ]
  814. const vfcurveItem = vfcurveList.find(({id, part_index}) => {
  815. return id == state.cbsExamSongId && part_index == partIndex
  816. })
  817. if (vfcurveItem) {
  818. setGlobalData('vfcurveItem', vfcurveItem.vfcurve)
  819. }
  820. /** drum set声部 重音 */
  821. const customArtPositionList = [
  822. {id: '12644', part_index: '25'}
  823. ]
  824. const customArtPositionItem = customArtPositionList.find(({id, part_index}) => {
  825. return id == state.cbsExamSongId && part_index == partIndex
  826. })
  827. if (customArtPositionItem) {
  828. setGlobalData('customArtPosition', true)
  829. }
  830. /** 全声部声部 - & 全音符 */
  831. const customTenutoList = [
  832. {id: '12645', part_index: '5'}
  833. ]
  834. const customTenutoItem = customTenutoList.find(({id, part_index}) => {
  835. return id == state.cbsExamSongId && part_index == partIndex
  836. })
  837. if (customTenutoItem) {
  838. setGlobalData('customTenutoItem', true)
  839. }
  840. /** 全声部声部 > */
  841. const customAccentList = [
  842. {id: '12711', part_index: '22'},
  843. {id: '12711', part_index: '25'},
  844. ]
  845. const customAccentItem = customAccentList.find(({id, part_index}) => {
  846. return id == state.cbsExamSongId && part_index == partIndex
  847. })
  848. if (customAccentItem || state.isEvxml) {
  849. setGlobalData('customAccentItem', true)
  850. }
  851. /** 全声部声部 + */
  852. const customLefthandpizzicatoList = [
  853. {id: '12711', part_index: '25'},
  854. {id: '7755', part_index: '10'},
  855. {id: '6226', part_index: '16'},
  856. ]
  857. const customLefthandpizzicatoItem = customLefthandpizzicatoList.find(({id, part_index}) => {
  858. return id == state.cbsExamSongId && part_index == partIndex
  859. })
  860. if (customLefthandpizzicatoItem) {
  861. setGlobalData('customLefthandpizzicatoItem', true)
  862. }
  863. }
  864. /** 设置自定义渐慢 */
  865. export const setCustomGradual = () => {
  866. if (state.gradualTimes) {
  867. const detailId = state.cbsExamSongId + "";
  868. const partIndex = state.partIndex + "";
  869. if (["12280"].includes(detailId) && ["24"].includes(partIndex)) {
  870. state.gradualTimes["8"] = "00:26:10";
  871. state.gradualTimes["66"] = "01:53:35";
  872. state.gradualTimes["90"] = "02:41:40";
  873. }
  874. }
  875. };
  876. /** 设置自定义音符数据 */
  877. export const setCustomNoteRealValue = () => {
  878. const detailId = state.cbsExamSongId + "";
  879. const partIndex = state.partIndex + "";
  880. if (["2670"].includes(detailId)) {
  881. customData.customNoteRealValue = {
  882. 0: 0.03125,
  883. };
  884. }
  885. if (["12673"].includes(detailId) && ['22'].includes(partIndex)) {
  886. customData.customNoteRealValue = {
  887. 208: 0.125,
  888. };
  889. }
  890. if (["12667", "12673"].includes(detailId)){
  891. customData.customNoteCurrentTime = true
  892. }
  893. };