Skip to content

Commit ae24bd4

Browse files
committed
js refactor
1 parent 9c57f07 commit ae24bd4

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

readme.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# canvas-particles
1+
# Canvas Particles
22

3-
##### v1.0.8
4-
5-
A Canvas Particle Library written in vanilla JS.
3+
A Canvas Particle Library written in vanilla JS for use with the canvas HTML5 elemnt.
64

7-
Particle animations on HTML5 Canvas
5+
##### v1.0.8
86

97
## How to Use canvas-particles
108

@@ -18,7 +16,7 @@ Copy just the JS file for the animation you want and place it in your project di
1816
Add a link to the js file from your html file.
1917
Add a canvas element to your html file where you would like the animation to display.
2018

21-
The CSS file remove padding and margin from the body and a background radial gradient which is optional but gives a nice aesthetic
19+
The CSS file removes padding and margin from the body and a background radial gradient which is optional but is included for demo purposes.
2220

2321
## Current Animations
2422

@@ -29,15 +27,16 @@ The CSS file remove padding and margin from the body and a background radial gra
2927
* <strong>Gravity</strong>: Multicolored balls fall down with simulated gravity and friction
3028
* <strong>Wobble Down</strong>: White balls fall down as they wobble on the x-axis
3129
* <strong>Wobble Up</strong>: White balls float up as they wobble on the x-axis
32-
* <strong>Weather</strong>: White balls fly to the right simulating snow weather.
30+
* <strong>Weather Left</strong>: White balls fly to the left simulating snow weather.
31+
* <strong>Weather Right</strong>: White balls fly to the right simulating snow weather.
3332

3433
## Change Log
3534

3635
#### v1.0.8
3736

3837
* Change Weather to Weather Right
3938
* Added Weather Left
40-
* Added Gravity effedct to Weather animations
39+
* Added Gravity effect to Weather animations
4140

4241
#### v1.0.7
4342

src/js/balls.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var colors = [
2222
"#172A40",
2323
"#FFF7DC",
2424
"#D9383A"
25-
]
25+
];
2626

2727
// Mouse coordiantes
2828

