index.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="./opensheetmusicdisplay.min.js"></script>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. body {
  15. /* padding-bottom: 60px; */
  16. /* height: 600px;
  17. overflow: hidden; */
  18. }
  19. body::-webkit-scrollbar {
  20. width: 0;
  21. display: none;
  22. }
  23. /* .vf-text {
  24. display: none;
  25. } */
  26. #cursorImg-0 {
  27. display: none;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="osmdContainer" />
  33. <script>
  34. var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay("osmdContainer");
  35. osmd.setOptions({
  36. backend: "svg",
  37. // drawTitle: false,
  38. // drawPartNames: false,
  39. // drawLyricist: false,
  40. drawTitle: false,
  41. drawPartNames: false,
  42. drawLyricist: false,
  43. followCursor: false,
  44. autoResize: true,
  45. drawSubtitle: false,
  46. drawTitle: false,
  47. autoBeam: true,
  48. drawMetronomeMarks: false,
  49. drawMeasureNumbers: false,
  50. drawComposer: false,
  51. autoGenerateMutipleRestMeasuresFromRestMeasures: true,
  52. autoGenerateMultipleRestMeasuresFromRestMeasures: true,
  53. });
  54. osmd.EngravingRules.RenderMultipleRestMeasures = true;
  55. osmd.EngravingRules.CompactMode = true;
  56. osmd.EngravingRules.PageTopMarginNarrow = 6.0; // for compact mode
  57. osmd.EngravingRules.PageBottomMargin = 15.0;
  58. osmd.EngravingRules.PageLeftMargin = 1.0
  59. osmd.EngravingRules.PageRightMargin = 1.0
  60. // console.log('osmd.EngravingRules', osmd.EngravingRules)
  61. // osmd.EngravingRules.StaffDistance = 1.0;
  62. // osmd.EngravingRules.BetweenStaffDistance = 1.0;
  63. // osmd.EngravingRules.MinimumStaffLineDistance = 1.0;
  64. //osmd.EngravingRules.MinSkyBottomDistBetweenStaves = 1.0; // default 1.0. this can cause collisions with slurs and dynamics sometimes
  65. osmd.EngravingRules.MinSkyBottomDistBetweenSystems = 3.0; // default 5.0
  66. // note that osmd.EngravingRules === osmd.rules, since it's passed as a reference
  67. osmd.EngravingRules.MinimumDistanceBetweenSystems = 1;
  68. osmd.setPageFormat('794x1123')
  69. // osmd.setPageFormat('650x884')
  70. function getSvgPngToSize(osmd) {
  71. if (osmd) {
  72. if (osmd.Drawer.Backends.length > 0) {
  73. var imgList = []
  74. for (var idx = 0, len = osmd.Drawer.Backends.length; idx < len; idx++) {
  75. var backend = osmd.Drawer.Backends[idx]
  76. var state = backend.ctx.state;
  77. var width = backend.ctx.width / state.scale.x;
  78. var height = backend.ctx.height / state.scale.y;
  79. var cont = new XMLSerializer().serializeToString(
  80. backend.ctx.svg
  81. )
  82. imgList.push({
  83. img: cont,
  84. width: width,
  85. height: height,
  86. })
  87. }
  88. return imgList
  89. }
  90. } else {
  91. console.log('没有OSMD')
  92. }
  93. }
  94. function render() {
  95. osmd.render();
  96. // console.log(getSvgPngToSize(osmd), 'getSvgPngToSize(osmd)')
  97. window.parent.postMessage({
  98. api: 'musicStaffRender',
  99. loading: false,
  100. osmdImg: getSvgPngToSize(osmd)
  101. }, '*');
  102. }
  103. function renderXml(xmlUrl, isComberRender, canSelectTracks) {
  104. osmd
  105. .load(xmlUrl)
  106. .then(
  107. function () {
  108. // 是否合并显示
  109. // console.log(osmd.Sheet.Instruments, isComberRender, canSelectTracks, 'osmd.Sheet.Instruments')
  110. // if (isComberRender) {
  111. // for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  112. // const trackName = osmd.Sheet.Instruments[i].Name || '';
  113. // console.log(trackName, 'trackName', canSelectTracks.includes(trackName))
  114. // osmd.Sheet.Instruments[i].Visible = canSelectTracks.includes(trackName)
  115. // }
  116. // }
  117. osmd.zoom = .5
  118. render();
  119. }
  120. );
  121. }
  122. function resetRender(partIndex) {
  123. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  124. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  125. }
  126. render();
  127. }
  128. function resetRenderPage(type, xmlUrl) {
  129. let str = 'staff'
  130. if (type === 'first') {
  131. str = 'jianpu'
  132. window.sett = {
  133. keySignature: false,
  134. };
  135. } else if (type === 'fixed') {
  136. str = 'jianpu'
  137. window.sett = {
  138. keySignature: true,
  139. };
  140. }
  141. osmd.EngravingRules.DYMusicScoreType = str
  142. // console.log(type, xmlUrl)
  143. osmd
  144. .load(xmlUrl)
  145. .then(
  146. function () {
  147. // for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  148. // // console.log(osmd.Sheet.Instruments[i].Name);
  149. // osmd.Sheet.Instruments[i].Visible = i === partIndex;
  150. // }
  151. osmd.zoom = .5
  152. render();
  153. }
  154. );
  155. }
  156. </script>
  157. </body>
  158. </html>