-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch2.js
75 lines (62 loc) · 2.1 KB
/
sketch2.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
'use strict';
class Sketch2 extends Sketch {
constructor() {
super();
this.scale = 32;
this.width = Math.floor(this.canvas.width / this.scale);
this.height = this.width;
}
//build a new maze
load() {
super.load();
}
update() {
}
draw(ctx, width, height, t, mousePoint) {
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, width, height);
const speed = 10;
t = t * speed;
const vt = t % 1;
const rowCount = 62;
const colCount = 32;
const colSize = width / colCount;
const groundHeight = height * 0.5;
const rowHeight = groundHeight / rowCount;
//draw ground
for (let row = 0; row < rowCount; row++) {
const rowIndex = row - Math.floor(t);
ctx.fillStyle = `hsl(119, ${100 * this.rnd(rowIndex / rowCount)}%, 50%)`;
ctx.beginPath();
const a = 0.01;
const factor = (1 - a/((((rowCount - row) - vt)/rowCount)+a));
const rowy = height - groundHeight * factor;
ctx.moveTo(0, rowy);
for (let x = 1; x <= colCount; x++) {
ctx.lineTo(x * colSize, rowy + (1/factor) * row * this.rnd(rowIndex * x + 1));
}
ctx.lineTo(1000, 1000);
ctx.lineTo(-1000, 1000);
ctx.closePath();
ctx.fill();
}
//draw sky
for (let row = 0; row < rowCount; row++) {
const rowIndex = row - Math.floor(t);
const a = 0.1;
const factor = (1 - a/((((rowCount - row) - vt)/rowCount)+a));
const rowy = groundHeight * factor;
ctx.fillStyle = `hsla(48, ${100 * this.rnd(rowIndex / rowCount)}%, 90%, ${1-factor})`;
for (let x = 1; x <= colCount; x++) {
const xpos = x * colSize + -100 + 100 * this.rnd(rowIndex * x + 1);
const ydelta = factor * (-5 + 10 * this.rnd(rowIndex * x * 2 + 1));
if (this.rnd(rowIndex * x + 1) > 0.9) {
ctx.fillRect(xpos, rowy + ydelta, 2, 2);
}
}
}
}
}
app.sketches[2] = new Sketch2();
app.sketches[2].desc = `When the world is falling apart around you just keep running.`;