index.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. });
  37. // osmd.EngravingRules.RenderMultipleRestMeasures = true;
  38. // osmd.EngravingRules.CompactMode = true;
  39. // osmd.EngravingRules.PageTopMarginNarrow = 5.0; // for compact mode
  40. // osmd.EngravingRules.PageBottomMargin = 15.0;
  41. // osmd.EngravingRules.DYMusicScoreType =
  42. // state.musicRenderType === EnumMusicRenderType.staff ? "staff" : "jianpu";
  43. // // 如果为固定调,需要加入全局
  44. // if (state.musicRenderType === EnumMusicRenderType.fixedTone) {
  45. // window.sett = {
  46. // keySignature: true,
  47. // };
  48. // }
  49. // debugger
  50. // console.log('osmd.EngravingRules')
  51. // osmd.EngravingRules.StaffDistance = 1.0;
  52. // osmd.EngravingRules.BetweenStaffDistance = 1.0;
  53. // osmd.EngravingRules.MinimumStaffLineDistance = 1.0;
  54. //osmd.EngravingRules.MinSkyBottomDistBetweenStaves = 1.0; // default 1.0. this can cause collisions with slurs and dynamics sometimes
  55. osmd.EngravingRules.MinSkyBottomDistBetweenSystems = 3.0; // default 5.0
  56. // note that osmd.EngravingRules === osmd.rules, since it's passed as a reference
  57. osmd.EngravingRules.MinimumDistanceBetweenSystems = 1;
  58. // osmd.setPageFormat('794x1123')
  59. osmd.setPageFormat('650x884')
  60. function getSvgPngToSize(osmd) {
  61. if (osmd) {
  62. if (osmd.Drawer.Backends.length > 0) {
  63. var imgList = []
  64. for (var idx = 0, len = osmd.Drawer.Backends.length; idx < len; idx++) {
  65. var backend = osmd.Drawer.Backends[idx]
  66. var state = backend.ctx.state;
  67. var width = backend.ctx.width / state.scale.x;
  68. var height = backend.ctx.height / state.scale.y;
  69. var cont = new XMLSerializer().serializeToString(
  70. backend.ctx.svg
  71. )
  72. imgList.push({
  73. img: cont,
  74. width: width,
  75. height: height,
  76. })
  77. }
  78. return imgList
  79. }
  80. } else {
  81. console.log('没有OSMD')
  82. }
  83. }
  84. function render() {
  85. osmd.render();
  86. // console.log(getSvgPngToSize(osmd), 'getSvgPngToSize(osmd)')
  87. window.parent.postMessage({
  88. api: 'musicStaffRender',
  89. loading: false,
  90. osmdImg: getSvgPngToSize(osmd)
  91. }, '*');
  92. }
  93. function renderXml(xmlUrl, partIndex, type) {
  94. if(type) {
  95. let str = 'staff'
  96. if (type === 'first') {
  97. str = 'jianpu'
  98. window.sett = {
  99. keySignature: false,
  100. };
  101. } else if (type === 'fixed') {
  102. str = 'jianpu'
  103. window.sett = {
  104. keySignature: true,
  105. };
  106. }
  107. osmd.EngravingRules.DYMusicScoreType = str
  108. }
  109. osmd
  110. .load(xmlUrl)
  111. .then(
  112. function () {
  113. // for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  114. // // console.log(osmd.Sheet.Instruments[i].Name);
  115. // osmd.Sheet.Instruments[i].Visible = i === partIndex;
  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>