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

Untitled

CSSDeck G+