index.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. });
  35. osmd.setPageFormat('650x884')
  36. function getSvgPngToSize(osmd) {
  37. if (osmd) {
  38. if (osmd.Drawer.Backends.length > 0) {
  39. var imgList = []
  40. for (var idx = 0, len = osmd.Drawer.Backends.length; idx < len; idx++) {
  41. var backend = osmd.Drawer.Backends[idx]
  42. var state = backend.ctx.state;
  43. var width = backend.ctx.width / state.scale.x;
  44. var height = backend.ctx.height / state.scale.y;
  45. var cont = new XMLSerializer().serializeToString(
  46. backend.ctx.svg
  47. )
  48. imgList.push({
  49. img: cont,
  50. width: width,
  51. height: height,
  52. })
  53. }
  54. return imgList
  55. }
  56. } else {
  57. console.log('没有OSMD')
  58. }
  59. }
  60. function render() {
  61. osmd.render();
  62. // console.log(getSvgPngToSize(osmd), 'getSvgPngToSize(osmd)')
  63. window.parent.postMessage({
  64. api: 'musicStaffRender',
  65. loading: false,
  66. osmdImg: getSvgPngToSize(osmd)
  67. }, '*');
  68. }
  69. function renderXml(xmlUrl, partIndex) {
  70. osmd
  71. .load(xmlUrl)
  72. .then(
  73. function () {
  74. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  75. // console.log(osmd.Sheet.Instruments[i].Name);
  76. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  77. }
  78. osmd.zoom = .5
  79. render();
  80. }
  81. );
  82. }
  83. function resetRender(partIndex) {
  84. for (let i = 0; i < osmd.Sheet.Instruments.length; i++) {
  85. // console.log(osmd.Sheet.Instruments[i].Name);
  86. osmd.Sheet.Instruments[i].Visible = i === partIndex;
  87. }
  88. render();
  89. }
  90. </script>
  91. </body>
  92. </html>