index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.xml",
  32. "OSMD Function Test - Grace Notes": "OSMD_function_test_GraceNotes.xml",
  33. "OSMD Function Test - Ornaments": "OSMD_function_test_Ornaments.xml",
  34. },
  35. zoom = 1.0,
  36. // HTML Elements in the page
  37. err,
  38. error_tr,
  39. canvas,
  40. select,
  41. selectBounding,
  42. skylineDebug,
  43. bottomlineDebug,
  44. zoomIn,
  45. zoomOut,
  46. zoomDiv,
  47. custom,
  48. nextCursorBtn,
  49. resetCursorBtn,
  50. showCursorBtn,
  51. hideCursorBtn,
  52. backendSelect;
  53. // Initialization code
  54. function init() {
  55. var name, option;
  56. err = document.getElementById("error-td");
  57. error_tr = document.getElementById("error-tr");
  58. zoomDiv = document.getElementById("zoom-str");
  59. custom = document.createElement("option");
  60. select = document.getElementById("select");
  61. selectBounding = document.getElementById("selectBounding");
  62. skylineDebug = document.getElementById("skylineDebug");
  63. bottomlineDebug = document.getElementById("bottomlineDebug");
  64. zoomIn = document.getElementById("zoom-in-btn");
  65. zoomOut = document.getElementById("zoom-out-btn");
  66. canvas = document.createElement("div");
  67. nextCursorBtn = document.getElementById("next-cursor-btn");
  68. resetCursorBtn = document.getElementById("reset-cursor-btn");
  69. showCursorBtn = document.getElementById("show-cursor-btn");
  70. hideCursorBtn = document.getElementById("hide-cursor-btn");
  71. backendSelect = document.getElementById("backend-select");
  72. // Hide error
  73. error();
  74. // Create select
  75. for (name in demos) {
  76. if (demos.hasOwnProperty(name)) {
  77. option = document.createElement("option");
  78. option.value = demos[name];
  79. option.textContent = name;
  80. }
  81. select.appendChild(option);
  82. }
  83. select.onchange = selectOnChange;
  84. selectBounding.onchange = selectBoundingOnChange;
  85. // Pre-select default music piece
  86. custom.appendChild(document.createTextNode("Custom"));
  87. // Create zoom controls
  88. zoomIn.onclick = function () {
  89. zoom *= 1.2;
  90. scale();
  91. };
  92. zoomOut.onclick = function () {
  93. zoom /= 1.2;
  94. scale();
  95. };
  96. skylineDebug.onclick = function() {
  97. openSheetMusicDisplay.DrawSkyLine = !openSheetMusicDisplay.DrawSkyLine;
  98. }
  99. bottomlineDebug .onclick = function() {
  100. openSheetMusicDisplay.DrawBottomLine = !openSheetMusicDisplay.DrawBottomLine;
  101. }
  102. // Create OSMD object and canvas
  103. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, backendSelect.value);
  104. openSheetMusicDisplay.setLogLevel('info');
  105. document.body.appendChild(canvas);
  106. // Set resize event handler
  107. new Resize(
  108. function(){
  109. disable();
  110. },
  111. function() {
  112. var width = document.body.clientWidth;
  113. canvas.width = width;
  114. try {
  115. openSheetMusicDisplay.render();
  116. } catch (e) {}
  117. enable();
  118. }
  119. );
  120. window.addEventListener("keydown", function(e) {
  121. var event = window.event ? window.event : e;
  122. if (event.keyCode === 39) {
  123. openSheetMusicDisplay.cursor.next();
  124. }
  125. });
  126. nextCursorBtn.addEventListener("click", function() {
  127. openSheetMusicDisplay.cursor.next();
  128. });
  129. resetCursorBtn.addEventListener("click", function() {
  130. openSheetMusicDisplay.cursor.reset();
  131. });
  132. hideCursorBtn.addEventListener("click", function() {
  133. openSheetMusicDisplay.cursor.hide();
  134. });
  135. showCursorBtn.addEventListener("click", function() {
  136. openSheetMusicDisplay.cursor.show();
  137. });
  138. backendSelect.addEventListener("change", function(e) {
  139. var value = e.target.value;
  140. // clears the canvas element
  141. canvas.innerHTML = "";
  142. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, value);
  143. openSheetMusicDisplay.setLogLevel('info');
  144. selectOnChange();
  145. });
  146. }
  147. function Resize(startCallback, endCallback) {
  148. var rtime;
  149. var timeout = false;
  150. var delta = 200;
  151. function resizeEnd() {
  152. timeout = window.clearTimeout(timeout);
  153. if (new Date() - rtime < delta) {
  154. timeout = setTimeout(resizeEnd, delta);
  155. } else {
  156. endCallback();
  157. }
  158. }
  159. window.addEventListener("resize", function () {
  160. rtime = new Date();
  161. if (!timeout) {
  162. startCallback();
  163. rtime = new Date();
  164. timeout = window.setTimeout(resizeEnd, delta);
  165. }
  166. });
  167. window.setTimeout(startCallback, 0);
  168. window.setTimeout(endCallback, 1);
  169. }
  170. function selectBoundingOnChange(evt) {
  171. var value = evt.target.value;
  172. openSheetMusicDisplay.DrawBoundingBox = value;
  173. }
  174. function selectOnChange(str) {
  175. error();
  176. disable();
  177. var isCustom = typeof str === "string";
  178. if (!isCustom) {
  179. str = folder + select.value;
  180. }
  181. zoom = 1.0;
  182. openSheetMusicDisplay.load(str).then(
  183. function() {
  184. return openSheetMusicDisplay.render();
  185. },
  186. function(e) {
  187. console.warn(e.stack);
  188. error("Error reading sheet: " + e);
  189. }
  190. ).then(
  191. function() {
  192. return onLoadingEnd(isCustom);
  193. }, function(e) {
  194. console.warn(e.stack);
  195. error("Error rendering sheet: " + process.env.DEBUG ? e.stack : e);
  196. onLoadingEnd(isCustom);
  197. }
  198. );
  199. }
  200. function onLoadingEnd(isCustom) {
  201. // Remove option from select
  202. if (!isCustom && custom.parentElement === select) {
  203. select.removeChild(custom);
  204. }
  205. // Enable controls again
  206. enable();
  207. }
  208. function logCanvasSize() {
  209. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  210. }
  211. function scale() {
  212. disable();
  213. window.setTimeout(function(){
  214. openSheetMusicDisplay.zoom = zoom;
  215. openSheetMusicDisplay.render();
  216. enable();
  217. }, 0);
  218. }
  219. function error(errString) {
  220. if (!errString) {
  221. error_tr.style.display = "none";
  222. } else {
  223. err.textContent = errString;
  224. error_tr.style.display = "";
  225. canvas.width = canvas.height = 0;
  226. enable();
  227. }
  228. }
  229. // Enable/Disable Controls
  230. function disable() {
  231. document.body.style.opacity = 0.3;
  232. select.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  233. }
  234. function enable() {
  235. document.body.style.opacity = 1;
  236. select.disabled = zoomIn.disabled = zoomOut.disabled = "";
  237. logCanvasSize();
  238. }
  239. // Register events: load, drag&drop
  240. window.addEventListener("load", function() {
  241. init();
  242. selectOnChange();
  243. });
  244. window.addEventListener("dragenter", function(event) {
  245. event.preventDefault();
  246. disable();
  247. });
  248. window.addEventListener("dragover", function(event) {
  249. event.preventDefault();
  250. });
  251. window.addEventListener("dragleave", function(event) {
  252. enable();
  253. });
  254. window.addEventListener("drop", function(event) {
  255. event.preventDefault();
  256. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  257. return;
  258. }
  259. // Add "Custom..." score
  260. select.appendChild(custom);
  261. custom.selected = "selected";
  262. // Read dragged file
  263. var reader = new FileReader();
  264. reader.onload = function (res) {
  265. selectOnChange(res.target.result);
  266. };
  267. var filename = event.dataTransfer.files[0].name;
  268. if (filename.toLowerCase().indexOf(".xml") > 0
  269. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  270. reader.readAsText(event.dataTransfer.files[0]);
  271. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  272. reader.readAsBinaryString(event.dataTransfer.files[0]);
  273. }
  274. else {
  275. alert("No vaild .xml/.mxl/.musicxml file!");
  276. }
  277. });
  278. }());