canvas_performance.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!--This file executes some benchmarks with HTML Canvas and VexFlow rendering-->
  2. <!doctype html><html><head><script defer="defer">(function(){//---------------->
  3. //----------------------------------------------------------------------------->
  4. // TODO
  5. // * More complex VexFlow systems
  6. // Init function
  7. window.onload = function() {
  8. document.title = "HTML Canvas Memory Tests";
  9. document.write("<h1>[OSMD] HTML Canvas Memory Tests</h1>");
  10. // Run the tests
  11. repeat(
  12. "Canvas creation", 100,
  13. "create an empty huge 1920 x (1080*30) canvas.",
  14. function(){
  15. var canvas = document.createElement("canvas");
  16. canvas.width = 1920;
  17. canvas.height = 1080 * 30;
  18. }
  19. );
  20. repeat(
  21. "Fill canvas with white", 10,
  22. "create a huge canvas and fill it completely with white.",
  23. function(){
  24. var canvas = document.createElement("canvas");
  25. canvas.width = 1920;
  26. canvas.height = 1080 * 30;
  27. var ctx = canvas.getContext("2d");
  28. ctx.fillRect(0, 0, canvas.width, canvas.height);
  29. }
  30. );
  31. repeat(
  32. "[VexFlow] Draw systems", 100,
  33. "Draw 20 empty staves on a normal (1920x200) canvas (one instrument).",
  34. function(){
  35. var canvas = document.createElement("canvas");
  36. canvas.width = 1920;
  37. canvas.height = 1080;
  38. var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
  39. var ctx = renderer.getContext();
  40. for (var j=0; j < 20; j++)
  41. (new Vex.Flow.Stave(
  42. Math.floor(Math.random()*canvas.width),
  43. Math.floor(Math.random()*canvas.height),
  44. Math.floor(Math.random()*canvas.width)
  45. )).addClef("treble")
  46. .setContext(ctx)
  47. .draw();
  48. }
  49. );
  50. repeat(
  51. "[VexFlow] Draw systems", 100,
  52. "Draw 20 empty staves on a big (1920x1080) canvas (one instrument).",
  53. function(){
  54. var canvas = document.createElement("canvas");
  55. canvas.width = 1920;
  56. canvas.height = 1080;
  57. var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
  58. var ctx = renderer.getContext();
  59. for (var j=0; j < 20; j++)
  60. (new Vex.Flow.Stave(
  61. Math.floor(Math.random()*canvas.width),
  62. Math.floor(Math.random()*canvas.height),
  63. Math.floor(Math.random()*canvas.width)
  64. )).addClef("treble")
  65. .setContext(ctx)
  66. .draw();
  67. }
  68. );
  69. repeat(
  70. "[VexFlow] Format & draw complex system", 50,
  71. "Draw 20 systems with notes, ties and beams on a big canvas (one instrument).",
  72. function(){
  73. var canvas = document.createElement("canvas");
  74. canvas.width = 1920;
  75. canvas.height = 1080;
  76. var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
  77. var ctx = renderer.getContext();
  78. for (var j=0; j < 20; j++) {
  79. var stave = new Vex.Flow.Stave(10, 0, 500);
  80. // Add a treble clef
  81. stave.addClef("treble");
  82. stave.setContext(ctx).draw();
  83. var notes = [
  84. new Vex.Flow.StaveNote({ keys: ["e##/5"], duration: "8d" }).
  85. addAccidental(0, new Vex.Flow.Accidental("##")).addDotToAll(),
  86. new Vex.Flow.StaveNote({ keys: ["b/4"], duration: "16" }).
  87. addAccidental(0, new Vex.Flow.Accidental("b"))
  88. ];
  89. var notes2 = [
  90. new Vex.Flow.StaveNote({ keys: ["b/4"], duration: "8" }),
  91. new Vex.Flow.StaveNote({ keys: ["d/4"], duration: "16" }),
  92. new Vex.Flow.StaveNote({ keys: ["c/4", "e/4"], duration: "16" }).
  93. addAccidental(0, new Vex.Flow.Accidental("b"))
  94. ];
  95. var notes3 = [
  96. new Vex.Flow.StaveNote({ keys: ["c/4", "e/4"], duration: "8" }),
  97. new Vex.Flow.StaveNote({ keys: ["e/4"], duration: "8" }).
  98. addAccidental(0, new Vex.Flow.Accidental("#"))
  99. ];
  100. var notes4 = [
  101. new Vex.Flow.StaveNote({ keys: ["d/4"], duration: "8" }),
  102. new Vex.Flow.StaveNote({ keys: ["e/4"], duration: "8" }),
  103. ];
  104. // Create the beams
  105. var beam = new Vex.Flow.Beam(notes);
  106. var beam2 = new Vex.Flow.Beam(notes2);
  107. var beam3 = new Vex.Flow.Beam(notes3);
  108. var beam4 = new Vex.Flow.Beam(notes4);
  109. // Create a tie between the last note of the first group and the
  110. // first note of the last group.
  111. var tie = new Vex.Flow.StaveTie({
  112. first_note: notes[1],
  113. last_note: notes2[0],
  114. first_indices: [0],
  115. last_indices: [0]
  116. });
  117. // Create another tie between the two chords in the tune
  118. var tie2 = new Vex.Flow.StaveTie({
  119. first_note: notes2[2],
  120. last_note: notes3[0],
  121. first_indices: [0, 1],
  122. last_indices: [0, 1]
  123. });
  124. var all_notes = notes.concat(notes2).concat(notes3).concat(notes4);
  125. // Helper function to justify and draw a 4/4 voice
  126. Vex.Flow.Formatter.FormatAndDraw(ctx, stave, all_notes);
  127. // Render beams
  128. beam.setContext(ctx).draw();
  129. beam2.setContext(ctx).draw();
  130. beam3.setContext(ctx).draw();
  131. beam4.setContext(ctx).draw();
  132. // Render ties
  133. tie.setContext(ctx).draw();
  134. tie2.setContext(ctx).draw();
  135. }
  136. }
  137. );
  138. // Finish
  139. document.close();
  140. }
  141. //----------------------------------------------------------------------------->
  142. // Display the result of a test on the page
  143. function formatResult(label, descr, times, elapsed) {
  144. write("<p><b>" + label + "</b>: " + descr + "<br>");
  145. write("<i>Elapsed time: " + (elapsed/times) + "ms each (" + times + " iterations)</i></Op>");
  146. }
  147. // A simple timer
  148. var timer = function() {
  149. var start = new Date();
  150. return {
  151. stop: function() {
  152. return (new Date()).getTime() - start.getTime();
  153. }
  154. }
  155. }
  156. // Repeat a test
  157. var repeat = function(label, times, descr, func) {
  158. var t = timer();
  159. for (var i = 0; i < times; i++) func();
  160. formatResult(label, descr, times, t.stop());
  161. }
  162. // Write directly on the body
  163. var write = function(x){document.write(x)}
  164. //----------------------------------------------------------------------------->
  165. }());</script><!--------------------------------------------------------------->
  166. <script src="../node_modules/vexflow/releases/vexflow-min.js"></script><!------>
  167. </head></html><!--------------------------------------------------------------->