Ajax Loader
×
HTML
<canvas></canvas>
1
<canvas></canvas>
 
CSS
html, body {
1
html, body {
2
  width: 100%; height: 100%;
3
  background: #333;
4
}
5
 
6
canvas {
7
  display: block;
8
}
 
JavaScript
(function() {
1
(function() {
2
 
3
  // rAF
4
  window.requestAnimationFrame = function() {
5
    return window.requestAnimationFrame ||
6
      window.webkitRequestAnimationFrame ||
7
      window.mozRequestAnimationFrame ||
8
      window.msRequestAnimationFrame ||
9
      window.oRequestAnimationFrame ||
10
      function(f) {
11
        window.setTimeout(f,1e3/60);
12
      }
13
  }();
14
  
15
  var canvas = document.querySelector('canvas');
16
  var ctx = canvas.getContext('2d');
17
  
18
  var W = canvas.width = window.innerWidth;
19
  var H = canvas.height = window.innerHeight;
20
  
21
  // Our SpaceShip Constructor
22
  var SpaceShip = function() {
23
    
24
    // x/y positions
25
    this.x = 200;
26
    this.y = 100;
27
    
28
    this.radius = 40;
29
    
30
    // How many wings/blades or whatever you want to call it
31
    this.wing_count = 3;
32
    // Math.PI*2 === 360 degrees
33
    // 360 / 3 = 120 degrees
34
    // So every 120 degrees will have 1 hand/blade
35
    // of 60 degrees (half of 120)
36
    this.steps = Math.PI*2 / this.wing_count;
37
    
38
    this.color = 'hsl(0,100%,50%)';
39
    // We can try changing color after EVERY rotation
40
    // would be cool!
41
    this.hue = 0; // initial hue
42
    // This is the target hue we'll reach
43
    // during (at start) the first rotation
44
    this.hue_target = parseInt( Math.random()*360 );
45
    
46
    // Lets mess with the angle now
47
    this.angle = 0;
48
    // Manipulating speed - seems good!
49
    this.rotation_speed = 0.06;
50
    
51
    this.draw = function(ctx) {
52
      
53
      // But we want to do this after every rotation
54
      if (this.angle > Math.PI*2) {
55
        // Kind of reset the angle when its more
56
        // than 360 degrees.
57
        this.angle -= Math.PI*2;
58
        
59
        // Choose a random hue target now
60
        this.hue_target = parseInt( Math.random()*360 );
61
        
62
        // ... and we made it :)
63
      }
64
      
65
      // You notice initially there's a color change ?
66
      // Why this (hue_target - hue) * random_numer ?
67
      // Well, we basically measure the distance that we
68
      // have to travel, from initial to target and multiple
69
      // that by an "easing" factor.
70
      // This causes "easing" effect when changing from initial
71
      // to target color, i.e., it starts with high speed
72
      // and as it progresses, the spead decreases until
73
      // it reaches the target.
74
      // We just learnt how to implement Easing in html5 canvas!
75
      this.hue += (this.hue_target - this.hue) * 0.05;
76
      this.color = 'hsl(' + this.hue + ',100%,50%)';
77
    
78
      ctx.strokeStyle = this.color;
79
      // Setting a line width
80
      ctx.lineWidth = 15;
81
      
82
      // Time to rotate this thing
83
      // Since ctx.rotate() will rotate the entire 2D context
84
      // and not just the arc's, we'll have to save
85
      // our current drawing state into the stack.
86
      ctx.save();
87
      
88
      // as you can see entire context was rotated
89
      // we'll fix this by first translating
90
      // The x/y values that we pass to translate()
91
      // is the same as the ones we passed to ctx.arc below
92
      ctx.translate(this.x, this.y);
93
      
94
      // The entire thing became a circle :P
95
      // cuz we never cleared after each rendering of frame
96
      this.angle += this.rotation_speed;
97
      ctx.rotate(this.angle);
98
      
99
      for (var i = 0; i < this.wing_count; i++) {
100
        ctx.beginPath();
101
        
102
        // For arc, the new x/y pos will be 0,0
103
        ctx.arc(
104
          0,
105
          0,
106
          this.radius,
107
          i*this.steps,
108
          i*this.steps + this.steps/2,
109
          false
110
        );
111
        
112
        ctx.stroke();
113
        ctx.closePath();
114
      }
115
      
116
      // ... and restore here
117
      ctx.restore();
118
      
119
      // A rounded body in the center
120
      ctx.beginPath();
121
      ctx.fillStyle = 'black';
122
      ctx.arc(this.x, this.y, 20, 0, Math.PI*2, false);
123
      ctx.fill();
124
      ctx.closePath();
125
    
126
    };
127
  };
128
  
129
  // Create a spaceship object
130
  var ship = new SpaceShip();
131
  // Setting proper x/y to center it
132
  ship.x = W/2;
133
  ship.y = H/2;
134
  
135
  (function renderFrame() {
136
    window.requestAnimationFrame(renderFrame);
137
    
138
    // Magik! We dont need so much speed though...
139
    ctx.clearRect(0,0,W,H);
140
    
141
    ship.draw(ctx);
142
  }());
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
 
160
}());
 

Untitled

CSSDeck G+