-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
executable file
·172 lines (146 loc) · 5.03 KB
/
Particle.js
File metadata and controls
executable file
·172 lines (146 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Create a Particle system on canvas through javascript.
* @author Jatin Mistry
*/
(function() {
// requestAnim shim layer
//window.reqAnimationFrame = (function(){
// return window.requestAnimationFrame ||
// window.webkitRequestAnimationFrame ||
// window.mozRequestAnimationFrame ||
// window.oRequestAnimationFrame ||
// window.msRequestAnimationFrame ||
// function(/* function */ callback, /* DOMElement */ element){
// window.setTimeout(callback, 16);
// };
//})();
// Mouse Move Event Handler
function mouseMove(e){
mousePointer.x = e.layerX;
mousePointer.y = e.layerY;
}
// Mouse Down Event Handler
function mouseDown(e){
var len = particles.length;
var closestIndex = 0;
var closestDistance = 1000;
for( var i = 0; i < len; i++ ){
var thisDistance = DistanceBetween( particles[i], mousePointer );
if( thisDistance < closestDistance ){
closestDistance = thisDistance;
closestIndex = i;
}
}
if (closestDistance < particles[closestIndex].currentSize){
particles[closestIndex].frozen = true;
}
}
// Method to resize the canvas
function resize(){
canvas.width = w = this.innerWidth;
canvas.height = h = this.innerHeight;
}
// Calculate Distance between 2 particles or elements
function DistanceBetween(p1,p2){
var dx = p2.x-p1.x;
var dy = p2.y-p1.y;
return Math.sqrt(dx*dx + dy*dy);
}
// The main animation loop
function animate(){
//debugger;
animationID = requestAnimationFrame( animate );
draw();
}
// Initialize
function init(){
d = document,
canvas = d.body.appendChild(d.createElement('canvas')),
context = canvas.getContext('2d'),
time = 0,
w = canvas.width = this.innerWidth,
h = canvas.height = this.innerHeight,
m = Math,
VELOCITY = 0.5,
PARTICLES = 200,
mousePointer = {x:0, y:0},
particles = [],
colors = [ "#FFFFFF","#FFFF00","#FF0000","#00D8CC","#EB3C00","#FFAD01","#4C944A","#8CC84A","#E81123","#4963B7","#FA6800" ];
for( var i = 0; i < PARTICLES; i++ ){
// create an array of particles with following properties
particles.push( {
x: m.random()*window.innerWidth,
y: m.random()*window.innerHeight,
vx: ((m.random()*(VELOCITY*2))-VELOCITY),
vy: ((m.random()*(VELOCITY*2))-VELOCITY),
size: 1+m.random()*3,
color: colors[ m.floor( m.random() * colors.length ) ]
} );
}
Setup();
}
// Setup the Particle system to accept mouse events and resize.
function Setup(){
canvas.addEventListener('mousemove', mouseMove, false);
window.addEventListener('mousedown', mouseDown, false);
window.addEventListener('resize', resize, false);
resize();
}
// Draw the particle system
function draw(){
// Clear canvas
canvas.width = canvas.width; //Or context.clearRect(0, 0, w, h);
// get the total particles to be drawn
var len = particles.length;
var particle;
for( var i = 0; i < len; i++ ){
particle = particles[i];
//debugger;
if (!particle.frozen){
particle.x += particle.vx;
particle.y += particle.vy;
// Check if particle's x-coordinate is at extreme right of the screen
if (particle.x > window.innerWidth) {
particle.vx = -VELOCITY - Math.random();
}
// Check if particle's x-coordinate is at extreme left of the screen
else if (particle.x < 0) {
particle.vx = VELOCITY + Math.random();
}
// Move the particle in x-direction by some fraction
else {
particle.vx *= 1 + (Math.random() * 0.0005);
}
// Check if particle's y-coordinate is at extreme bottom of the screen
if (particle.y > window.innerHeight) {
particle.vy = -VELOCITY - Math.random();
}
// Check if particle's y-coordinate is at extreme top of the screen
else if (particle.y < 0) {
particle.vy = VELOCITY + Math.random();
}
// Move the particle in y-direction by some fraction
else {
particle.vy *= 1 + (Math.random() * 0.0005);
}
// calculate the distance between the mouse pointer and the particle
var distanceFactor = DistanceBetween( mousePointer, particle );
distanceFactor = Math.max( Math.min( 15 - ( distanceFactor / 10 ), 10 ), 1 );
// set the size of particle depending upon its position from mouse
particle.currentSize = particle.size*distanceFactor;
}
// draw the particle
context.fillStyle = particle.color;
context.beginPath();
context.arc(particle.x,particle.y,particle.currentSize,0,Math.PI*2,true);
context.closePath();
context.fill();
}
}
// Declare variables
var d, canvas, context, time, w, h, m, VELOCITY, PARTICLES, mousePointer, particles, colors, animationID;
// Initialize
init();
// Perform animation
animate();
})();