-
Notifications
You must be signed in to change notification settings - Fork 0
/
live-brush.html
181 lines (156 loc) · 3.93 KB
/
live-brush.html
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
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/addons/p5.dom.min.js"></script>
<script>
// A fork from Justin Chambers - by Ystudio
// 03/2020
var particles = [];
var nums;
var particleDensity = 4000;
var noiseScale = 700;
var maxLife = 10;
var simulationSpeed = 1/2;
var fadeFrame = 0;
var backgroundColor;
var visualMode = 2;
var numModes = 4;
var invertColors = false;
function setup(){
createCanvas(windowWidth,windowHeight);
nums = width * height / particleDensity;
backgroundColor = color(30, 30, 30);
background(backgroundColor);
for(var i = 0; i < nums; i++){
particles[i] = new Particle();
}
}
function draw(){
noStroke();
++fadeFrame;
if(fadeFrame % 5 == 0){
if(invertColors){
blendMode(ADD);
} else {
blendMode(DIFFERENCE);
}
fill(1, 1, 1);
rect(0,0,width,height);
if(invertColors){
blendMode(DARKEST);
} else {
blendMode(LIGHTEST);
}
fill(backgroundColor);
rect(0,0,width,height);
}
blendMode(BLEND);
smooth();
for(var i = 0; i < nums; i++){
var iterations = map(i,0,nums,5,1);
var radius = map(i,0,nums,2,6);
particles[i].move(iterations);
particles[i].checkEdge();
var alpha = 255;
var particleColor;
var fadeRatio;
fadeRatio = min(particles[i].life * 5 / maxLife, 01);
fadeRatio = min((maxLife - particles[i].life) * 5 / maxLife, fadeRatio);
var colorCase = visualMode;
if(visualMode == 0)
{
colorCase = int(particles[i].pos.x / width * 3) + 1;
}
switch(colorCase)
{
case 1:
var lifeRatioGrayscale = min(255, (255 * particles[i].life / maxLife) + red(backgroundColor));
particleColor = color(lifeRatioGrayscale, alpha * fadeRatio);
break;
case 2:
particleColor = particles[i].color;
break;
case 3:
particleColor = color(blue(particles[i].color) + 70, green(particles[i].color) + 20, red(particles[i].color) - 50);
break;
}
if(invertColors){
particleColor = color(255 - red(particleColor), 255 - green(particleColor), 255 - blue(particleColor));
}
fill(red(particleColor), green(particleColor), blue(particleColor), alpha * fadeRatio);
particles[i].display(radius);
}
}
function Particle(){
// member properties and initialization
this.vel = createVector(0, 0);
this.pos = createVector(random(0, width), random(0, height));
this.life = random(0, maxLife);
this.flip = int(random(0,2)) * 2 - 1;
var randColor = int(random(0,1));
switch(randColor)
{
case 0:
this.color = color(110,57,204);
break;
case 1:
this.color = color(7,153,242);
break;
case 2:
this.color = color(255,255,255);
break;
}
// member functions
this.move = function(iterations){
if((this.life -= 0.4666) < 0)
this.respawn();
while(iterations > 0){
var angle = noise(this.pos.x/noiseScale, this.pos.y/noiseScale)*TWO_PI*noiseScale*this.flip;
this.vel.x = cos(angle);
this.vel.y = sin(angle);
this.vel.mult(simulationSpeed);
this.pos.add(this.vel);
--iterations;
}
}
this.checkEdge = function(){
if(this.pos.x > width || this.pos.x < 0 || this.pos.y > height || this.pos.y < 0){
this.respawn();
}
}
this.respawn = function(){
this.pos.x = random(0, width);
this.pos.y = random(0, height);
this.life = maxLife;
}
this.display = function(r){
ellipse(this.pos.x, this.pos.y, r, r);
}
}
function advanceVisual()
{
visualMode = ++visualMode % numModes;
if(visualMode == 0){
invertColors = !invertColors;
backgroundColor = invertColors ? color(235, 235, 235) : color(20, 20, 20);
}
noiseSeed(random()*Number.MAX_SAFE_INTEGER);
background(backgroundColor);
for(var i = 0; i < nums; i++){
particles[i].respawn();
particles[i].life = random(0,maxLife);
}
}
</script>
<style>
body {
padding: 0;
margin: 0;
position: fixed;
}
</style>
<title>My first 3d animation</title>
</head>
<body></body>
</html>