'How to fix the graphing line on math calculator with initial scale [Resolved]

Im currently building an math graphing calculator in discord.js, the graphing work fine on 1.0x scale but when im changing the scale the graphing line start not right with the axes, I saw it to be move to bottom-left not to be the center of the graph

function draw(tree) {
    var i = 0;
    dx = 1 / global_scale;

    var y;
    variables.x = minX;

    var width = canvas.width;

    while (isNaN((y = eval(tree))) && i < width) {
        variables.x = minX + i * dx;
        i++;
    }

    let previousY = (y - minY) * global_scale;

    for (; i < width; i++) {
        variables.x = minX + i * dx;
        y = eval(tree);

        if (isNaN(y)) {
            console.log(`discontinuity at x = ${x}`);
            while ((y = eval(tree)) === NaN && i < width) {
                variables.x = minX + i * dx;
                i++;
            }
            previousY = (y - minY) * global_scale;
            continue;
        }

        y = (y - minY) * global_scale;

        ctx.beginPath();
        ctx.moveTo(i - 1, previousY);
        ctx.lineTo(i, y);
        ctx.lineWidth = 2;
        ctx.stroke();
        previousY = y;
    }
}```

[The graphing when i do x^2 with 0.01 initial scale][1]

[The graphing when i do sin(x) with 0.01 initial scale][2]


and i wanna it to be something like this :
[The graphing when i do x^2 with 1.0 initial scale][3]


  [1]: https://i.stack.imgur.com/WTTl6.png
  [2]: https://i.stack.imgur.com/8fH4n.png
  [3]: https://i.stack.imgur.com/hefWl.png


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source