-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconffeti.js
136 lines (118 loc) · 3.15 KB
/
conffeti.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let nouvelle,
ancienne,
pression;
let themeCouleur = [
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5',
'#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4CAF50',
'#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800',
'#FF5722'
];
class Particule {
constructor(parent) {
this.parent = parent;
this.gravite = parent.gravite;
this.reinit();
this.forme = round(random(0, 1));
this.etape = 0;
this.prise = 0;
this.priseFacteur = random(-0.02, 0.02);
this.multFacteur = random(0.01, 0.08);
this.priseAngle = 0;
this.priseVitesse = 0.05;
}
reinit() {
this.position = this.parent.position.copy();
this.position.y = random(-20, -100);
this.position.x = random(0, width);
this.velocite = createVector(random(-6, 6), random(-10, 2));
this.friction = random(0.995, 0.98);
this.taille = round(random(5, 15));
this.moitie = this.taille / 2;
this.couleur = color(random(themeCouleur));
}
dessiner() {
this.etape = 0.5 + Math.sin(this.velocite.y * 20) * 0.5;
this.prise = this.priseFacteur + Math.cos(this.priseAngle) * this.multFacteur;
this.priseAngle += this.priseVitesse;
translate(this.position.x, this.position.y);
rotate(this.velocite.x * 2);
scale(1, this.etape);
noStroke();
fill(this.couleur);
if (this.forme === 0) {
rect(-this.moitie, -this.moitie, this.taille, this.taille);
} else {
ellipse(0, 0, this.taille, this.taille);
}
resetMatrix();
}
integration() {
this.velocite.add(this.gravite);
this.velocite.x += this.prise;
this.velocite.mult(this.friction);
this.position.add(this.velocite);
if (this.position.y > height) {
this.reinit();
}
if (this.position.x < 0) {
this.reinit();
}
if (this.position.x > width + 10) {
this.reinit();
}
}
rendu() {
this.integration();
this.dessiner();
}
}
class SystemeDeParticules {
constructor(nombreMax, position, gravite) {
this.position = position.copy();
this.nombreMax = nombreMax;
this.gravite = createVector(0, 0.1);
this.friction = 0.98;
// le tableau
this.particules = [];
for (var i = 0; i < this.nombreMax; i++) {
this.particules.push(new Particule(this));
}
}
rendu() {
if (pression) {
var force = p5.Vector.sub(nouvelle, ancienne);
this.gravite.x = force.x / 20;
this.gravite.y = force.y / 20;
}
this.particules.forEach(particules => particules.rendu());
}
}
let confettis;
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(60);
ancienne = createVector(0, 0);
nouvelle = createVector(0, 0);
confettis = new SystemeDeParticules(500, createVector(width / 2, -20));
}
function draw() {
background(color("#111"));
nouvelle.x = mouseX;
nouvelle.y = mouseY;
confettis.rendu();
ancienne.x = nouvelle.x;
ancienne.y = nouvelle.y;
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
confettis.position = createVector(width / 2, -40);
}
function mousePressed() {
next = 0;
pression = true;
}
function mouseReleased() {
pression = false;
confettis.gravite.y = 0.1;
confettis.gravite.x = 0;
}