Welcome to JS Bin
Load cached copy from
12
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta name="description" content="Simple animation tests" />
5
<meta charset=utf-8 />
6
<title>Simple Animation Tests</title>
7
</head>
8
<body>
9
  <canvas id="canvas"></canvas>
10
</body>
11
</html>
12
  
4
 
1
body, html {
2
  height: 100%;
3
  margin: 0;
4
}
62
 
1
var canvas = document.getElementById('canvas'),
2
    ctx = canvas.getContext('2d'),
3
    HEIGHT = window.innerHeight,
4
    WIDTH = window.innerWidth,
5
    TO_RADIANS = Math.PI / 360;
6
    
7
var raf = (function(){
8
  return  window.requestAnimationFrame       || 
9
          window.webkitRequestAnimationFrame || 
10
          window.mozRequestAnimationFrame    || 
11
          window.oRequestAnimationFrame      || 
12
          window.msRequestAnimationFrame     || 
13
          function( callback ){
14
            window.setTimeout(callback, 1000 / 60);
15
          };
16
})();
17
18
function clear(style) {
19
  ctx.save();
20
  ctx.fillStyle = style || 'rgba(0,0,0,0.01)';
21
  ctx.fillRect(0, 0, WIDTH, HEIGHT);
22
  ctx.restore();
23
}
24
25
function square(x, y, w, h) {
26
  ctx.beginPath();
27
  ctx.rect(WIDTH/2 + x, HEIGHT/2 + y, w, h);
28
  ctx.closePath();
29
  ctx.fill();
30
}
31
32
function circle(x, y, w, h) {
33
  ctx.beginPath();
34
  ctx.arc(WIDTH/2 + x, HEIGHT/2+ y, w, 0, Math.PI*2, true);
35
  ctx.closePath();
36
  ctx.fill();
37
}
38
39
canvas.width = WIDTH;
40
canvas.height = HEIGHT;
41
42
var offsetX = 0,
43
    offsetY = 0;
44
45
function draw() {
46
  clear();
47
  
48
  offsetX+= 2;
49
  offsetY++;
50
  var x = Math.sin(WIDTH + offsetX * TO_RADIANS) * WIDTH/4;
51
  var y = Math.sin(HEIGHT + offsetY * TO_RADIANS) * HEIGHT/4;
52
  
53
  ctx.fillStyle = 'cyan';
54
  circle(x, y, 2, 10);
55
//   offsetX += 2;
56
  
57
  
58
  raf(draw, 16);
59
}
60
61
clear('black');
62
draw();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x