-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch12.js
120 lines (107 loc) · 3.68 KB
/
sketch12.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
'use strict';
class Sketch12 extends Sketch {
constructor() {
super();
this.scale = 16;
this.width = Math.floor(this.canvas.width / this.scale);
this.height = this.width;
}
load() {
super.load();
this.seeds = [];
this.seeds.push({
x: 255,
y: 0,
vx: 0,
vy: 0,
h: 0,
targetHeight: 400,
alive: true
});
this.mushrooms = [];
}
update() {
const gravity = 0.1;
this.seeds.forEach( s => {
s.vy += gravity;
s.x += s.vx;
s.y += s.vy;
if (s.y >= s.targetHeight) {
const minDist = this.mushrooms.reduce( (acc, e) => {
const dist = Math.abs(e.x - s.x);
return Math.min(acc, dist);
}, Infinity);
if ((minDist > 10) || this.mushrooms.length < 2 ) {
if (s.x > -100 && s.x < 600) {
this.mushrooms.push({
birth: this.t,
x: s.x,
y: s.y,
growTime: this.lmap(Math.random(), 0, 1, 4, 7),
dieTime: this.lmap(Math.random(), 0, 1, 4, 7),
seeded: false,
stemHeight: this.lmap(Math.random(), 0, 1, 50, 150),
s: this.lmap(Math.random(), 0, 1, 50, 100),
h: s.h,
alive: true
});
}
}
s.alive = false;
}
});
this.mushrooms.forEach( m => {
const age = this.t - m.birth;
if (age > m.growTime && !m.seeded) {
m.seeded = true;
for (let i = 0; i < 5 + Math.floor(5 * Math.random()); i++) {
this.seeds.push({
x: m.x,
y: m.y - m.stemHeight,
vx: this.lmap(Math.random(), 0, 1, -5, 5),
vy: 0,
h: m.h + this.lmap(Math.random(), 0, 1, -20, 20),
targetHeight: Math.max(300, Math.min(500, m.y + Math.random() * 20 * Math.sin(Math.random() * 6.28))),
alive: true
});
}
}
m.alive = age < (m.growTime + m.dieTime);
});
this.seeds = this.seeds.filter( s => s.alive );
this.mushrooms = this.mushrooms.filter( m => m.alive );
this.mushrooms.sort( (a, b) => a.y - b.y );
}
draw(ctx, width, height, t, mousePoint) {
ctx.fillStyle = 'blue';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = 'green';
ctx.fillRect(0, 300, width, height);
this.seeds.forEach( s => {
ctx.fillStyle = `hsl(${s.h}, 100%, 50%)`;
ctx.fillRect(s.x, s.y, 3, 3);
});
this.mushrooms.forEach( m => {
const age = t - m.birth;
const growFraction = Math.min(1, age / m.growTime);
const dieFraction = Math.max(0, (age - m.growTime) / m.dieTime);
const ageFraction = (t - m.birth) / (m.growTime + m.dieTime);
//draw stem
const stemWidth = this.lmap(growFraction, 0, 1, 1, 20);
const stemHeight = growFraction < 1 ? this.lmap(growFraction, 0, 1, 1, m.stemHeight) : this.lmap(dieFraction, 0, 1, m.stemHeight, 1);
const stemTopY = m.y - stemHeight;
const sat = growFraction < 1 ? m.s : this.lmap(dieFraction, 0, 1, m.s, 0);
ctx.fillStyle = `hsl(44, ${sat}%, 70%)`;
ctx.fillRect(m.x - stemWidth / 2, stemTopY, stemWidth, stemHeight);
//draw cap
ctx.fillStyle = `hsl(${m.h}, ${sat}%, 50%)`;
ctx.beginPath();
ctx.moveTo(m.x - growFraction * 40, stemTopY);
ctx.arcTo(m.x, stemTopY - growFraction * 40, m.x + growFraction * 40, stemTopY, growFraction * 30);
ctx.lineTo(m.x + growFraction * 40, stemTopY);
ctx.fill();
});
}
}
app.sketches[12] = new Sketch12();
app.sketches[12].desc = `You probably shouldn't eat these.`;