@@ -63,6 +63,7 @@ function Circle(x,y,dx,dy,rad,color) {
6363
this.color = color;
6464

6565
// Draw circle function
66+
6667
this.draw = function() {
6768
ctx.beginPath();
6869
ctx.arc(this.x, this.y, this.rad, 0, Math.PI *2, false);
@@ -73,79 +74,97 @@ function Circle(x,y,dx,dy,rad,color) {
7374
};
7475

7576
// Update circle position for animation
77+
7678
this.update = function() {
7779

7880
// Left/Right Collision Detection for window
81+
7982
if (this.x + this.rad > window.innerWidth ||
8083
this.x - this.rad < 0) {
8184
this.dx = -this.dx;
8285
}
8386

8487
// Top/Bottom Collision Detection for window
88+
8589
if (this.y + this.rad > window.innerHeight ||
8690
this.y - this.rad < 0) {
8791
this.dy = -this.dy;
8892
}
8993

9094
// Increment position (x,y)
95+
9196
this.x += this.dx;
9297
this.y += this.dy;
9398

9499
// Interactivity (mouse and circles)
100+
95101
// Mouse detection for circle
102+
96103
if (mouse.x - this.x < 50 && mouse.x - this.x > -50 && mouse.y - this.y < 50 && mouse.y - this.y > -50) {
97104
// Limit circle grow size
98105
if (this.rad < maxRad) {
99106
this.rad += 1;
100107
}
101-
// Limit circle shrink size
108+
109+
// Limit circle shrink size
110+
102111
} else if (this.rad > minRad) {
103112
this.rad -= 1;
104113
} else if (this.rad < this.minRad) {
105114
this.rad += 1;
106115
}
107116

108117
// Draw Circle
118+
109119
this.draw();
110120

111121
};
112122
}
113123

114124
// Create circles array
125+
115126
var circles = [];
116127

117128
function init() {
118129

119130
// Reset circles array
131+
120132
circles = [];
121133

122134
// Randomize circle value (position, velocity, fill and stroke color, and opacity)
123-
for (var i = 0; i < 800; i++) {
135+
136+
for(var i = 0; i < 800; i++) {
124137
var rad = (Math.floor(Math.random() * 4)) + 1;
125138
var x = Math.random() * (window.innerWidth - rad * 2) + rad;
126139
var y = Math.random() * (window.innerHeight -rad * 2) + rad;
127140
var dx = (Math.random() - 0.5) * 3;
128141
var dy = (Math.random() - 0.5) * 3;
129142
var color = colors[Math.floor(Math.random() * colors.length)];
130143
circles.push(new Circle(x,y,dx,dy,rad,color));
131-
132-
console.log(circles);
133144
}
134145
}
135146

136147
// Animation function
148+
137149
function animation() {
150+
138151
//Start loop
152+
139153
requestAnimationFrame(animation);
154+
140155
// Clear window after drawing circle
156+
141157
ctx.clearRect(0,0,window.innerWidth, window.innerHeight);
158+
142159
// Draw the circles
143-
for (var i = 0; i < circles.length; i++) {
160+
161+
for(var i = 0; i < circles.length; i++) {
144162
circles[i].update();
145163
}
146164

147165
}
148166

149167
// Run
168+
150169
animation();
151170
init();

src/js/falldown.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function Circle(x,y,dx,dy,rad,color) {
5252
this.color = color;
5353

5454
// Draw circle function
55+
5556
this.draw = function() {
5657
ctx.beginPath();
5758
ctx.arc(this.x, this.y, this.rad, 0, Math.PI *2, false);
@@ -60,6 +61,7 @@ function Circle(x,y,dx,dy,rad,color) {
6061
};
6162

6263
// Update circle position for animation
64+
6365
this.update = function() {
6466

6567
// Move circle to top once it reaches bottom
@@ -69,64 +71,82 @@ function Circle(x,y,dx,dy,rad,color) {
6971
}
7072

7173
// Increment position (x,y)
74+
7275
this.x += this.dx;
7376
this.y += this.dy;
7477

7578
// Interactivity (mouse and circles)
79+
7680
// Mouse detection for circle
81+
7782
if (mouse.x - this.x < 50 && mouse.x - this.x > -50 && mouse.y - this.y < 50 && mouse.y - this.y > -50) {
83+
7884
// Limit circle grow size
85+
7986
if (this.rad < maxRad) {
8087
this.rad += 1;
8188
}
89+
8290
// Limit circle shrink size
91+
8392
} else if (this.rad > minRad) {
8493
this.rad -= 1;
8594
} else if (this.rad < this.minRad) {
8695
this.rad += 1;
8796
}
8897

8998
// Draw Circle
99+
90100
this.draw();
91101

92102
};
93103
}
94104

95105
// Create circles array
106+
96107
var circles = [];
97108

98109
function init() {
99110

100111
// Reset circles array
112+
101113
circles = [];
102114

103115
// Randomize circle value (position, velocity, fill and stroke color, and opacity)
104-
for (var i = 0; i < 100; i++) {
116+
117+
for(var i = 0; i < 100; i++) {
105118
var rad = (Math.floor(Math.random() * 4)) + 1;
106119
var x = Math.random() * (window.innerWidth - rad * 2) + rad;
107120
var y = Math.random() * (window.innerHeight - rad *2) + rad;
108121
var dx = 0;
109122
var dy = Math.abs((Math.random() - 0.5)) * 2;
110123
var color = "white";
111124
circles.push(new Circle(x,y,dx,dy,rad,color));
112-
113-
console.log(circles);
114125
}
126+
115127
}
116128

117129
// Animation function
130+
118131
function animation() {
132+
119133
//Start loop
134+
120135
requestAnimationFrame(animation);
136+
121137
// Clear window after drawing circle
138+
122139
ctx.clearRect(0,0,window.innerWidth, window.innerHeight);
140+
123141
// Draw the circles
124-
for (var i = 0; i < circles.length; i++) {
142+
143+
for(var i = 0; i < circles.length; i++) {
125144
circles[i].update();
126145
}
127146

128147
}
129148

130-
// Run
149+
// Run
150+
131151
animation();
132152
init();

src/js/wobbleup.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ function Circle(x,y,dx,dy,rad,color, boundaryRight, boundaryLeft) {
5353
this.dx = dx;
5454
this.dy = dy;
5555
this.rad = rad;
56+
this.color = color;
5657
this.boundaryRight = boundaryRight;
5758
this.boundaryLeft = boundaryLeft;
5859
var minRad = rad;
5960
var maxRad = (rad * 2);
60-
this.color = color;
6161

6262
// Draw circle function
6363

6464
this.draw = function() {
6565
ctx.beginPath();
66-
ctx.arc(this.x, this.y, this.rad, 0, Math.PI *2, false);
66+
ctx.arc(this.x, this.y, this.rad, 0, Math.PI *2);
6767
ctx.fillStyle = this.color;
6868
ctx.fill();
6969
};
@@ -94,6 +94,7 @@ function Circle(x,y,dx,dy,rad,color, boundaryRight, boundaryLeft) {
9494
if (mouse.x - this.x < 50 && mouse.x - this.x > -50 && mouse.y - this.y < 50 && mouse.y - this.y > -50) {
9595

9696
// Limit circle grow size
97+
9798
if (this.rad < maxRad) {
9899
this.rad += 1;
99100
}
@@ -139,7 +140,6 @@ function init() {
139140
circles.push(new Circle(x,y,dx,dy,rad,color,boundaryRight,boundaryLeft));
140141
}
141142

142-
143143
}
144144

145145
// Animation function
@@ -156,7 +156,7 @@ function animation() {
156156

157157
// Draw the circles
158158

159-
for (var i = 0; i < circles.length; i++) {
159+
for(var i = 0; i < circles.length; i++) {
160160
circles[i].update();
161161
}
162162

0 commit comments

Comments
 (0)