demo.js 7.5 KB

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