index.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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) {
  94. osmd
  95. .load(xmlUrl)
  96. .then(
  97. function () {
  98. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  99. // console.log(osmd.Sheet.Instruments[i].Name);
  100. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  101. }
  102. osmd.zoom = .5
  103. render();
  104. }
  105. );
  106. }
  107. function resetRender(partIndex) {
  108. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  109. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  110. }
  111. render();
  112. }
  113. function resetRenderPage(type, xmlUrl) {
  114. let str = 'staff'
  115. if (type === 'first') {
  116. str = 'jianpu'
  117. window.sett = {
  118. keySignature: false,
  119. };
  120. } else if (type === 'fixed') {
  121. str = 'jianpu'
  122. window.sett = {
  123. keySignature: true,
  124. };
  125. }
  126. osmd.EngravingRules.DYMusicScoreType = str
  127. // console.log(type, xmlUrl)
  128. osmd
  129. .load(xmlUrl)
  130. .then(
  131. function () {
  132. // for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  133. // // console.log(osmd.Sheet.Instruments[i].Name);
  134. // osmd.Sheet.Instruments[i].Visible = i === partIndex;
  135. // }
  136. osmd.zoom = .5
  137. render();
  138. }
  139. );
  140. }
  141. </script>
  142. </body>
  143. </html>