aboutsummaryrefslogtreecommitdiffstats
path: root/site/main.mjs
blob: 0c81b413b900b3a0c219e3fabfa568e8b71b8499 (plain)
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
const NUM_POINTS = 5;

// thanks vihart
const TAU = 2 * Math.PI;

// no more than 1% of world size per tick.
const MAX_SPEED = 0.01;

// size of rendered points in proportion to canvas.
const POINT_RADIUS = 0.01;

class Point {
    x = Math.random();
    y = Math.random();
    #heading = Math.random() * TAU;
    speed = Math.random() * MAX_SPEED;
    color = randColor();

    #speedX;
    get speedX() {
        if (this.#speedX === undefined) {
            this.#speedX = this.speed * Math.cos(this.heading);
        }
        return this.#speedX;
    }

    #speedY;
    get speedY() {
        if (this.#speedY === undefined) {
            this.#speedY = this.speed * Math.sin(this.heading);
        }
        return this.#speedY;
    }

    get heading() {
        return this.#heading;
    }

    set heading(val) {
        this.#heading = val;
        this.#speedX = this.#speedY = undefined;
    }
}

const points = [...Array(NUM_POINTS)].map(_ => new Point());

function randColor() {
    const [r, g, b] = [...Array(3)].map(_ => Math.random() * 255);
    return `rgb(${r} ${g} ${b})`
}

function renderPoints(ctx, points) {
    points.forEach(p => {
        ctx.fillStyle = p.color;
        ctx.beginPath();
        ctx.arc(p.x, p.y, POINT_RADIUS, 0, TAU);
        ctx.fill();

        ctx.lineWidth = 0.005;
        ctx.beginPath();
        ctx.moveTo(p.x, p.y);
        ctx.lineTo(p.x + p.speedX * 3, p.y + p.speedY * 3);
        ctx.stroke();
    });
}

function movePoints(points) {
    points.forEach(p => {
        p.x += p.speedX;
        p.y += p.speedY;
    });
}

function bouncePoints(points) {
    let didBounce = false;
    points.forEach(p => {
        const x = p.x + p.speedX;
        const y = p.y + p.speedY;

        if (x < 0) {
            p.heading = Math.PI - p.heading;
            didBounce = true;
        } else if (x >= 1) {
            p.heading = Math.PI - p.heading;
            didBounce = true;
        } else if (y < 0) {
            p.heading = -1 * p.heading;
            didBounce = true;
        } else if (y >= 1) {
            p.heading = -1 * p.heading;
            didBounce = true;
        }
    });
    return didBounce;
}

async function loaded() {
    const canvas = document.querySelector('canvas');
    const ctx = canvas.getContext('2d');
    console.debug('canvas:', canvas, 'ctx', ctx, 'points:', points);
    ctx.scale(canvas.width, canvas.height);

    const fps = document.querySelector('#fps');

    const goButton = document.querySelector('button');
    let paused = true;
    goButton.onclick = e => {
        paused = !paused;
        if (paused) {
            e.target.textContent = 'go';
        } else {
            e.target.textContent = 'pause';
            self.requestAnimationFrame(render);
        }
    };

    let lastTime = document.timeline.currentTime;
    function render(t) {
        if (t > lastTime) {
            fps.textContent = Math.floor(1_000 / (t - lastTime));
            lastTime = t;
        }

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        renderPoints(ctx, points);
        if (bouncePoints(points)) {
            //goButton.onclick({ target: goButton });
        }
        movePoints(points);

        if (!paused) {
            self.requestAnimationFrame(render);
        }
    }

    //goButton.onclick({ target: goButton });
    render(document.timeline.currentTime);
}

document.addEventListener('DOMContentLoaded', loaded);