aboutsummaryrefslogtreecommitdiff
path: root/lib/screens/circle.js
blob: d09cb0785e4f6567c1851f92754ded5e21244dbc (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
'use strict';

/**
 * Draws concentric circles. Each ring has its own color.
 *
 * @function CircleScreen
 * @implements IScreen
 */
module.exports = function (modulation, width, height, renderer) {

    let response = '';

    const circles = width > height ? height : width;

    for (let i = 0; i < circles; ++i) {
        const centerX = Math.round(width / 2) + 1;
        const centerY = Math.round(height / 2) + 1;

        const red = Math.floor(Math.random() * 255);
        const blue = Math.floor(Math.random() * 255);
        const green = Math.floor(Math.random() * 255);

        for (let j = 0; j < 180; ++j) {
            const angle =  2 * j * (Math.PI / 180);
            const x = Math.round(centerX + Math.sin(angle) * i);
            const y = Math.round(centerY + Math.cos(angle) * i);

            if (x <= width && x > 0 && y <= height && y > 0) {
                const position = `\x1B[${y};${x}H`; // Move cursor to y,x (CSI y;x H)
                response += `${position}${renderer(red, blue, green)} `;
            }
        }
    }

    return response;
};