Guest User

Untitled

a guest
May 27th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Canvas Performance Demo</title>
  6. </head>
  7. <body>
  8.  
  9. <div>
  10. <h1>Canvas Performance Test</h1>
  11. <label for="num_boxes">Boxes:</label>
  12. <input type="text" id="num_boxes" value="1000"><br>
  13. <label for="test_time">Test Time (in milliseconds):</label>
  14. <input type="text" id="test_time" value="10000"><br>
  15. <button id="run_test">Run test</button>
  16. </div>
  17.  
  18. <script type="text/javascript">
  19.  
  20. var demo = {};
  21.  
  22. demo.rand = function demo_rand (min, max) {
  23. return (Math.round(Math.random() * (max - min)) + min);
  24. };
  25.  
  26. demo.makeBox = function demo_makeBox (width, height, color, speed) {
  27. var dir_x = (demo.rand(1, 2) === 1) ? -1: 1;
  28. var dir_y = (demo.rand(1, 2) === 1) ? -1: 1;
  29. return {
  30. x: 0,
  31. y: 0,
  32. width: width,
  33. height: height,
  34. color: color,
  35. speed: speed,
  36. direction: {
  37. x: dir_x,
  38. y: dir_y
  39. }
  40. };
  41. };
  42.  
  43. demo.drawBox = function demo_drawBox (ctx, box) {
  44. ctx.save();
  45. ctx.fillStyle = box.color;
  46. ctx.fillRect(parseInt(box.x), parseInt(box.y), box.width, box.height);
  47. ctx.restore();
  48. };
  49.  
  50. var view = {
  51. width: 640,
  52. height: 480
  53. };
  54.  
  55. var canvas = document.createElement("canvas");
  56. canvas.width = view.width;
  57. canvas.height = view.height;
  58. document.body.appendChild(canvas);
  59.  
  60. var ctx = canvas.getContext("2d");
  61.  
  62. ctx.save();
  63. ctx.fillStyle = "rgb(0, 0, 0)";
  64. ctx.fillRect(0, 0, view.width, view.height);
  65. ctx.restore();
  66.  
  67. demo.run = function (box_count, time) {
  68.  
  69. ctx.save();
  70. ctx.fillStyle = "rgb(0, 0, 0)";
  71. ctx.fillRect(0, 0, view.width, view.height);
  72. ctx.restore();
  73.  
  74. // Create some boxes
  75. var boxes = [];
  76. var num_boxes = box_count;
  77. for (var x = 0; x < num_boxes; x++) {
  78. var color = "rgb("
  79. + demo.rand(0, 255) + ", "
  80. + demo.rand(0, 255) + ", "
  81. + demo.rand(0, 255) + ")";
  82. var box = demo.makeBox(10, 10, color, demo.rand(50, 100));
  83. box.x = demo.rand(0, view.width - box.width);
  84. box.y = demo.rand(0, view.height - box.height);
  85. boxes.push(box);
  86. }
  87.  
  88. var last_update = 0;
  89. var elapsed_log = [];
  90.  
  91. var update = function update () {
  92. var now = new Date().getTime();
  93. elapsed = now - last_update;
  94. elapsed_log.push(elapsed);
  95. last_update = now;
  96.  
  97. ctx.fillStyle = "rgb(0, 0, 0)";
  98. ctx.fillRect(0, 0, view.width, view.height);
  99.  
  100. // Move boxes and check collision
  101. for (var x = 0; x < boxes.length; x++) {
  102. var b = boxes[x];
  103. var pixels = ((b.speed / 1000) * elapsed);
  104. b.x += (pixels * b.direction.x);
  105. b.y += (pixels * b.direction.y);
  106. if (b.x > (view.width - b.width)) {
  107. b.x = (view.width - b.width);
  108. b.direction.x = -1;
  109. }
  110. if (b.x < 0) {
  111. b.x = 0;
  112. b.direction.x = 1;
  113. }
  114. if (b.y > (view.height - b.height)) {
  115. b.y = (view.height - b.height);
  116. b.direction.y = -1;
  117. }
  118. if (b.y < 0) {
  119. b.y = 0;
  120. b.direction.y = 1;
  121. }
  122. demo.drawBox(ctx, b);
  123. }
  124.  
  125. };
  126.  
  127. last_update = new Date().getTime();
  128. var interval_id = window.setInterval(update, 0);
  129.  
  130. window.setTimeout(function () {
  131.  
  132. window.clearInterval(interval_id);
  133.  
  134. var sum = 0;
  135. for (var x in elapsed_log) {
  136. sum += elapsed_log[x];
  137. }
  138. var avg = Math.round(sum / elapsed_log.length);
  139.  
  140. ctx.save();
  141. ctx.fillStyle = "rgba(0,0,0,0.75)";
  142. ctx.fillRect(0, 0, view.width, 30);
  143. ctx.restore();
  144.  
  145. ctx.save();
  146. ctx.fillStyle = "rgb(255, 255, 255)";
  147. ctx.font = "20px Courier";
  148. ctx.fillText("Test complete. Average FPS: " + Math.round(1000 / avg), 5, 20);
  149. ctx.restore();
  150.  
  151. }, time);
  152. }
  153.  
  154. var run_button = document.getElementById("run_test");
  155. run_button.addEventListener("click", function () {
  156. var num_boxes = document.getElementById("num_boxes").value;
  157. var test_time = document.getElementById("test_time").value;
  158. demo.run(num_boxes, test_time);
  159. }, true);
  160.  
  161. </script>
  162.  
  163. </body>
  164. </html>
Add Comment
Please, Sign In to add comment