nzisaacnz

Animation Creation v0.1

Nov 10th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 10.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  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.  
  24. function GameLoop(draw,calc)
  25. {
  26.  var newTime = Date.now();
  27.  var time = 0;
  28.  var calculate = calc||function(){};
  29.  var draw = calc||function(){};
  30.  var stopped = false;
  31.  var paused = false;
  32.  var animation= function()
  33.  {
  34.   if(!stopped)
  35.   {
  36.    oldTime = newTime;
  37.    if(!paused)
  38.    {
  39.     calculate();
  40.    }
  41.    draw();
  42.    newTime = Date.now();
  43.    time=this.newTime-this.oldTime;
  44.    animationFrame = requestAnimationFrame(animation);
  45.   }
  46.  };
  47.  this.start = function(d,c)
  48.  {
  49.   if(d)draw = d;
  50.   if(c)calculate = c;
  51.   paused = false;
  52.   animationFrame = requestAnimationFrame(animation);
  53.  };
  54.  this.stop = function()
  55.  {
  56.   cancelAnimationFrame(animationFrame);
  57.  };
  58. }
  59.  
  60. </script>
  61. <script>
  62. function MouseHandle(node,MEF,obj)
  63. {
  64.  var events = "mousedown|mousemove|mouseout|mouseover|mouseup|mousewheel|click|dblclick".split("|");
  65.  this.pos = {start:false,current:false,end:false};
  66.  this.stage = -1;
  67.  this.down = false;
  68.  this.obj = obj;
  69.  this.MEF = MEF;
  70.  for(var a=0; a<events.length; a++)
  71. {
  72.  node["on"+events[a]] = this.MEF;
  73. }
  74. node.Handle = {};
  75. for(var a in this)
  76. {
  77.  node.Handle[a] = this[a];
  78. }
  79. }
  80. function MouseArgs(params)
  81. {
  82. var events = "mousedown|mousemove|mouseout|mouseover|mouseup|mousewheel|click|dblclick".split("|");
  83. this.start = new Point(-1,-1);
  84. this.end = new Point(-1,-1);
  85. this.down = false;
  86. this.obj = params;
  87. this.timestamp = {};
  88. for(var a in events)
  89. {
  90.  this.timestamp[a] = false;
  91. }
  92. }
  93. /* Example Function
  94. function meHandler(event)
  95. {
  96. switch(event.type)
  97. {
  98.  case "mousewheel":
  99.  
  100.  break;
  101.  case "mousedown":
  102.  
  103.  break;
  104.  case "mousemove":
  105.  
  106.  break;
  107.  case "mouseup":
  108.  
  109.  break;
  110.  case "mouseover":
  111.  
  112.  break;
  113.  case "mouseout":
  114.  
  115.  break;
  116.  case "click":
  117.  
  118.  break;
  119.  case "dblclick":
  120.  
  121.  break;
  122. }
  123. }
  124. */
  125. </script>
  126. <script>
  127. function View()
  128. {
  129.  this.pos = {x:0,y:0};
  130.  this.Z = 0;
  131.  this.zoom = 1;
  132.  this.scale = 1.1;
  133.  this.Zoom = function(delta,x,y)
  134.  {
  135.   var X = x-this.pos.x;
  136.   var Y = y-this.pos.y;
  137.   var scale = this.scale;
  138.   if(delta>0) this.Z++;
  139.   else
  140.   {
  141.    this.Z--;
  142.    scale = 1/scale;
  143.   }
  144.   this.zoom = Math.pow(this.scale, this.Z);
  145.   this.pos.x+=X-scale*X;
  146.   this.pos.y+=Y-scale*Y;
  147.  }
  148. }
  149. </script>
  150. <style>
  151. *
  152. {
  153.  font-family:Calibri;
  154. }
  155. #canvas,#display
  156. {
  157.  position:absolute;
  158.  width:50%;
  159.  top:0px;
  160.  bottom:0px;
  161.  border:1px solid;
  162. }
  163. #canvas{left:0px;}
  164. #display{right:0px;}
  165. canvas
  166. {
  167.  background-image:url('opaque.png');
  168.  background-size:10px 10px;
  169. }
  170. body,#canvas,#display
  171. {
  172.  min-height:850px;
  173. }
  174.  
  175. #keyFrames
  176. {
  177.  overflow-y:scroll;
  178.  overflow-x:hidden;
  179.  position:relative;
  180.  width:100%;
  181.  height:auto;
  182.  background-color:#EEE;
  183.  text-align:right;
  184. }
  185. .keyFrame
  186. {
  187.  position:absolute;
  188.  top:inherit;height:inherit;
  189.  border-left:1px solid #000;
  190.  -webkit-user-select:none;
  191. }
  192. .animation
  193. {
  194.  height:20px;
  195.  border-top:1px solid;
  196.  width:100%;
  197. }
  198. #framePos
  199. {
  200.  align:center;
  201.  z-index:1000000000000;
  202.  position:absolute;
  203.  left:0%;
  204.  top:0;
  205.  height:100%;
  206.  width:2px;
  207.  background-color:#000;
  208.  cursor:pointer;
  209. }
  210. #tridown
  211. {
  212.  width:0px;
  213.  height:0px;
  214.  border-left:5px solid transparent;
  215.  border-right:5px solid transparent;
  216.  border-top:20px solid #00F;
  217.  margin-left:-4px;
  218.  margin-top:-2px;
  219.  opacity:0.5;
  220.  cursor:inherit;
  221. }
  222. #square
  223. {
  224.  width:20px;
  225.  height:20px;
  226.  background-color:#00F;
  227.  margin-left:-9px;
  228.  border-radius:0px 0px 10px 10px;
  229.  opacity:0.5;
  230. }
  231. </style>
  232. </head>
  233. <body>
  234. <div id="canvas">
  235. <table width="100%" height="100%">
  236. <tr align="center">
  237. <td valign="center">Canvas<br>
  238. <canvas id="cvs" width="800" height="600"></canvas>
  239.  
  240. <div id="keyFrames" unselectable onselectstart="event.preventDefault();return false;">
  241. Animation Length<input type="number" min="1" value="10000" oninput="ChangeAniLength(this.value)"/>
  242. <div id="frameDisp"><br><br></div>
  243. <div id="framePos" title="Frame {0}"><div id="square"></div><div id="tridown"></div></div>
  244.  
  245. </div>
  246. <script src="animation.js"></script>
  247. <script>
  248. var sprite =
  249. {
  250.  PointA:[0,250,777,1003,1500,2500,2600,2700,2800,2900,3000,4000,4500,4750,6000,6100,6110,6500,7000,8000,9000],
  251.  PointB:[0,1000,2000,3000,4000,5000,6000,7000,8000,9000],
  252.  PointC:[0,250,777,1003,1500,2500,2600,2700,2800,2900,3000,4000,4500,4750,6000,6100,6110,6500,7000,8000,9000],
  253.  PointD:[0,1000,2000,3000,4000,5000,6000,7000,8000,9000],
  254.  PointE:[0,250,777,1003,1500,2500,2600,2700,2800,2900,3000,4000,4500,4750,6000,6100,6110,6500,7000,8000,9000],
  255.  PointF:[0,1000,2000,3000,4000,5000,6000,7000,8000,9000]
  256. }
  257. for(var s in sprite)
  258. {
  259.  var a = AddAnimation(sprite,s);
  260.  a.title = s;
  261.  for(var b=0; b<sprite[s].length; b++)
  262. {
  263.  AddKeyFrame(sprite[s][b],a,sprite,s,b)
  264. }
  265. }
  266. </script>
  267.  
  268. </td>
  269. </tr>
  270. </table>
  271. </div>
  272.  
  273. </div>
  274.  
  275.  
  276. <div id="display">
  277. <div style="margin-top:-100px;">
  278. <table width="100%" height="100%">
  279. <tr align="center">
  280. <td valign="center">Preview<br>
  281. <canvas id="preview" width="800" height="600"></canvas>
  282. </td>
  283. </tr>
  284. </table>
  285. </div>
  286. </body>
  287. <script>
  288. function Point(x,y,width)
  289. {
  290.  this.width=width||3;
  291.  this.x = x;
  292.  this.y = y;
  293.  this.draw = function(G,view)
  294.  {
  295.   G.beginPath();
  296.   G.moveTo(this.x*view.zoom+view.pos.x+this.width,this.y*view.zoom+view.pos.y);
  297.   G.arc(this.x*view.zoom+view.pos.x,this.y*view.zoom+view.pos.y,this.width,0,2*Math.PI);
  298.   G.stroke();
  299.  }
  300.  this.draw2 = function(G)
  301.  {
  302.   G.beginPath();
  303.   G.moveTo(this.x+this.width,this.y);
  304.   G.arc(this.x,this.y,this.width,0,2*Math.PI);
  305.   G.stroke();
  306.  }
  307. }
  308.  
  309. function Animation(start,end,type,params)
  310. {
  311.  for(var a in params)
  312.  {
  313.   this[a] = params[a];
  314.  }
  315.  this.aniLen = end-start;
  316.  this.start = start;
  317.  this.type = type;
  318.  this.calc = function(time)
  319.  {
  320.   var t = ((time-this.start)/this.aniLen)%1;
  321.   var dx = 0;
  322.   var dy = 0;
  323.   if(this.point)
  324.   {
  325.    dx = (this.point.end.x-this.point.start.x);
  326.    dy = (this.point.end.y-this.point.start.y);
  327.   }
  328.   switch(this.type)
  329.   {
  330.    case Animation.Linear:
  331.     return new Point(this.point.start.x+t*dx,this.point.start.y+t*dy);
  332.    break;
  333.    case Animation.Sine:
  334.     var dt = Math.sin(t*Math.PI*2)*0.5+0.5;
  335.     return new Point(this.point.start.x+dt*dx,this.point.start.y+dt*dy);
  336.    break;
  337.    case Animation.Cosine:
  338.     var dt = Math.cos(t*Math.PI*2)*0.5+0.5;
  339.     return new Point(this.point.start.x+dt*dx,this.point.start.y+dt*dy);
  340.    break;
  341.    case Animation.Tangent:
  342.     var dt = Math.tan(t*Math.PI*2)*0.5+0.5;
  343.     return new Point(this.point.start.x+dt*dx,this.point.start.y+dt*dy);
  344.    break;
  345.    case Animation.Bezier:
  346.     var t1 = 1-t;
  347.     var pt=[t1*t1*t1,t1*t1,t1,1];
  348.     var tt=[1,3*t,3*t*t,t*t*t];
  349.     for(var a=0; a<4; a++)
  350.    {
  351.     dx+=pt[a]*tt[a]*this.points[a].x;
  352.     dy+=pt[a]*tt[a]*this.points[a].y;
  353.    }
  354.    return new Point(dx,dy);
  355.   break;
  356.   case Animation.Other:
  357.    return this.Function(t);
  358.   break;
  359.  }
  360. }
  361. this.update = function(params)
  362. {
  363.  for(var a in params)
  364.  {
  365.   this[a] = params[a];
  366.  }
  367. }
  368. }
  369. Animation.Linear=0;
  370. Animation.Sine=1;
  371. Animation.Cosine=2;
  372. Animation.Tangent=3;
  373. Animation.Bezier=4;
  374. Animation.Other=5;
  375.  
  376.  
  377. function CanvasMouseEvent(event)
  378. {
  379. this.Handle.pos.current = new Point(event.offsetX,event.offsetY);
  380. switch(event.type)
  381. {
  382.  case "mousewheel":
  383.   event.preventDefault();
  384.   this.Handle.obj.view.Zoom(event.wheelDelta,this.Handle.pos.current.x,this.Handle.pos.current.y);
  385.   redraw();
  386.  break;
  387.  case "mousedown":
  388.   this.Handle.pos.start = new Point(event.offsetX-this.Handle.obj.view.pos.x,event.offsetY-this.Handle.obj.view.pos.y);
  389.   this.Handle.pos.end = new Point(event.offsetX-this.Handle.obj.view.pos.x,event.offsetY-this.Handle.obj.view.pos.y);
  390.   this.Handle.down = true;
  391.   event.preventDefault();
  392.  break;
  393.  case "mousemove":
  394.   if(this.Handle.down)
  395.   {
  396.    this.Handle.obj.view.pos.x = this.Handle.pos.current.x-this.Handle.pos.start.x;
  397.    this.Handle.obj.view.pos.y = this.Handle.pos.current.y-this.Handle.pos.start.y;
  398.    this.Handle.pos.end = new Point(event.offsetX,event.offsetY);
  399.    redraw();
  400.   }
  401.  break;
  402.  case "mouseup":
  403.   this.Handle.down = false;
  404.   this.Handle.pos.end = new Point(event.offsetX,event.offsetY);
  405.  break;
  406. }
  407. }
  408.  
  409.  
  410. function PreviewMouseEvent(event)
  411. {
  412. this.Handle.pos.current = new Point(event.offsetX,event.offsetY);
  413. switch(event.type)
  414. {
  415.  case "mousewheel":
  416.   event.preventDefault();
  417.   this.Handle.obj.view.Zoom(event.wheelDelta,this.Handle.pos.current.x,this.Handle.pos.current.y);
  418.  break;
  419.  case "mousedown":
  420.   this.Handle.pos.start = new Point(event.offsetX-this.Handle.obj.view.pos.x,event.offsetY-this.Handle.obj.view.pos.y);
  421.   this.Handle.pos.end = new Point(event.offsetX-this.Handle.obj.view.pos.x,event.offsetY-this.Handle.obj.view.pos.y);
  422.   this.Handle.down = true;
  423.   event.preventDefault();
  424.  break;
  425.  case "mousemove":
  426.   if(this.Handle.down)
  427.   {
  428.    this.Handle.obj.view.pos.x = this.Handle.pos.current.x-this.Handle.pos.start.x;
  429.    this.Handle.obj.view.pos.y = this.Handle.pos.current.y-this.Handle.pos.start.y;
  430.    this.Handle.pos.end = new Point(event.offsetX,event.offsetY);
  431.   }
  432.  break;
  433.  case "mouseup":
  434.   this.Handle.down = false;
  435.   this.Handle.pos.end = new Point(event.offsetX,event.offsetY);
  436.  break;
  437. }
  438. }
  439.  
  440.  
  441. function Part(start,end,type,params)
  442. {
  443. for(var a in params)
  444. {
  445.  this[a] = params[a];
  446. }
  447. this.anim = new Animation(start,end,type,params);
  448. this.update = function(params)
  449. {
  450.  for(var a in params)
  451.  {
  452.   this[a] = params[a];
  453.  }
  454. }
  455. this.calc = function(time,view)
  456. {
  457.  var raw = this.anim.calc(time);
  458.  return new Point(raw.x*view.zoom+view.pos.x,raw.y*view.zoom+view.pos.y);
  459. }
  460. }
  461. function Sprite()
  462. {
  463. this.update = function(params)
  464. {
  465.  for(var a in params)
  466.  {
  467.   this[a] = params[a];
  468.  }
  469. }
  470. }
  471.  
  472. function redraw()
  473. {
  474. t = frame.value/frame.max;
  475. var G = cvs.getContext("2d");
  476. G.clearRect(0,0,cvs.width,cvs.height);
  477. }
  478.  
  479. var totalTime = 0;
  480. function dispDraw()
  481. {
  482. var G = preview.getContext("2d");
  483. G.clearRect(0,0,cvs.width,cvs.height);
  484. totalTime+=Template.time;
  485. }
  486.  
  487. function dispCalc()
  488. {
  489.  
  490. }
  491.  
  492. var view = new View();
  493. var preView = new View();
  494. var cHandle = new MouseHandle(cvs,CanvasMouseEvent,{view:view});
  495. var pHandle = new MouseHandle(preview,PreviewMouseEvent,{view:preView});
  496. var Template = new GameLoop();
  497.  
  498.  
  499. redraw();
  500. Template.start(dispDraw,dispCalc);
  501.  
  502. </script>
  503. </body>
  504. </html>
Advertisement
Add Comment
Please, Sign In to add comment