index.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. </style>
  23. </head>
  24. <body>
  25. <div id="osmdContainer" />
  26. <script>
  27. var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay("osmdContainer");
  28. osmd.setOptions({
  29. backend: "svg",
  30. drawTitle: false,
  31. drawPartNames: true,
  32. drawLyricist: false,
  33. drawComposer: false
  34. // drawingParameters: "compacttight" // don't display title, composer etc., smaller margins
  35. });
  36. function getSvgPngToSize(osmd) {
  37. if (osmd) {
  38. const imgList = ''
  39. if (osmd.Drawer.Backends.length > 0) {
  40. var backend = osmd.Drawer.Backends[0]
  41. var state = backend.ctx.state;
  42. var width = backend.ctx.width / state.scale.x;
  43. var height = backend.ctx.height / state.scale.y;
  44. var cont = new XMLSerializer().serializeToString(
  45. backend.ctx.svg
  46. )
  47. return {
  48. img: cont,
  49. width: width,
  50. height: height
  51. }
  52. }
  53. } else {
  54. console.log('没有OSMD')
  55. }
  56. }
  57. function render() {
  58. osmd.render();
  59. window.parent.postMessage({
  60. api: 'musicStaffRender',
  61. loading: false,
  62. osmdImg: getSvgPngToSize(osmd)
  63. }, '*');
  64. }
  65. function renderXml(xmlUrl, partIndex) {
  66. osmd
  67. .load(xmlUrl)
  68. .then(
  69. function () {
  70. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  71. // console.log(osmd.Sheet.Instruments[i].Name);
  72. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  73. }
  74. osmd.zoom = .5
  75. render();
  76. }
  77. );
  78. }
  79. function resetRender(partIndex) {
  80. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  81. // console.log(osmd.Sheet.Instruments[i].Name);
  82. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  83. }
  84. render();
  85. }
  86. </script>
  87. </body>
  88. </html>