-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_cranberries.js
232 lines (171 loc) · 7.79 KB
/
sketch_cranberries.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// The Nature of Code
// http://natureofcode.com
// Blob Example
// Example 5.13 adapted by Zach Lieberman
/////////print svg points
//var svg = document.getElementById('circle-svg');
//console.log(svg);
var x_el = [];
var y_el = [];
var pearDots = document.getElementsByClassName('st0');
for(var i = 0; i<pearDots.length; i++){
//console.log(pearDots.getAttributeNS(null, 'cx'));
var pearDot = pearDots[i];
var pearDot_value = pearDot.getAttributeNS(null, 'cx');
x_el.push(pearDot_value);
pearDot_value = pearDot.getAttributeNS(null, 'cy');
y_el.push(pearDot_value);
}
/////////
var c,d;
var physics;
var repeler;
var first_class_blob;
var first_class_blob2;
//var x_el = [2.8, 7.2, 3.9, 10.4, 26.1, 43.8, 59.5, 65.9, 62.7, 67.1];
//var y_el = [3.1, 24.6, 44.6, 59.1, 65.4, 65.4, 59.1, 44.6, 24.6, 3.1];
var x_el_t = [x_el[2],(x_el[3]+x_el[2])/2,x_el[3]];
var y_el_t = [y_el[2],(y_el[3]+y_el[2])/2,y_el[3]];
var x_el_t_t = [x_el[3]+10,x_el[3],x_el[3]-10];
var y_el_t_t = [y_el[3]-10,y_el[3],y_el[3]+10];
var magnif = 5;
var dista = 250;
function setup() {
var w = 560, h = 500;
createCanvas(w, h);
// Initialize the physics
physics = new VerletPhysics2D();
physics.setDrag(0.03);
physics.addBehavior(new GravityBehavior(new Vec2D(0, 1.5)));
c = $("#c")[0];
c.width = width;
c.height = height;
d = new Degas(c);
var colors_b = ["#f4d7c0", "#fcdcb6", "#e6bc98", "#512e23", "#d4aa78", "#a16e4b"];
var colors_b_t = ["#a37a62", "#bc7d77", "#a37a62","#331d17","#a37a62", "#7c514a"];
var colors_b_t_t = ["#895650","#965d59","#895650","#140b09","#895650","#603d3b"];
var c_index = getRandomInt(0,6);
var vec_att = new Vec2D(((x_el[0]/10+x_el[x_el.length-1]*9/10)*magnif),(y_el[0]/10+y_el[y_el.length-1]*9/10)*magnif);
var vec_att2 = new Vec2D(((x_el[0]*9/10+x_el[x_el.length-1]/10)*magnif),(y_el[0]*9/10+y_el[y_el.length-1]/10)*magnif);
var vec_att_next = new Vec2D(vec_att.x+dista, vec_att.y);
var vec_att2_next = new Vec2D(vec_att2.x+dista, vec_att2.y);
first_class_blob = new Bblob(d,colors_b[c_index], colors_b_t_t[c_index], colors_b_t[c_index], x_el, y_el, x_el_t, y_el_t, vec_att, vec_att2, 0);
first_class_blob2 = new Bblob(d,colors_b[c_index], colors_b_t_t[c_index], colors_b_t[c_index], x_el, y_el, x_el_t, y_el_t, vec_att_next, vec_att2_next, dista);
repeler = new Particle(new Vec2D(mouseX, mouseY), 100, 100, -5);
first_class_blob.pushParticles();
first_class_blob2.pushParticles();
first_class_blob.addSp();
first_class_blob2.addSp();
}
function draw() {
//randomSeed(99);
background(55);
physics.update();
//repeler.set(mouseX,mouseY);
var x_rela = mouseX - $('#c').offset().left;
var y_rela = mouseY - $('#c').offset().top;
repeler.set(x_rela,y_rela);
first_class_blob.updateBlobs();
first_class_blob2.updateBlobs();
d.render();
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function Bblob(which_Degas, color, t_color, t_t_color, x_el, y_el, x_el_t, y_el_t, fir_att, sec_att, x_cord) {
this.particles = [];
this.attractor = new Particle(fir_att, 50, 50, 5);
this.attractor2 = new Particle(sec_att, 50, 50, 5);
this.pPoints = [];
this.pPoints_t = [];
this.pPoints_t_t = [];
this.baseColor = color;
this.titColor = t_color;
this.titColor2 = t_t_color;
this.p = new Degas.Path(this.pPoints);
this.p.stroke = this.baseColor;
this.p.fill = this.baseColor;
this.p.smoothPointsNumber = 20;
this.p.closed = true;
this.p_t = new Degas.Path(this.pPoints_t);
this.p_t.stroke = this.titColor2;
this.p_t.fill = this.titColor2;
this.p_t.smoothPointsNumber = 20;
this.p_t.closed = true;
this.p_t_t = new Degas.Path(this.pPoints_t_t);
this.p_t_t.stroke = this.titColor;
this.p_t_t.fill = this.titColor;
this.p_t_t.smoothPointsNumber = 20;
this.p_t_t.closed = true;
which_Degas.addChild(this.p);
which_Degas.addChild(this.p_t);
which_Degas.addChild(this.p_t_t);
this.pushParticles = function(){
for (var i = 0; i < x_el.length; i++) {
this.x = x_el[i]*magnif+x_cord;
this.y = y_el[i]*magnif;
this.particles.push(new Particle(new Vec2D(this.x, this.y), 0.01, 300, 1));
this.pPoints.push( new Degas.Point( this.x, this.y ) );
}
for (var i = 0; i < x_el_t.length; i++) {
this.x = x_el_t[i]*magnif;
this.y = y_el_t[i]*magnif;
this.pPoints_t.push( new Degas.Point( this.x, this.y ) );
}
for (var i = 0; i < x_el_t_t.length; i++) {
this.x = x_el_t_t[i]*magnif;
this.y = y_el_t_t[i]*magnif;
this.pPoints_t_t.push( new Degas.Point( this.x, this.y ) );
}
}
this.addSp = function(){
for (var i = 0; i < x_el.length; i++) {
this.distance1 = Math.hypot(this.particles[(i + 1) % this.particles.length].x-this.particles[i].x, this.particles[(i + 1) % this.particles.length].y-this.particles[i].y);
this.spring1 = new VerletSpring2D(this.particles[i], this.particles[(i + 1) % this.particles.length], this.distance1, 0.02);
physics.addSpring(this.spring1);
if (i % 3 == 0) {
this.distance2 = Math.hypot(this.particles[(i + Math.floor((x_el.length)/2)) % this.particles.length].x-this.particles[i].x, this.particles[(i + Math.floor((x_el.length)/2)) % this.particles.length].y-this.particles[i].y);
this.spring2 = new VerletSpring2D(this.particles[i], this.particles[(i + Math.floor((x_el.length)/2)) % this.particles.length], this.distance2, 0.02);
physics.addSpring(this.spring2);
}
this.spring3 = new VerletSpring2D(this.particles[i], this.attractor, Math.hypot(this.attractor.x-this.particles[i].x,this.attractor.y-this.particles[i].y), 0.005);
this.spring4 = new VerletSpring2D(this.particles[i], this.attractor2, Math.hypot(this.attractor2.x-this.particles[i].x,this.attractor2.y-this.particles[i].y), 0.005);
physics.addSpring(this.spring3);
physics.addSpring(this.spring4);
}
}
this.updateBlobs = function(){
this.seconds = millis() / 1000;
this.attractor.set(fir_att.x, fir_att.y);
this.attractor2.set(sec_att.x, sec_att.y);
//this.attractor_t.set(this.particles[4].x+10, this.particles[4].y);
//this.attractor2_t.set(this.particles[5].x-10, this.particles[5].y);
for( var i = 0; i < this.particles.length; i++ ){
this.p.points[i].x = this.particles[i].x;
this.p.points[i].y = this.particles[i].y;
this.particles[i].behavior.radius = 100 + 40 * sin(this.seconds + i / 3.0);
this.particles[i].behavior.radiusSquared = this.particles[i].behavior.radius * this.particles[i].behavior.radius;
}
this.x_1 = Math.floor(this.particles[1].x);
this.y_1 = Math.floor(this.particles[1].y);
this.x_2 = Math.floor(this.particles[2].x);
this.y_2 = Math.floor(this.particles[2].y);
//this.x_changed = [this.fourth_x, (this.fourth_x+this.fifth_x)/2, this.fifth_x, (this.fourth_x+this.fifth_x)*3/5, (this.fourth_x+this.fifth_x)*2/5];
//this.y_changed = [this.fourth_y, (this.fourth_y+this.fifth_y)/2+10, this.fifth_y, (this.fourth_y+this.fifth_y)/2-40, (this.fourth_y+this.fifth_y)/2-40];
this.x_changed = [this.x_1-10, (this.x_1 + this.x_2)/2, this.x_2+10];
this.y_changed = [this.y_1+6, (this.y_1 + this.y_2)/2 + 20, this.y_2+6];
this.x_changed_t = [this.x_1-30, (this.x_1 + this.x_2)/2, this.x_2+30];
this.y_changed_t = [this.y_1+15, (this.y_1 + this.y_2)/2 + 30, this.y_2+15];
for( var i = 0; i < this.p_t.points.length; i++ ){
this.p_t.points[i].x = this.x_changed[i];
this.p_t.points[i].y = this.y_changed[i];
}
for( var i = 0; i < this.p_t_t.points.length; i++ ){
this.p_t_t.points[i].x = this.x_changed_t[i];
this.p_t_t.points[i].y = this.y_changed_t[i];
}
this.p.smooth();
this.p_t.smooth();
this.p_t_t.smooth();
}
}