demo.js 7.3 KB

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