-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch16.js
88 lines (74 loc) · 2.04 KB
/
sketch16.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
'use strict';
class Sketch16 extends Sketch {
constructor() {
super();
this.scale = 1;
this.width = Math.floor(this.canvas.width / this.scale);
this.height = this.width;
}
load() {
super.load();
this.objects = [];
for (let i = 0; i < 10; i++) {
const a = Math.random() * 2 * Math.PI;
const v = 3;
const dx = v * Math.cos(a);
const dy = v * Math.sin(a);
this.objects.push({
x: this.lmap(Math.random(), 0, 1, 50, this.width - 50),
y: this.lmap(Math.random(), 0, 1, 50, this.height - 50),
dx,
dy,
state: 'h'
});
}
}
update() {
const r = 40;
this.objects.forEach( o => {
const newX = o.x + o.dx;
const newY = o.y + o.dy;
if (newX < r) {
o.dx = -o.dx;
}
if (newX > (this.width - r)) {
o.dx = -o.dx;
}
if (newY < r * 1.2) {
o.dy = -o.dy;
}
if (newY > (this.height - r)) {
o.dy = -o.dy;
}
o.x = newX;
o.y = newY;
o.state = 's';
});
for (let i = 0; i < this.objects.length - 1; i++) {
for (let j = i + 1; j < this.objects.length; j++) {
const dx = this.objects[i].x - this.objects[j].x;
const dy = this.objects[i].y - this.objects[j].y;
const d2 = dx * dx + dy * dy;
if (d2 < 7000) {
this.objects[i].state = 'h';
this.objects[j].state = 'h';
}
}
}
}
draw(ctx, width, height, t, mousePoint) {
ctx.fillStyle = `hsl(226, 60%, 52%)`;
ctx.fillRect(0, 0, width, height);
const hface = '\ud83d\ude00';
const sface = '\ud83d\ude2d';
const faces = {h: hface, s: sface};
ctx.font = '80px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
this.objects.forEach( o => {
ctx.fillText(faces[o.state], o.x, o.y);
});
}
}
app.sketches[16] = new Sketch16();
app.sketches[16].desc = `Which version of yourself is the real one?`;