-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
113 lines (72 loc) · 1.91 KB
/
sketch.js
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
/*
Particles spinner example:
using vector from angle.
*/
let particles = []
const max_particles = 1
let dir = 0
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(60) //adjust to see animation in slow motion
}
function draw() {
background(10);
for (let i = 0 ; i < max_particles ; i++){
let p = new Particle(width/2, height/2, dir) //new direction each cycle
particles.push(p)
}
for (let p of particles){
p.display()
p.move()
// p.bounce()
if (p.die()){
//removes each particle if it happens to be dead
let i = particles.indexOf(p) //get the index of the dead particle
particles.splice(i, 1) //splice it away.
}
}
dir +=0.1
}
class Particle{
constructor (x,y, angle){
this.r = random(12,20)
this.pos = createVector(x,y)
this.vel = p5.Vector.fromAngle(angle)
this.vel.mult(10);
this.lifetime = 255
}
display(){
stroke(120,225, 125, this.lifetime)
fill(120,225, 125, this.lifetime)
ellipse(this.pos.x, this.pos.y, this.r)
}
move(){
this.vel.add(this.accel)
this.pos.add(this.vel);
this.lifetime -= 4 //disappear faster by subtracting more each cycle
}
// applyForce(force) {
// this.accel.add(force);
// }
die(){
return (this.lifetime < 0)
}
bounce(){
//Bounce off the canvas edges
if (this.pos.y >= height-this.r) {
this.pos.y = height-this.r;
this.vel.y *= -1;
} else if (this.pos.y <= this.r){
//this.pos.y = height-this.r; //the reappear at the bottom
this.pos.y = this.r;
this.vel.y *= -1;
}
if (this.pos.x >= width-this.r) {
this.pos.x = width-this.r;
this.vel.x *= -1;
} else if (this.pos.x <= this.r) {
this.pos.x = this.r;
this.vel.x *= -1;
}
}
}