I want to build something similar to desmos, where you can draw a graph in a canvas and then move it around.
I've been successful so far, but the only thing left is the user input.
Using a <input type="text">
tag I want the user to write for example:
"5x + 2"
The result should be:
var f = 5*x + 2;
I searched a lot for a way to do this and the only things I found were some Maths libraries in JavaScript and the eval()
function. The last one is really helpful, because I could replace the x
with the x
value in the graph and it would work to build the graph of the function. The problem is that it lags a lot when I want to move the graph around so it is not the best idea.
I'm sure that it lags because the eval()
function has to convert the string each time for every x value of the canvas for about 40-50 times a second.
What I want to achieve is convert the string into a Math function just once, and then use it.
Is it possible? Can anyone please help
EDIT 1: This is my function:
function f (pixelX) {
var x = getCoordX (pixelX);
var f = 2 * x + 2;
return getPixelY(f);
}