diff options
author | Jinwei Zhao <[email protected]> | 2016-04-04 14:00:39 +0800 |
---|---|---|
committer | Jinwei Zhao <[email protected]> | 2017-01-13 15:07:45 +0800 |
commit | e002d09d2b6b2317fec6caa8836b20e6709c5da3 (patch) | |
tree | 1e1623524986fc63abcab7b10912dffe55dc1da9 /deprecated/ZJW/javascripts/viz.pde | |
parent | dbcebe1def5c355120c61b575390d1d9ac355f67 (diff) | |
download | jinwei.me-e002d09d2b6b2317fec6caa8836b20e6709c5da3.tar.gz |
jinwei.me
Diffstat (limited to 'deprecated/ZJW/javascripts/viz.pde')
-rw-r--r-- | deprecated/ZJW/javascripts/viz.pde | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/deprecated/ZJW/javascripts/viz.pde b/deprecated/ZJW/javascripts/viz.pde new file mode 100644 index 0000000..4084708 --- /dev/null +++ b/deprecated/ZJW/javascripts/viz.pde | |||
@@ -0,0 +1,81 @@ | |||
1 | ArrayList particles; | ||
2 | int maxParticles = 255; | ||
3 | |||
4 | void setup() { | ||
5 | size(window.innerWidth, window.innerHeight); | ||
6 | particles = new ArrayList(); | ||
7 | rectMode(CENTER); | ||
8 | } | ||
9 | |||
10 | void draw() { | ||
11 | background(21, 32, 46); | ||
12 | |||
13 | if(particles.size() < maxParticles) { | ||
14 | x = random(0, width); | ||
15 | y = random(0, height); | ||
16 | PVector l = new PVector(x, y); | ||
17 | Particle particle = new Particle(l); | ||
18 | particles.add(particle); | ||
19 | } | ||
20 | |||
21 | |||
22 | for(int i=0; i<particles.size(); i++) { | ||
23 | Particle p = (Particle) particles.get(i); | ||
24 | Particle next; | ||
25 | |||
26 | if(i>1) { | ||
27 | next = (Particle) particles.get(i-1); | ||
28 | stroke(57,219,255,10); | ||
29 | line(next.location.x, next.location.x, p.location.x, p.location.y) | ||
30 | } | ||
31 | |||
32 | |||
33 | // Run the particle | ||
34 | p.run(); | ||
35 | |||
36 | if(p.isDead()) { | ||
37 | particles.remove(i); | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | |||
42 | class Particle { | ||
43 | PVector location; | ||
44 | PVector velocity; | ||
45 | PVector acceleration; | ||
46 | float lifespan; | ||
47 | float rectSize; | ||
48 | |||
49 | Particle(PVector l) { | ||
50 | location = l.get(); | ||
51 | velocity = new PVector(random(0,0.05),random(0.02, 0.8)); | ||
52 | acceleration = new PVector(random(0, 0.01),random(0,0.01)); | ||
53 | lifespan = 255.0; | ||
54 | rectSize = 5; | ||
55 | } | ||
56 | |||
57 | void run() { | ||
58 | display(); | ||
59 | update(); | ||
60 | } | ||
61 | |||
62 | void display() { | ||
63 | noStroke(); | ||
64 | fill(73, 219, 255, lifespan/5); | ||
65 | rect(location.x, location.y, rectSize, rectSize); | ||
66 | } | ||
67 | |||
68 | void update() { | ||
69 | lifespan -= 2; | ||
70 | velocity.add(acceleration); | ||
71 | location.add(velocity); | ||
72 | } | ||
73 | |||
74 | boolean isDead() { | ||
75 | if (lifespan < 0) { | ||
76 | return true; | ||
77 | } else { | ||
78 | return false; | ||
79 | } | ||
80 | } | ||
81 | } | ||