Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.getElementById("c");
  2. var ctx = canvas.getContext("2d");
  3. var mouse = {};
  4.  
  5. function getMousePos(canvas, evt) {
  6.     var rect = canvas.getBoundingClientRect();
  7.     return {
  8.         x: evt.clientX - rect.left,
  9.         y: evt.clientY - rect.top
  10.     };
  11. };
  12.  
  13. canvas.addEventListener('mousemove', function(evt) {
  14.     mouse = getMousePos(canvas, evt);
  15.     console.log("e");
  16. }, false);
  17.  
  18.  
  19.  
  20. CONFIG = {
  21.     debug: false
  22. };
  23.  
  24.  
  25. BOX = function(){
  26.     this.x = 0;
  27.     this.y = 0;
  28.     this.width = 100;
  29.     this.height = 100;
  30.  
  31. };
  32.  
  33. BOX.prototype = {
  34.  
  35.     move: function(x,y,delta){
  36.     if (CONFIG.debug)
  37.         console.log("BOX:: move()");
  38.  
  39.     this.x = x * delta;
  40.     this.y = y * delta;
  41.     },
  42.  
  43.     render:  function(){
  44.         if (CONFIG.debug)
  45.             console.log("BOX:: render()");
  46.  
  47.         if (CONFIG.debug)
  48.             console.log(this.x,this.y,this.width,this.height);
  49.  
  50.         ctx.beginPath();
  51.         ctx.rect(this.x,this.y,this.width,this.height);
  52.         ctx.stroke();
  53.     }
  54. };
  55.  
  56.  
  57. FRAME = function() {
  58.     this.elements = [];
  59. };
  60.  
  61. FRAME.prototype = {
  62.     add: function(obj){
  63.         this.elements.push(obj);
  64.     },
  65.     clear: function(){
  66.         ctx.clearRect(0, 0, canvas.width, canvas.height);
  67.     },
  68.  
  69.     step: function(delta){
  70.         this.clear();
  71.  
  72.         for(var i=0; i < this.elements.length; i++){
  73.  
  74.             if( this.elements[i].render){
  75.                 this.elements[i].move(mouse.x, mouse.y,delta);
  76.                 this.elements[i].render();
  77.             }
  78.         }
  79.     }
  80. };
  81.  
  82.  
  83. ANIMATION = {
  84.     lastTick : null,
  85.     currentTick : null,
  86.     elem: null,
  87.  
  88.     init: function(){
  89.         this.elem = new FRAME();
  90.         this.elem.add(new BOX());
  91.  
  92.         ANIMATION.lastTick = null;
  93.         ANIMATION.currentTick = null;
  94.  
  95.         if (CONFIG.debug)
  96.             console.log("ANIMATION:: init()");
  97.     },
  98.  
  99.     step: function(){
  100.             if (ANIMATION.lastTick == null) {
  101.                 if (CONFIG.debug)
  102.                     console.log("ANIMATION:: set lastTick: first Tickness");
  103.                
  104.                 ANIMATION.lastTick = Date.now();
  105.             }
  106.  
  107.         ANIMATION.currentTick = Date.now();
  108.         var delta =  (ANIMATION.lastTick - ANIMATION.currentTick);
  109.         ANIMATION.lastTick = ANIMATION.currentTick;
  110.  
  111.         this.elem.step(delta);
  112.     }
  113. };
  114.  
  115.  
  116. function init(){
  117.  
  118.     ANIMATION.init();
  119.  
  120. };
  121.  
  122.  
  123. function renderFrame(){
  124.  
  125.     ANIMATION.step();
  126.     renderFrame();
  127.     window.requestAnimationFrame(renderFrame);
  128. };
  129.  
  130.  
  131. window.onload = function () {
  132.  
  133.  
  134.     init();
  135.     renderFrame();
  136. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement