Advertisement
nzisaacnz

Game loop template with play/pause fading

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