index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { OpenSheetMusicDisplay } from '../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay';
  2. /*jslint browser:true */
  3. (function () {
  4. "use strict";
  5. var openSheetMusicDisplay;
  6. // The folder of the demo files
  7. var folder = process.env.STATIC_FILES_SUBFOLDER ? process.env.STATIC_FILES_SUBFOLDER + "/" : "",
  8. // The available demos
  9. demos = {
  10. "Beethoven - An die ferne Geliebte": "Beethoven_AnDieFerneGeliebte.xml",
  11. "M. Clementi - Sonatina Op.36 No.1 Pt.1": "MuzioClementi_SonatinaOpus36No1_Part1.xml",
  12. "M. Clementi - Sonatina Op.36 No.1 Pt.2": "MuzioClementi_SonatinaOpus36No1_Part2.xml",
  13. "M. Clementi - Sonatina Op.36 No.3 Pt.1": "MuzioClementi_SonatinaOpus36No3_Part1.xml",
  14. "M. Clementi - Sonatina Op.36 No.3 Pt.2": "MuzioClementi_SonatinaOpus36No3_Part2.xml",
  15. "J.S. Bach - Praeludium In C Dur BWV846 1": "JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml",
  16. "J.S. Bach - Air": "JohannSebastianBach_Air.xml",
  17. "C. Gounod - Meditation": "CharlesGounod_Meditation.xml",
  18. "J. Haydn - Concertante Cello": "JosephHaydn_ConcertanteCello.xml",
  19. "Mozart - An Chloe": "Mozart_AnChloe.xml",
  20. "Mozart - Das Veilchen": "Mozart_DasVeilchen.xml",
  21. "Mozart - Trio": "MozartTrio.mxl",
  22. "S. Joplin - Elite Syncopations": "ScottJoplin_EliteSyncopations.xml",
  23. "S. Joplin - The Entertainer": "ScottJoplin_The_Entertainer.xml",
  24. "ActorPreludeSample": "ActorPreludeSample.xml",
  25. "R. Schumann - Dichterliebe": "Dichterliebe01.xml",
  26. "C. Debussy - Mandoline": "Debussy_Mandoline.xml",
  27. "France Levasseur - Parlez Mois": "Parlez-moi.mxl",
  28. "Telemann - Sonate-Nr.1.1-Dolce": "TelemannWV40.102_Sonate-Nr.1.1-Dolce.xml",
  29. "Telemann - Sonate-Nr.1.2-Allegro": "TelemannWV40.102_Sonate-Nr.1.2-Allegro-F-Dur.xml",
  30. "Saltarello": "Saltarello.mxl",
  31. "OSMD Function Test - All": "OSMD_function_test_all.mxl",
  32. "OSMD Function Test - Grace Notes": "OSMD_function_test_GraceNotes.mxl",
  33. },
  34. zoom = 1.0,
  35. // HTML Elements in the page
  36. err,
  37. error_tr,
  38. canvas,
  39. select,
  40. selectBounding,
  41. skylineDebug,
  42. bottomlineDebug,
  43. zoomIn,
  44. zoomOut,
  45. zoomDiv,
  46. custom,
  47. nextCursorBtn,
  48. resetCursorBtn,
  49. showCursorBtn,
  50. hideCursorBtn,
  51. backendSelect;
  52. // Initialization code
  53. function init() {
  54. var name, option;
  55. err = document.getElementById("error-td");
  56. error_tr = document.getElementById("error-tr");
  57. zoomDiv = document.getElementById("zoom-str");
  58. custom = document.createElement("option");
  59. select = document.getElementById("select");
  60. selectBounding = document.getElementById("selectBounding");
  61. skylineDebug = document.getElementById("skylineDebug");
  62. bottomlineDebug = document.getElementById("bottomlineDebug");
  63. zoomIn = document.getElementById("zoom-in-btn");
  64. zoomOut = document.getElementById("zoom-out-btn");
  65. canvas = document.createElement("div");
  66. nextCursorBtn = document.getElementById("next-cursor-btn");
  67. resetCursorBtn = document.getElementById("reset-cursor-btn");
  68. showCursorBtn = document.getElementById("show-cursor-btn");
  69. hideCursorBtn = document.getElementById("hide-cursor-btn");
  70. backendSelect = document.getElementById("backend-select");
  71. // Hide error
  72. error();
  73. // Create select
  74. for (name in demos) {
  75. if (demos.hasOwnProperty(name)) {
  76. option = document.createElement("option");
  77. option.value = demos[name];
  78. option.textContent = name;
  79. }
  80. select.appendChild(option);
  81. }
  82. select.onchange = selectOnChange;
  83. selectBounding.onchange = selectBoundingOnChange;
  84. // Pre-select default music piece
  85. custom.appendChild(document.createTextNode("Custom"));
  86. // Create zoom controls
  87. zoomIn.onclick = function () {
  88. zoom *= 1.2;
  89. scale();
  90. };
  91. zoomOut.onclick = function () {
  92. zoom /= 1.2;
  93. scale();
  94. };
  95. skylineDebug.onclick = function() {
  96. openSheetMusicDisplay.DrawSkyLine = !openSheetMusicDisplay.DrawSkyLine;
  97. }
  98. bottomlineDebug .onclick = function() {
  99. openSheetMusicDisplay.DrawBottomLine = !openSheetMusicDisplay.DrawBottomLine;
  100. }
  101. // Create OSMD object and canvas
  102. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, backendSelect.value);
  103. openSheetMusicDisplay.setLogLevel('info');
  104. document.body.appendChild(canvas);
  105. // Set resize event handler
  106. new Resize(
  107. function(){
  108. disable();
  109. },
  110. function() {
  111. var width = document.body.clientWidth;
  112. canvas.width = width;
  113. try {
  114. openSheetMusicDisplay.render();
  115. } catch (e) {}
  116. enable();
  117. }
  118. );
  119. window.addEventListener("keydown", function(e) {
  120. var event = window.event ? window.event : e;
  121. if (event.keyCode === 39) {
  122. openSheetMusicDisplay.cursor.next();
  123. }
  124. });
  125. nextCursorBtn.addEventListener("click", function() {
  126. openSheetMusicDisplay.cursor.next();
  127. });
  128. resetCursorBtn.addEventListener("click", function() {
  129. openSheetMusicDisplay.cursor.reset();
  130. });
  131. hideCursorBtn.addEventListener("click", function() {
  132. openSheetMusicDisplay.cursor.hide();
  133. });
  134. showCursorBtn.addEventListener("click", function() {
  135. openSheetMusicDisplay.cursor.show();
  136. });
  137. backendSelect.addEventListener("change", function(e) {
  138. var value = e.target.value;
  139. // clears the canvas element
  140. canvas.innerHTML = "";
  141. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, value);
  142. openSheetMusicDisplay.setLogLevel('info');
  143. selectOnChange();
  144. });
  145. }
  146. function Resize(startCallback, endCallback) {
  147. var rtime;
  148. var timeout = false;
  149. var delta = 200;
  150. function resizeEnd() {
  151. timeout = window.clearTimeout(timeout);
  152. if (new Date() - rtime < delta) {
  153. timeout = setTimeout(resizeEnd, delta);
  154. } else {
  155. endCallback();
  156. }
  157. }
  158. window.addEventListener("resize", function () {
  159. rtime = new Date();
  160. if (!timeout) {
  161. startCallback();
  162. rtime = new Date();
  163. timeout = window.setTimeout(resizeEnd, delta);
  164. }
  165. });
  166. window.setTimeout(startCallback, 0);
  167. window.setTimeout(endCallback, 1);
  168. }
  169. function selectBoundingOnChange(evt) {
  170. var value = evt.target.value;
  171. openSheetMusicDisplay.DrawBoundingBox = value;
  172. }
  173. function selectOnChange(str) {
  174. error();
  175. disable();
  176. var isCustom = typeof str === "string";
  177. if (!isCustom) {
  178. str = folder + select.value;
  179. }
  180. zoom = 1.0;
  181. openSheetMusicDisplay.load(str).then(
  182. function() {
  183. return openSheetMusicDisplay.render();
  184. },
  185. function(e) {
  186. console.warn(e.stack);
  187. error("Error reading sheet: " + e);
  188. }
  189. ).then(
  190. function() {
  191. return onLoadingEnd(isCustom);
  192. }, function(e) {
  193. console.warn(e.stack);
  194. error("Error rendering sheet: " + process.env.DEBUG ? e.stack : e);
  195. onLoadingEnd(isCustom);
  196. }
  197. );
  198. }
  199. function onLoadingEnd(isCustom) {
  200. // Remove option from select
  201. if (!isCustom && custom.parentElement === select) {
  202. select.removeChild(custom);
  203. }
  204. // Enable controls again
  205. enable();
  206. }
  207. function logCanvasSize() {
  208. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  209. }
  210. function scale() {
  211. disable();
  212. window.setTimeout(function(){
  213. openSheetMusicDisplay.zoom = zoom;
  214. openSheetMusicDisplay.render();
  215. enable();
  216. }, 0);
  217. }
  218. function error(errString) {
  219. if (!errString) {
  220. error_tr.style.display = "none";
  221. } else {
  222. err.textContent = errString;
  223. error_tr.style.display = "";
  224. canvas.width = canvas.height = 0;
  225. enable();
  226. }
  227. }
  228. // Enable/Disable Controls
  229. function disable() {
  230. document.body.style.opacity = 0.3;
  231. select.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  232. }
  233. function enable() {
  234. document.body.style.opacity = 1;
  235. select.disabled = zoomIn.disabled = zoomOut.disabled = "";
  236. logCanvasSize();
  237. }
  238. // Register events: load, drag&drop
  239. window.addEventListener("load", function() {
  240. init();
  241. selectOnChange();
  242. });
  243. window.addEventListener("dragenter", function(event) {
  244. event.preventDefault();
  245. disable();
  246. });
  247. window.addEventListener("dragover", function(event) {
  248. event.preventDefault();
  249. });
  250. window.addEventListener("dragleave", function(event) {
  251. enable();
  252. });
  253. window.addEventListener("drop", function(event) {
  254. event.preventDefault();
  255. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  256. return;
  257. }
  258. // Add "Custom..." score
  259. select.appendChild(custom);
  260. custom.selected = "selected";
  261. // Read dragged file
  262. var reader = new FileReader();
  263. reader.onload = function (res) {
  264. selectOnChange(res.target.result);
  265. };
  266. if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".xml") > 0) {
  267. reader.readAsText(event.dataTransfer.files[0]);
  268. }
  269. else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  270. reader.readAsBinaryString(event.dataTransfer.files[0]);
  271. }
  272. else {
  273. alert("No vaild .xml/.mxl file!");
  274. }
  275. });
  276. }());