I want to build something similar to desmos where you can draw a graph in a canvas and then move it.
I have been successful so far, but only user input remains.
Using the tag <input type="text">, 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 ones I found were some JavaScript Maths libraries and a function eval(). The latter is really useful, because I could replace it with xa value xon the graph, and it will work to plot the function. The problem is that it breaks down a lot when I want to move the chart, so this is not a good idea.
I am sure that it lags behind because the function eval()should convert the string every time for each x value of the canvas about 40-50 times per second.
What I want to achieve is to convert a string to a Math function only once, and then use it.
Is it possible? Can anybody help
EDIT 1 : This is my function:
function f (pixelX) {
var x = getCoordX (pixelX);
var f = 2 * x + 2;
return getPixelY(f);
}
source
share