index.js 10 KB

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