npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-mathplotter

v1.0.1

Published

A React math expression plotter — interactive canvas with zoom, pan, multi-expression support, and root detection.

Readme

react-mathplotter

A React math expression plotter built on HTML5 Canvas. Supports interactive zoom, pan, multi-expression plotting with distinct colors, and automatic root detection. Plotter Example


Installation

npm install react-mathplotter

Peer dependencies (you need these in your project):

npm install react react-dom

Quick Start

import PlotCanvas from 'react-mathplotter';

export default function App() {
    return (
        <PlotCanvas
            defaultExpressions={['x^2', 'sin(x)']}
            width={600}
            height={500}
        />
    );
}

Props

| Prop | Type | Default | Description | |---|---|---|---| | defaultExpressions | string[] | [] | Uncontrolled initial list of expression strings | | expressions | string[] | — | Controlled list of expression strings | | onExpressionsChange | (texts: string[]) => void | — | Called when the expression list changes (controlled mode) | | onRootsChange | (roots: Root[]) => void | — | Called when found roots update | | width | number | 500 | Canvas width in pixels | | height | number | 500 | Canvas height in pixels | | colors | string[] | built-in palette | Hex colors for each expression line | | initialDomain | { x: [min, max], y: [min, max] } | {x:[-10,10], y:[-10,10]} | Initial visible domain | | style | CSSProperties | — | Inline style for the wrapper element | | className | string | — | CSS class for the wrapper element |


Imperative API (via ref)

Attach a ref to get full programmatic control:

import { useRef } from 'react';
import PlotCanvas from 'react-mathplotter';

export default function App() {
    const plotRef = useRef();

    return (
        <>
            <PlotCanvas ref={plotRef} />

            <button onClick={() => plotRef.current.addExpression('x^2 - 4')}>
                Add x² - 4
            </button>
            <button onClick={() => plotRef.current.resetView()}>
                Reset View
            </button>
            <button onClick={() => {
                const img = plotRef.current.exportImage('png');
                const a = document.createElement('a');
                a.href = img;
                a.download = 'plot.png';
                a.click();
            }}>
                Download PNG
            </button>
        </>
    );
}

ref.current methods

addExpression(text, color?)ExpressionObj

Add a new math expression to the plot.

const expr = plotRef.current.addExpression('x^2 - 3', '#e11d48');
console.log(expr.id); // save the id for later updates/removal

removeExpression(id)

Remove an expression by the id returned from addExpression.

plotRef.current.removeExpression(expr.id);

updateExpression(id, newText)

Update the text of an existing expression.

plotRef.current.updateExpression(expr.id, 'x^3 - x');

setExpression(index, text)

Set an expression at a specific zero-based index. Creates missing slots automatically.

plotRef.current.setExpression(0, 'sin(x)');
plotRef.current.setExpression(1, 'cos(x)');

clearExpressions()

Remove all expressions and reset to a single empty slot.

plotRef.current.clearExpressions();

getExpressions()ExpressionObj[]

Returns the current list of expression objects.

const exprs = plotRef.current.getExpressions();
// [{ id, text, color }, ...]

getRoots()Root[]

Returns the roots found in the current viewport.

const roots = plotRef.current.getRoots();
// [{ x_value: 2, y_value: 0, color: '#0055ff', text: 'x^2 - 4' }, ...]

resetView()

Snap the viewport back to initialDomain.

plotRef.current.resetView();

setDomain(x, y)

Programmatically set the visible viewport.

plotRef.current.setDomain([-5, 5], [-25, 25]);

getDomain(){ x: [min, max], y: [min, max] }

Get the current visible domain.

const { x, y } = plotRef.current.getDomain();

exportImage(format?, quality?)string

Export the canvas as a data URL. Formats: 'png' (default), 'jpeg', 'webp'.

const dataUrl = plotRef.current.exportImage('jpeg', 0.85);

Controlled Mode

For full external state control:

const [expressions, setExpressions] = useState(['x^2']);

<PlotCanvas
    expressions={expressions}
    onExpressionsChange={setExpressions}
/>

Supported Math Syntax

The plotter uses mathjs for expression parsing. Anything mathjs understands works:

x^2 + 1
sin(x) * cos(x)
sqrt(x)
log(x)
abs(x)
pi * x
e^x

Interactivity

| Action | Effect | |---|---| | Scroll wheel | Zoom in/out anchored to cursor | | Click & drag | Pan the viewport |


License

MIT