index.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, partIndex, isComberRender) {
  104. osmd
  105. .load(xmlUrl)
  106. .then(
  107. function () {
  108. // 是否合并显示
  109. // if (!isComberRender) {
  110. // for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  111. // osmd.Sheet.Instruments[i].Visible = i === partIndex;
  112. // }
  113. // }
  114. osmd.zoom = .5
  115. render();
  116. }
  117. );
  118. }
  119. function resetRender(partIndex) {
  120. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  121. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  122. }
  123. render();
  124. }
  125. function resetRenderPage(type, xmlUrl) {
  126. let str = 'staff'
  127. if (type === 'first') {
  128. str = 'jianpu'
  129. window.sett = {
  130. keySignature: false,
  131. };
  132. } else if (type === 'fixed') {
  133. str = 'jianpu'
  134. window.sett = {
  135. keySignature: true,
  136. };
  137. }
  138. osmd.EngravingRules.DYMusicScoreType = str
  139. // console.log(type, xmlUrl)
  140. osmd
  141. .load(xmlUrl)
  142. .then(
  143. function () {
  144. // for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  145. // // console.log(osmd.Sheet.Instruments[i].Name);
  146. // osmd.Sheet.Instruments[i].Visible = i === partIndex;
  147. // }
  148. osmd.zoom = .5
  149. render();
  150. }
  151. );
  152. }
  153. </script>
  154. </body>
  155. </html>