Ajax Loader
×

Untitled

CSS
body {margin: 0; padding: 0; overflow: hidden}
1
body {margin: 0; padding: 0; overflow: hidden}
2
canvas {width: 100%; height: 100%};
 
JavaScript
// rAF
1
// rAF
2
window.requestAnimationFrame = function() {
3
  return window.requestAnimationFrame ||
4
    window.webkitRequestAnimationFrame ||
5
    window.mozRequestAnimationFrame ||
6
    window.msRequestAnimationFrame ||
7
    window.oRequestAnimationFrame ||
8
    function(f) {
9
      window.setTimeout(f,1e3/60);
10
    }
11
}();
12
 
13
var canvas = document.createElement('canvas');
14
document.body.appendChild(canvas);
15
 
16
var ctx = canvas.getContext('2d'),
17
    window_width = window.innerWidth,
18
    window_height = window.innerHeight,
19
    gravity = 0.2;
20
 
21
canvas.width = window_width;
22
canvas.height = window_height;
23
 
24
var platform = {
25
  x: null,
26
  y: null,
27
  width: null,
28
  height: null,
29
  color: null,
30
  
31
  init: function() {
32
    platform.width = 200;
33
    platform.height = 5;
34
    platform.color = 'red';
35
    platform.x = (window_width / 2) - (platform.width / 2);
36
    platform.y = (window_height - 50);
37
  },
38
  
39
  draw: function() {
40
    ctx.fillStyle = platform.color;
41
    ctx.fillRect(platform.x, platform.y, platform.width, platform.height);
42
  }
43
}
44
 
45
var ball = {
46
  x: null,
47
  y: null,
48
  radius: null,
49
  color: null,
50
  vy: null,
51
  
52
  init: function() {
53
    ball.radius = 10;
54
    ball.color = 'blue';
55
    ball.x = (window_width / 2);
56
    ball.y = 50;
57
    ball.vy = 20;
58
  },
59
  
60
  draw: function() {
61
    ctx.beginPath();
62
    ctx.fillStyle = ball.color;
63
    ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI*2, false);
64
    ctx.fill();
65
    ctx.closePath();
66
  }
67
}
68
 
69
function paintCanvas() {
70
  ctx.clearRect(0, 0, window_width, window_height);
71
}
72
 
73
function init() {
74
  platform.init();
75
  ball.init();
76
}
77
 
78
var delta, 
79
    now, 
80
    then = new Date().getTime(),
81
    fps = 30,
82
    interval = 1000/fps;
83
 
84
function f2T(Delta, Speed) {
85
    return (Speed * Delta) * (60 / 1000); 
86
}
87
 
88
function update() {
89
  fps = 30 + Math.random() * 30;
90
  interval = 1000/fps;
91
  
92
  now = new Date().getTime();
93
  delta = now - then; 
94
  
95
  if(delta > interval) {
96
    then = now;
97
    paintCanvas();
98
    
99
    // Draw the platform
100
    platform.draw();
101
    
102
    // Move the ball
103
    ball.y += f2T(delta, ball.vy);
104
    ball.vy += f2T(delta, gravity);
105
    ball.draw();
106
    
107
    var speed = ball.vy + gravity, 
108
      distance = platform.y - (ball.y + ball.radius),
109
        time = distance / speed;
110
    
111
    if(time > 0 && time < f2T(delta, 1)) {
112
      ball.y = platform.y - ball.radius;
113
      if (ball.vy > 0)
114
        ball.vy *= - 0.4;
115
    }
116
  }
117
}
118
 
119
function animloop() {
120
  requestAnimationFrame(animloop);
121
  update();
122
}
123
 
124
init();
125
animloop();
 

Untitled

CSSDeck G+