nzisaacnz

Game loop template with piston

Sep 11th, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.58 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5.  
  6. </style>
  7. <script>
  8.  
  9. </script>
  10. </head>
  11. <body>
  12. <canvas width="800" height="600" id="cvs" onclick="playPause()" onmousemove="mouseMove(event)"></canvas>
  13. <script>
  14.  
  15. var Template = {};
  16. var requestAnimationFrame =
  17. window.requestAnimationFrame||
  18. window.webkitRequestAnimationFrame||
  19. window.mozRequestAnimationFrame||
  20. window.oRequestAnimationFrame||
  21. window.msRequestAnimationFrame;
  22.  
  23. var cancelAnimationFrame =
  24. window.cancelAnimationFrame||
  25. window.webkitCancelRequestAnimationFrame||
  26. window.webkitCancelAnimationFrame||
  27. window.mozCancelRequestAnimationFrame||
  28. window.mozCancelAnimationFrame||
  29. window.oCancelRequestAnimationFrame||
  30. window.oCancelAnimationFrame||
  31. window.msCancelRequestAnimationFrame||
  32. window.msCancelAnimationFrame;
  33.  
  34. Template.newTime = Date.now();
  35. Template.time = 0;
  36. Template.animation = function()
  37. {
  38.  if(!Template.stopped)
  39.  {
  40.   Template.oldTime = Template.newTime;
  41.   if(!Template.paused)
  42.   {
  43.    Template.calculate();
  44.   }
  45.   Template.draw();
  46.   Template.newTime = Date.now();
  47.   Template.time=Template.newTime-Template.oldTime;
  48.   Template.animationFrame = requestAnimationFrame(Template.animation);
  49.  }
  50. }
  51. Template.start = function(d,c)
  52. {
  53.  Template.draw = d;
  54.  Template.calculate = c;
  55.  Template.paused = false;
  56.  Template.animationFrame = requestAnimationFrame(Template.animation);
  57. }
  58. Template.stop = function()
  59. {
  60.  cancelAnimationFrame(Template.animationFrame);
  61. }
  62.  
  63.  
  64. function Transition(start,end,period,loops,func)
  65. {
  66.  this.start = start;
  67.  this.end = end;
  68.  this.period = period;
  69.  if(!func)this.func = function(a){return a;};
  70.  else this.func = func;
  71.  this.pos = 0;
  72.  this.reversed;
  73.  this.loop = loops;
  74.  this.res = 0;
  75.  this.calc=function()
  76.  {
  77.   if(this.reversed) this.pos -= Template.time/this.period;
  78.   else this.pos += Template.time/this.period;
  79.   if(this.loop==0)
  80.   {
  81.    this.pos = (this.pos+1)%1;
  82.   }
  83.   else
  84.   {
  85.    this.pos = Math.min(this.loop,Math.max(this.pos,0));
  86.   }
  87.   this.res = this.func(this.pos)*(end-start)+start;
  88.  }
  89.  this.get = function()
  90.  {
  91.   return this.res;
  92.  }
  93.  this.reverse = function()
  94.  {
  95.   this.reversed = !this.reversed;
  96.  }
  97. }
  98.  
  99. var G,person,mouse,piston;
  100. function init()
  101. {
  102.  G = cvs.getContext("2d");
  103.  piston = [
  104.  new Transition(-50,50,3000,0,function(d){return Math.sin(d*2*Math.PI)*0.5+0.5;}),
  105.  new Transition(-50,50,3000,0,function(d){return Math.cos(d*2*Math.PI)*0.5+0.5;}),
  106.  new Transition(Math.PI,-Math.PI,3000,0)
  107.  ];
  108.  person = {
  109.   x:0,
  110.   y:0,
  111.   w:0,
  112.   h:0
  113.  };
  114.  mouse = {
  115.   x:cvs.width/2,y:cvs.width/2
  116.  };
  117.  Template.start(draw,calc);
  118. }
  119.  
  120. function draw()
  121. {
  122.  person.x = mouse.x;
  123.  person.y = mouse.y;
  124.  person.w = piston[0].get();
  125.  person.h =  piston[1].get();
  126.  G.clearRect(0,0,cvs.width,cvs.height);
  127.  G.beginPath();
  128.  G.moveTo(person.x+person.w,person.y+person.h);
  129.  G.arc(person.x,person.y,50,piston[2].get(),piston[2].get()-Math.PI);
  130.  G.lineTo(person.x+person.w,person.y+person.h);
  131.  G.lineTo(person.x+person.w,person.y-150+person.h);
  132.  G.lineTo(person.x+person.w+0,person.y-100);
  133.  G.lineTo(person.x-100+person.w,person.y-100);
  134.  G.stroke();
  135. }
  136. function calc()
  137. {
  138.  var dx = Math.sqrt(Math.pow(mouse.x,2)+Math.pow(mouse.y,2));
  139.  piston[0].calc();
  140.  piston[1].calc();
  141.  piston[2].calc();
  142.  piston[0].period = dx;
  143.  piston[1].period = dx;
  144.  piston[2].period = dx;
  145. }
  146.  
  147. function playPause()
  148. {
  149.  //Click to pause and reverse
  150.  Template.paused = !Template.paused;
  151.  if(!Template.paused)
  152.  {
  153.  piston[0].reverse();
  154.  piston[1].reverse();
  155.  piston[2].reverse();
  156.  }
  157. }
  158.  
  159. function mouseMove(e)
  160. {
  161.  mouse.x = e.offsetX;
  162.  mouse.y = e.offsetY;
  163. }
  164.  
  165. init();
  166. </script>
  167.  
  168. </body>
  169. </html>
Advertisement
Add Comment
Please, Sign In to add comment