Ajax Loader
×
HTML
<!-- Starting with the basic canvas element -->
1
<!-- Starting with the basic canvas element -->
2
<canvas id="canvas"</canvas>
 
CSS
/* Some basic styles */
1
/* Some basic styles */
2
* {margin: 0; padding: 0; overflow: hidden}
3
 
4
canvas {background: black;}
 
JavaScript
// Adding rAF for smoother animation
1
// Adding rAF for smoother animation
2
window.requestAnimFrame = (function(){
3
  return  window.requestAnimationFrame       || 
4
    window.webkitRequestAnimationFrame || 
5
    window.mozRequestAnimationFrame    || 
6
    window.oRequestAnimationFrame      || 
7
    window.msRequestAnimationFrame     || 
8
    function( callback ){
9
      window.setTimeout(callback, 1000 / 60);
10
    };
11
})();
12
 
13
// Basic canvas initialization
14
var canvas = document.getElementById("canvas"),
15
    ctx = canvas.getContext("2d");
16
 
17
var W = window.innerWidth,
18
    H = window.innerHeight,
19
    text = "Angga kris",
20
    skipCount = 4,
21
    gravity = 0.2,
22
    touched = false,
23
    mouse = {},
24
    minDist = 20,
25
    bounceFactor = 0.6;
26
 
27
canvas.height = H;
28
canvas.width = W;
29
 
30
document.addEventListener("mousemove", trackPos, false);
31
 
32
// We also need the mouse positions
33
function trackPos(e) {
34
  mouse.x = e.pageX;
35
  mouse.y = e.pageY;
36
}
37
 
38
// Creating a class for our particles
39
var Particle = function() {
40
  this.r = Math.random() * 6;
41
  // Initial position will be out of canvas
42
  // but we'll set them later
43
  this.x = -100;
44
  this.y = -100;
45
  
46
  // Lets give each particle a different velocity
47
  this.vy = -5 + parseInt(Math.random() * 10);
48
  this.vx = -5 + parseInt(Math.random() * 10);
49
  
50
  // A flag to inform if the particle is free to fall or not
51
  this.isFree = false;
52
  
53
  this.a = Math.random();
54
  
55
  // Function to draw them
56
  this.draw = function() {
57
    ctx.beginPath();
58
    // Lets add random opacity
59
    ctx.fillStyle = "rgba(255, 223, 0, " +this.a+")";
60
    ctx.arc(this.x, this.y, this.r, 0, Math.PI*2, false);
61
    ctx.fill();
62
    ctx.closePath();
63
  };
64
    
65
  // Finally a function to set particle's function
66
  this.setPos = function(x, y) {
67
    this.x = x;
68
    this.y = y;
69
  }
70
};
71
 
72
// We also need an array where are particles will be
73
var particles = [];
74
 
75
// Lets add some text on the canvas now
76
(function drawText() {
77
  ctx.fillStyle = "black";
78
  ctx.font = "100px Arial, sans-serif";
79
  ctx.textAlign = "center";
80
  ctx.fillText(text, W/2, H/2);
81
})();
82
 
83
// Now, we need to save the positions of black pixels and then 
84
// use these positions to draw the particles
85
(function getPixelPos() {
86
  // Here, we are using the getImageData function in 
87
  // which 3 values are returned. The width and height of
88
  // the image and the pixel data array. The data array is
89
  // width x height x 4 in size where the 4 depicts 4 values
90
  // for each pixel i.e. red, green, blue and alpha (RGBA).
91
  var imageData = ctx.getImageData(0, 0, W, W),
92
      data = imageData.data;
93
  
94
  // We'll now iterate over the data array going through
95
  // rows and columns
96
  // Instead of reading each pixel, we can skip over some
97
  // to increase the performance
98
  for (var i = 0; i < imageData.height; i += skipCount) {
99
    for (var j = 0; j < imageData.width; j += skipCount) {
100
      // The values in the data array rangle from 0 to
101
      // (height x width x 4) - 1 so we'll use that information
102
      // to get the color of each pixel
103
      
104
      var color = data[(j * imageData.width * 4) + (i * 4) - 1];
105
      
106
      // Now if the color is black, we'll do our stuff
107
      if(color == 255) {
108
        particles.push(new Particle());
109
        particles[particles.length - 1].setPos(i, j);
110
      }
111
    }
112
  }
113
})();
114
 
115
function clear() {
116
  ctx.clearRect(0, 0, W, H);
117
}
118
 
119
// Now for a twist, we'll make the particles fall when they 
120
// are hovered by mouse with realistic physics :) GRAVITY FTW!
121
 
122
// We'll do our animation stuff here
123
// Lets see if it works or not, it works! Time for some animation
124
function update() {
125
  clear();
126
  for (i = 0; i < particles.length; i++) {
127
    var p = particles[i];
128
    
129
    // For the burning effect, we'll increase the radius
130
    // of each particles whilst reducing its opacity.
131
    // As soon as the opacity goes below zero, we'll make the
132
    // particle reborn, LOVELY!
133
    p.r += 0.15;
134
    p.a -= 0.015;
135
      
136
    if(p.a < 0) {
137
      p.r = Math.random() * 6;
138
      p.a = Math.random();
139
    }
140
    
141
    // Logic for making them fall on hover 
142
    if(mouse.x > p.x - p.r && 
143
       mouse.x < p.x + p.r &&
144
       mouse.y > p.y - p.r &&
145
       mouse.y < p.y + p.r)
146
      touched = true;
147
    
148
    //console.log(touched); // Working
149
    // We'll also make the nearby particles fall down
150
    // so we will need a minimum distance
151
    // We'll calculate the distance b/w mouse cursor
152
    // and the particles and then compare it with minDist
153
    
154
    if (touched == true) {
155
      var dist = Math.sqrt((p.x-mouse.x)*(p.x-mouse.x) + (p.y-mouse.y)*(p.y-mouse.y));
156
      
157
      if(dist <= minDist) 
158
        p.isFree = true;
159
      
160
      if(p.isFree == true) {
161
        // Add velocities and gravity
162
        p.y += p.vy;
163
        p.x += p.vx;
164
        
165
        // Take a moment and pause the codecast. Try hovering
166
        // particles and they'll fly away because no gravity 
167
        // is present, but it is still a cool effect ;)
168
        
169
        // Now they'll obey the rules of nature
170
        p.vy += gravity;
171
        
172
        // Note that particles go below the floor so we need
173
        // to make them bouncy and make them rebound as they
174
        // hit the floor and walls
175
        if(p.y + p.r > H) {
176
          p.vy *= -bounceFactor;
177
          p.y = H - p.r;
178
          
179
          // We also need a little friction on the floor
180
          // otherwise the particles will keep moving like
181
          // little ants :P
182
          if (p.vx > 0) 
183
            p.vx -= 0.1;
184
          else
185
            p.vx += 0.1;
186
        }
187
        
188
        // The codecast completes here. Try changing the colors
189
        // or size of particles and add your own text keywords!
190
        // Buh-bye :)
191
        
192
        // Collision with walls
193
        if(p.x + p.r > W) {
194
          p.vx *= -bounceFactor;
195
          p.x = W - p.r;
196
        }
197
        
198
        if (p.x - p.r < 0) {
199
          p.vx *= -bounceFactor;
200
          p.x = p.r;
201
        }
202
      }
203
    }
204
    
205
    ctx.globalCompositeOperation = "lighter";
206
    p.draw();
207
  }
208
}
209
 
210
(function animloop(){
211
  requestAnimFrame(animloop);
212
  update();
213
})();
 

angga

CSSDeck G+