index.html 4.6 KB

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