demo.js 8.3 KB

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