code.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var hidden = true;
  2. document.getElementById('show').onclick = function(){
  3. if(hidden) {
  4. document.getElementById('side').style.width = '200px';
  5. } else {
  6. document.getElementById('side').style.width = '0';
  7. }
  8. hidden = !hidden;
  9. };
  10. document.getElementById('full').onclick = function(){
  11. if(!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement)
  12. {
  13. var elem = document.getElementsByTagName('body')[0];
  14. var requestFullScreen = elem.requestFullscreen || elem.msRequestFullscreen || elem.mozRequestFullScreen || elem.webkitRequestFullscreen;
  15. requestFullScreen.call(elem);
  16. }
  17. else {
  18. var exitFullScreen = document.exitFullscreen || document.webkitExitFullscreen || document.mozCancelFullScreen || document.msExitFullscreen;
  19. exitFullScreen.call(document);
  20. }
  21. };
  22. var canvas = document.getElementById('canvas');
  23. var context = canvas.getContext('2d');
  24. function resize() {
  25. canvas.width = window.innerWidth;
  26. canvas.height = window.innerHeight;
  27. }
  28. window.addEventListener('resize', resize);
  29. resize();
  30. var x = 200,
  31. y = 0,
  32. vy = 0,
  33. ay = 0,
  34. m = 0.1, // Ball mass in kg
  35. r = 10, // Ball radius in cm, or pixels.
  36. e = -0.5, // Coefficient of restitution ("bounciness")
  37. rho = 1.2, // Density of air. Try 1000 for water.
  38. C_d = 0.47, // Coeffecient of drag for a ball
  39. A = Math.PI * r * r / 10000 // Frontal area of the ball; divided by 10000 to compensate for the 1px = 1cm relation
  40. ;
  41. var t0 = performance.now();
  42. function loop(t)
  43. {
  44. var dt = (t - t0) / 1000; // delta t, in seconds
  45. t0 = t;
  46. var fy = 0;
  47. /* Weight force, which only affects the y-direction (because that's the direction gravity points). */
  48. fy += m * 9.81;
  49. /* Air resistance force; this would affect both x- and y-directions, but we're only looking at the y-axis in this example. */
  50. fy += -1 * 0.5 * rho * C_d * A * vy * vy;
  51. /* Verlet integration for the y-direction */
  52. dy = vy * dt + (0.5 * ay * dt * dt);
  53. /* The following line is because the math assumes meters but we're assuming 1 cm per pixel, so we need to scale the results */
  54. y += dy * 100;
  55. new_ay = fy / m;
  56. avg_ay = 0.5 * (new_ay + ay);
  57. vy += avg_ay * dt;
  58. /* Let's do very simple collision detection */
  59. if (y + r > canvas.height && vy > 0) {
  60. /* This is a simplification of impulse-momentum collision response. e should be a negative number, which will change the velocity's direction. */
  61. vy *= e;
  62. /* Move the ball back a little bit so it's not still "stuck" in the wall. */
  63. y = canvas.height - r;
  64. }
  65. draw();
  66. window.requestAnimationFrame(loop);
  67. }
  68. function draw() {
  69. context.fillStyle = 'red';
  70. context.clearRect(0, 0, canvas.width, canvas.height);
  71. context.beginPath();
  72. context.arc(x, y, r, 0, Math.PI * 2, true);
  73. context.fill();
  74. context.closePath();
  75. }
  76. window.requestAnimationFrame(loop);