demo.js 8.4 KB

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