This is a lightweight web-based graphing calculator that accepts user-entered functions (LaTeX-supported via MathQuill or plain expressions) and renders them on an interactive canvas. The app focuses on implicit plotting (contour-like rendering of f(x,y)=0) using a marching-squares style approach, persistent overlay helpers (points/lines), and a small developer console API for programmatic control.
Live Demo : https://akhilsirvi.github.io/graphing-calculator/src/
- Interactive MathQuill input for functions with LaTeX support.
- Implicit function rendering using sampling + linear interpolation across cells (implemented in
src/graph/graph_rendering.js). - Pan and zoom with preserved X/Y aspect ratio (
src/graph/graph.js). - Persistent overlays: add/remove points and lines in world coordinates; they persist through redraws, panning and resizing.
- Factorial support: postfix
!is supported (e.g.5!or(x+1)!). Non-integer factorials use a Lanczos-based Gamma approximation. - Per-function inline error reporting shown under the related function input when parsing/evaluation fails.
- Input parsing: MathQuill supplies editable LaTeX/plain input; the code normalizes the string and rewrites known math names to
Math.*(e.g.sin→Math.sin). A small preprocessor converts postfix!intofactorial(...)before further conversions. - Factorial handling: integer factorials use an exact product; non-integer factorials call a Lanczos-based Gamma approximation implemented in
src/graph/graph_rendering.js(exposed aswindow.lanczosGamma/window.factorial). - Expression evaluation: sanitized expressions are compiled into a lightweight evaluator via
new Function('x','y', 'return (<expr>);'). Evaluations receivexandyin world coordinates and must return a numeric value. - Implicit rendering: the renderer samples the function on a grid covering the visible viewport, evaluates corner values for each grid cell, detects sign changes on edges, and uses linear interpolation to estimate zero crossings — producing short line segments that approximate the implicit curve (a marching-squares-like approach).
- Coordinate transforms: sampling ranges come from the current viewport;
worldToPixelandpixelToWorldconvert between world coordinates and canvas pixels so overlays, points, and lines remain correct under pan/zoom. - Error reporting and UX: parsing/evaluation for each function is wrapped in try/catch; on error the UI inserts a
.fn-errorelement beneath the corresponding function input (role=alert) with a concise message so users can correct their expression. - Performance notes: accuracy depends on sampling resolution — higher resolution improves fidelity but increases CPU cost. Color picker changes and function edits trigger incremental redraws.
- Open
src/index.htmlin a browser (or run a simple static server for local development). - Enter a function into one of the function input boxes. MathQuill accepts LaTeX-style input; plain expressions are also accepted and converted to Math.* functions where appropriate (e.g.
sin,cos,pi,e). - Click
Plot Graphto render the implicit curve(s). Use the color picker to change the function's color; the indicator updates live. - Pan by dragging the graph area and zoom with the mouse wheel; overlays and function plots will re-render automatically.
- The parser rewrites known math names to
Math.*calls (e.g.sin(x)→Math.sin(x)). - You may use
piande(case-insensitive) which are converted toMath.PIandMath.E. - Postfix factorial
!is supported and will be converted tofactorial(...)before evaluation. Integer factorials use exact product; non-integer factorials use the Lanczos Gamma approximation.
From the browser DevTools console you can inspect and control the graphing internals exposed on window.Graph and window.App.
Common helpers
window.Graph.createCanvas(el)– create and return a 2D context for the graph containerel.window.Graph.clearAndRedraw(ctx, el)– redraw grid and overlays.window.Graph.addPoint(x, y, color, radius)– add a persistent overlay point (world coordinates).window.Graph.clearPoints()– remove persistent points.window.Graph.addLine(x1,y1,x2,y2,color,width)– add a persistent line.window.Graph.clearLines()– remove persistent lines.window.Graph.worldToPixel(ctx,x,y)/window.Graph.pixelToWorld(ctx,px,py)– coordinate conversion helpers.
window.Graph.addPoint(2, 3, 'deepskyblue', 6);
window.App && window.App.redraw();
window.Graph.addLine(-5, 0, 5, 0, 'cyan', 3);
window.App && window.App.redraw();- Rendering algorithm lives in
src/graph/graph_rendering.js(sampling grid resolution and linear interpolation). - The viewport, canvas creation, pan/zoom and redraw logic are implemented in
src/graph/graph.js. - UI wiring and MathQuill hooks are in
src/script.js. - Third-party libraries are under
lib/and listed inTHIRD-PARTY-LICENSES.md.
See THIRD-PARTY-LICENSES.md for licensing information about bundled libraries.