-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
537 lines (461 loc) · 14.8 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* eslint-disable no-undef, no-unused-vars */
const DAY = 4; // change the day being drawn here
const PADDING = 0;
const FRAMERATE = 60;
const duration = 15 * 1000;
const enable_record = false;
const useAspectRatio = true;
const width = () => windowWidth - PADDING;
const height = () => {
return useAspectRatio ? round((width() * 9) / 16) : windowHeight - PADDING;
};
// Setup p5.js
function setup() {
createCanvas(width(), height());
frameRate(FRAMERATE);
window["day" + DAY].setup();
if (enable_record) {
document.querySelector("button").addEventListener("click", (e) => {
e.preventDefault();
setTimeout(() => {
startRecording();
setTimeout(() => stopRecording(), duration);
}, 100);
});
} else {
let btn = document.querySelector("button");
btn.remove();
}
}
// Draw p5.js
function draw() {
window["day" + DAY].draw({
deltaTime: deltaTime
});
}
// This Redraws the Canvas when resized
windowResized = function () {
resizeCanvas(width(), height());
};
////////////////////////////////////////////////////////////////////////////////////////////
// TEMPLATE
////////////////////////////////////////////////////////////////////////////////////////////
window.blank = {
// Data
idk: { x: 0 },
// Custom methods
doSomething: function () {},
// Core drawing
setup: function () {},
draw: function (p5) {
// Clear background
background(13, 15, 19);
// Put drawings here
// fill(31, 134, 181); // lessgo
}
};
////////////////////////////////////////////////////////////////////////////////////////////
// DAY 1
////////////////////////////////////////////////////////////////////////////////////////////
window.day1 = {
// Data
cube: { x: 50, y: 40, width: 250, height: 250, velX: 0, velY: 0 },
// Custom methods
checkCollisions: function (cube) {
if (cube.x + cube.width > width() || cube.x < 0) {
cube.velX = cube.velX * -1;
}
if (cube.y + cube.height > height() || cube.y < 0) {
cube.velY = cube.velY * -1;
}
},
// Core drawing
setup: function () {
this.cube.velX = random(0, 0.5);
this.cube.velY = random(0, 0.5);
},
draw: function (p5) {
let cube = this.cube;
// Clear background
background(13, 15, 19);
// Put drawings here
fill(31, 134, 181);
noStroke();
rect(cube.x, cube.y, cube.width, cube.height);
fill(255);
textStyle(BOLD);
textSize(140);
text("p5*", cube.x + 10, cube.height + cube.y);
// Move cube
cube.x += cube.velX * p5.deltaTime;
cube.y += cube.velY * p5.deltaTime;
// Collision checks
this.checkCollisions(cube);
}
};
////////////////////////////////////////////////////////////////////////////////////////////
// DAY 2
////////////////////////////////////////////////////////////////////////////////////////////
window.day2 = {
// Data
cubes: [],
cube_count: 40,
base_color: null,
lerp_color: null,
// Custom methods
createCube: function (x, y, width, height, velX, velY, color) {
return { x, y, width, height, velX, velY, color };
},
createRandomCube: function () {
let _width = round((random(5, 200) * (width() / 1920)) / 2);
return this.createCube(
random(0, width() - _width),
random(0, height() - _width),
_width,
_width,
random(-0.5, 0.5),
random(-0.5, 0.5),
color(31, 134, 181)
);
},
drawCube: function (cube) {
noStroke();
fill(cube.color);
rect(cube.x, cube.y, cube.width, cube.height);
},
moveCube: function (cube, p5) {
cube.x += cube.velX * p5.deltaTime;
cube.y += cube.velY * p5.deltaTime;
},
checkCollisions: function (cube) {
if (cube.x + cube.width > width() || cube.x < 0) {
cube.velX = cube.velX * -1;
}
if (cube.y + cube.height > height() || cube.y < 0) {
cube.velY = cube.velY * -1;
}
},
setup: function () {
this.base_color = color(31, 134, 181, 180);
this.lerp_color = color(181, 134, 31, 180);
[...new Array(this.cube_count).keys()].forEach((x) => {
let eyyy = this.createRandomCube();
eyyy.color = lerpColor(
this.base_color,
this.lerp_color,
x / this.cube_count
);
this.cubes.push(eyyy);
});
},
draw: function (p5) {
// Clear background
background(13, 15, 19);
// Draw and move cubes.
for (let i = 0; i < this.cubes.length; ++i) {
let cube = this.cubes[i];
this.drawCube(cube);
this.moveCube(cube, p5);
this.checkCollisions(cube);
}
}
};
////////////////////////////////////////////////////////////////////////////////////////////
// DAY 3
////////////////////////////////////////////////////////////////////////////////////////////
window.day3 = {
// Data
SET_COLOR: 150,
UNSET_COLOR: 50,
grid: { res_x: 60, res_y: 0, w: 0, data: [] },
snake: {
body: [{ x: 10, y: 10, direction: 1, length: 7 }],
state: 0
},
// 0 = up, 1 = right, 2 = down, 3 = left
// Custom methods
getDisplacedX: function (joint, distance = 1) {
return joint.direction === 1
? joint.x + distance
: joint.direction === 3
? joint.x - distance
: joint.x;
},
getDisplacedY: function (joint, distance = 1) {
return joint.direction === 0
? joint.y - distance
: joint.direction === 2
? joint.y + distance
: joint.y;
},
moveSnake: function (delta) {
this.snake.state += 0.2 * delta;
let lead = this.snake.body[0]; // Get the lead joint
// Are we changing direction this step? (art only for now)
if (floor(random(1, 4)) === 1) {
// lead.direction = floor(random(0, 4)); // pure random
lead.direction += 1;
if (lead.direction === 4) lead.direction = 0;
}
// Have we hit an edge with the leading joint?
if (lead.x === this.grid.res_x - 1 && lead.direction === 1) {
lead.direction = 2;
}
if (lead.y === this.grid.res_y - 1 && lead.direction === 2) {
lead.direction = 3;
}
if (lead.x === 0 && lead.direction === 3) {
lead.direction = 0;
}
if (lead.y === 0 && lead.direction === 0) {
lead.direction = 1;
}
// Are we moving this step?
if (this.snake.state > 1) {
lead.x = this.getDisplacedX(lead);
lead.y = this.getDisplacedY(lead);
this.snake.state = 0;
}
// For each joint in the snakes body
for (let l = 0; l < this.snake.body.length; ++l) {
// Get the joint
let joint = this.snake.body[l];
// All along the joint
for (let c = 0; c < joint.length; ++c) {
let x = this.getDisplacedX(joint, -1 * c);
let y = this.getDisplacedY(joint, -1 * c);
if (x >= 0 && x < this.grid.res_x && y >= 0 && y < this.grid.res_y) {
// this.grid.data[x][y] = this.SET_COLOR; // correct, but using art for now
if (floor(random(1, 4)) >= 2) {
this.grid.data[x][y] = this.SET_COLOR + this.grid.data[x][y] / 5;
} else {
this.grid.data[x][y] = this.UNSET_COLOR;
}
}
}
// Is this the last joint and did we move?
if (this.snake.state === 0 && l === this.snake.body.length - 1) {
// Unset the previous point
let x = this.getDisplacedX(joint, -1 * joint.length);
let y = this.getDisplacedY(joint, -1 * joint.length);
if (x >= 0 && x < this.grid.res_x && y >= 0 && y < this.grid.res_y) {
// this.grid.data[x][y] = this.UNSET_COLOR; // correct, but using art for now
this.grid.data[x][y] = this.UNSET_COLOR + this.grid.data[x][y] / 2;
}
}
}
},
// This is really bad but I hacked it together super quickly and it
// mostly does the job
locateX: function (x, y) {
let gap =
(height() - this.grid.res_y * this.grid.w) / (this.grid.res_y + 1);
let start_x =
width() / 2 -
(this.grid.res_x * this.grid.w + gap * (this.grid.res_x - 1)) / 2;
return start_x + x * (this.grid.w + gap);
},
locateY: function (x, y) {
let gap =
(height() - this.grid.res_y * this.grid.w) / (this.grid.res_y + 1);
let start_y =
height() / 2 -
(this.grid.res_y * this.grid.w + gap * (this.grid.res_y - 1)) / 2;
return start_y + y * (this.grid.w + gap);
},
// Core drawing
setup: function () {
this.grid.w = floor(width() / (this.grid.res_x + 1));
this.grid.res_y = floor((this.grid.res_x + 1) * (height() / width()));
console.log(this.grid);
for (let x = 0; x < this.grid.res_x; ++x) {
this.grid.data.push(
[...new Array(this.grid.res_y)].map((x) => this.UNSET_COLOR)
);
}
// Place snake in centre
this.snake.body[0].x = floor(this.grid.res_x / 2);
this.snake.body[0].y = floor(this.grid.res_y / 2);
},
draw: function (p5) {
// Clear background
background(13, 15, 19);
// Put drawings here
for (let x = 0; x < this.grid.res_x; ++x) {
for (let y = 0; y < this.grid.res_y; ++y) {
noStroke();
fill(31, 134, 181, this.grid.data[x][y]);
rect(this.locateX(x, y), this.locateY(x, y), this.grid.w, this.grid.w);
}
}
// Update the snake
this.moveSnake(p5.deltaTime);
}
};
////////////////////////////////////////////////////////////////////////////////////////////
// DAY 4
////////////////////////////////////////////////////////////////////////////////////////////
window.day4 = {
// Data
SET_COLOR: 150,
UNSET_COLOR: 50,
grid: { res_x: 50, res_y: 0, w: 0, data: [] },
snake: {
body: [{ x: 8, y: 1, direction: 1, length: 128 }],
state: 0
},
halt: 0,
haltAt: 15000,
// 0 = up, 1 = right, 2 = down, 3 = left
// Custom methods
getDisplacedX: function (joint, distance = 1) {
return joint.direction === 1
? joint.x + distance
: joint.direction === 3
? joint.x - distance
: joint.x;
},
getDisplacedY: function (joint, distance = 1) {
return joint.direction === 0
? joint.y - distance
: joint.direction === 2
? joint.y + distance
: joint.y;
},
jointSnake: function (x, y, direction) {
let new_head = { x, y, direction, length: 1 };
this.snake.body.unshift(new_head);
return this.snake.body[0];
},
setColorAlongJoint: function (joint, color, l, a = -1) {
// Locate the previous point by reverse displacement
let x = this.getDisplacedX(joint, a * l);
let y = this.getDisplacedY(joint, a * l);
// Unset it gracefully
if (x >= 0 && x < this.grid.res_x && y >= 0 && y < this.grid.res_y) {
this.grid.data[x][y] = color;
}
},
moveSnake: function (delta, p5) {
if (this.halt < this.haltAt) this.snake.state += 0.02 * delta;
let lead = this.snake.body[0]; // Get the lead joint
// Are we moving this step?
if (this.snake.state > 1) {
++this.halt;
// Have we hit an edge with the leading joint?
if (
keyIsDown(DOWN_ARROW) ||
(lead.x === this.grid.res_x - 1 && lead.direction === 1)
) {
// lead.direction = 2;
lead = this.jointSnake(lead.x, lead.y, 2);
} else if (
keyIsDown(LEFT_ARROW) ||
(lead.y === this.grid.res_y - 1 && lead.direction === 2)
) {
// lead.direction = 3;
lead = this.jointSnake(lead.x, lead.y, 3);
} else if (
keyIsDown(UP_ARROW) ||
(lead.x === 0 && lead.direction === 3)
) {
// lead.direction = 0;
lead = this.jointSnake(lead.x, lead.y, 0);
} else if (
keyIsDown(RIGHT_ARROW) ||
(lead.y === 0 && lead.direction === 0)
) {
// lead.direction = 1;
lead = this.jointSnake(lead.x, lead.y, 1);
} else {
// No new lead point, so just increment the length of the
// lead joint.
++lead.length; // Increment the lead joint
}
// Move the lead forwards
lead.x = this.getDisplacedX(lead);
lead.y = this.getDisplacedY(lead);
// Reset state counter
this.snake.state = 0;
// console.log("-----------------------------");
// For each joint in the snakes body
for (let l = 0; l < this.snake.body.length; ++l) {
// Get the joint
let joint = this.snake.body[l];
// Is this the last joint?
if (l === this.snake.body.length - 1) {
--joint.length; // Decrement the end joint
// console.log(`lead length ${lead.length}`);
// console.log(`Trimmed to: ${joint.length}`);
}
// Foreach square along the joint, set the color
for (let c = 0; c < joint.length; ++c) {
this.setColorAlongJoint(joint, this.SET_COLOR, c);
}
// console.log(
// `Joint #${l + 1} is ${joint.length} long at ${joint.x}, ${joint.y}`
// );
// Is this the last joint?
if (l === this.snake.body.length - 1) {
// Unset the last color
this.setColorAlongJoint(joint, this.UNSET_COLOR, joint.length);
if (joint.length === 0) {
// Yeet it away if the joint no longer has length
// console.log(`>> Removed at: ${joint.length}`);
let dropped = this.snake.body.pop();
// Clean up the square left behind because we'll miss it next time.
this.setColorAlongJoint(dropped, this.UNSET_COLOR, 1);
// fill(131, 134, 181, 0.5);
// rect(this.locateX(x, y), this.locateY(x, y), this.grid.w, this.grid.w);
}
}
}
}
},
// This is really bad but I hacked it together super quickly and it
// mostly does the job
locateX: function (x, y) {
let gap =
(height() - this.grid.res_y * this.grid.w) / (this.grid.res_y + 1);
let start_x =
width() / 2 -
(this.grid.res_x * this.grid.w + gap * (this.grid.res_x - 1)) / 2;
return start_x + x * (this.grid.w + gap);
},
locateY: function (x, y) {
let gap =
(height() - this.grid.res_y * this.grid.w) / (this.grid.res_y + 1);
let start_y =
height() / 2 -
(this.grid.res_y * this.grid.w + gap * (this.grid.res_y - 1)) / 2;
return start_y + y * (this.grid.w + gap);
},
// Core drawing
setup: function () {
this.grid.w = floor(width() / (this.grid.res_x + 1));
this.grid.res_y = floor((this.grid.res_x + 1) * (height() / width()));
console.log(this.grid);
for (let x = 0; x < this.grid.res_x; ++x) {
this.grid.data.push(
[...new Array(this.grid.res_y)].map((x) => this.UNSET_COLOR)
);
}
// Place snake in centre
// this.snake.body[0].x = floor(this.grid.res_x / 2);
// this.snake.body[0].y = floor(this.grid.res_y / 2);
},
draw: function (p5) {
// Clear background
background(13, 15, 19);
// Put drawings here
for (let x = 0; x < this.grid.res_x; ++x) {
for (let y = 0; y < this.grid.res_y; ++y) {
noStroke();
fill(31, 134, 181, this.grid.data[x][y]);
rect(this.locateX(x, y), this.locateY(x, y), this.grid.w, this.grid.w);
}
}
// Update the snake
this.moveSnake(p5.deltaTime, p5);
}
};