-
Notifications
You must be signed in to change notification settings - Fork 1
/
weave_square.js
317 lines (273 loc) · 6.07 KB
/
weave_square.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
This is a Live Art work created by Edward Delaporte.
This script is Copyright Edward Delaporte 2021.
This script and the art it creates are licensed under
a Creative Commons Attribution-ShareAlike 4.0
International License.
http://creativecommons.org/licenses/by-sa/4.0/
You can share your own remix of this code
as long as you display this license and attribution.
*/
var weave = {
x: 0,
y: 0,
dx: 1,
dy: 1,
sequence: [],
size: 10,
color1: null,
color2: null
};
var pulse = 0;
var beat = 25;
function draw_weave(weave) {
// stroke(color(0,0,0)); // black outline
fill(weave.color2);
edge_dx = .5 * weave.size;
if(weave_type == "cross") {
// large background square
rect(weave.x - edge_dx,
weave.y - edge_dx,
weave.size + edge_dx,
weave.size + edge_dx
);
fill(weave.color);
// cross wide
rect(weave.x - edge_dx, weave.y,
weave.size + edge_dx, weave.size / 2);
// cross tall
rect(weave.x, weave.y - edge_dx,
weave.size /2, weave.size + edge_dx);
}
if(weave_type == "highlight") {
// large background square
rect(weave.x - edge_dx,
weave.y - edge_dx,
weave.size + edge_dx,
weave.size + edge_dx
);
fill(weave.color);
rect(weave.x + edge_dx, weave.y - edge_dx,
weave.size - edge_dx, weave.size / 4);
}
if(weave_type == "hat") {
// large background square
rect(weave.x - edge_dx,
weave.y - edge_dx,
weave.size + edge_dx,
weave.size + edge_dx
);
fill(weave.color);
rect(weave.x, weave.y - 2*edge_dx,
weave.size - edge_dx, weave.size / 2);
}
if(weave_type == "usa") {
stripe_w = weave.size / 3;
flag_x = weave.x - edge_dx;
flag_y = weave.y - edge_dx;
fill(color(255,0,0)); // red
rect(flag_x,
flag_y,
weave.size,
weave.size,
);
fill(color(255,255,255)); // white
rect(flag_x + stripe_w,
flag_y,
weave.size - stripe_w,
weave.size - stripe_w,
);
fill(color(0,0,255)); // blue
rect(flag_x + 2*stripe_w,
flag_y + 2*stripe_w,
weave.size - 2*stripe_w,
weave.size - 2*stripe_w,
);
}
if(weave_type == "angle") {
triangle(weave.x, weave.y,
weave.x + weave.size,
weave.y,
weave.x + weave.size,
weave.y + weave.size
);
fill(weave.color);
triangle(weave.x, weave.y,
weave.x,
weave.y + weave.size,
weave.x + weave.size,
weave.y + weave.size
);
}
if(weave_type == "dots") {
circle(weave.x, weave.y, 2,2);
circle(weave.x + weave.size,
weave.y + weave.size, 2,2);
fill(weave.color);
circle(weave.x + weave.size, weave.y, 2,2);
circle(weave.x,
weave.y + weave.size, 2,2);
}
if(weave_type == "offset_dots") {
circle(weave.x, weave.y, 2,2);
circle(weave.x + weave.size,
weave.y + weave.size, 2,2);
fill(weave.color);
circle(weave.x + weave.size + 4, weave.y, 2,2);
circle(weave.x + 4,
weave.y + weave.size, 2,2);
}
if(weave_type == "laser") {
circle(weave.x, weave.y, 2);
circle(weave.x + weave.size,
weave.y + weave.size, 2);
stroke(weave.color);
line(weave.x, weave.y,
weave.x+weave.size,
weave.y+weave.size);
}
return weave;
}
function anim_weave(weave) {
weave.x += weave.dx;
weave.y += weave.dy;
return weave;
}
function fresh_color(warmth=30) {
return color(random(0,10)*warmth,random(0,10)*warmth, random(0,10)*warmth);
}
function zippy() {
noStroke();
stripe_w = 60;
for(i=0; i<400; i++) {
fill(fresh_color(3)); // darker fill color
x = i * stripe_w;
// triangle(x, y, x+30, y-random(80), x, y+i+30);
rect(x,0, stripe_w, 400);
}
}
var weave_type = "";
var weave_types = ["cross",
"hat", "highlight", "laser",
"angle", "dots", "offset_dots"];
function setup() {
setup_canvas(400,400);
background('#4444CC');
zippy();
maxline = 400;
balls = [];
item_count = 4;
weave_type = choose(weave_types);
var today_date = new Date();
if(today_date.getMonth() == 6 &&
today_date.getDate() < 7) {
weave_type = "usa";
}
base_path = [];
set1 = [
[2,0,
2,0,
0,2,
], // wide box
[2,0,
0,2,
-1,0,
0,-1,
-1,0,
0,2,
2,0,
], // spiral
[0,2,
1,0,
0,-2
] // U
];
wide_flat = [
[2,0,
2,0,
0,-1,
-2,0,
-2,0
],
[2,0,
0,-1,
]
];
curls = [
[2,0,
0,2,
-1,0,
0,-1,
-1,0,
0,2,
2,0,
], // spiral
[2,0,
0,-1,
]
];
col_set = [
[2,0,
0,1,
2,0,
0,-1,
], // wide brick
[ 0,-2,
2, 0,
0, 2,
-2, 0
] // box
];
curve_sets = [set1, wide_flat, curls, col_set];
fun_curves = choose(curve_sets);
// fun_curves = col_set;
var curve_count = 4;
for(i=0; i<curve_count; i++) {
base_path = base_path.concat(
choose(fun_curves)
);
}
latest_path = base_path;
weave_template = Object(weave);
startx = [330, 70, 330, 70];
starty = [330, 70, 130, 260];
for(i=0;i<item_count;i++){
newb = Object.assign({}, weave_template);
newb.x = startx[i];
newb.y = starty[i]; // Make sure they each start offset a bit
newb.color = fresh_color();
newb.color2 = fresh_color();
latest_path = latest_path.map( item => item * -1);
seq1 = latest_path;
seq2 = seq1.map( item => item * -1); // reverse to close loop
newb.sequence = newb.sequence.concat(seq1);
newb.sequence = newb.sequence.concat(seq2);
balls.push(newb);
}
noStroke();
}
function shift_weave(item) {
if(item.sequence.length > 0) {
item.dx = item.sequence.shift();
item.dy = item.sequence.shift();
} else {
item.dx = 0;
item.dy = 0;
}
return item;
}
function draw() {
if(pulse == 0) {
balls = balls.map( item => shift_weave(item) );
}
pulse++;
if(pulse > beat) {
pulse = 0;
}
balls = balls.map( item => anim_weave(item) );
balls = balls.map( item => draw_weave(item) );
}
function mouseClicked() {
pulse = 0;
setup();
}