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 🙏

© 2025 – Pkg Stats / Ryan Hefner

canvas-curve

v0.2.1

Published

A spline widget

Readme

DEMO 1 - DEMO 2 - DOC

Features

  • create a nice canvas-based spline editor widget
  • add/remove points both programmatically and with the UI
  • interpolate points using cubic splines the graphic way (a-la Photoshop)
  • use both natural cubic splines and monotonic cubic splines
  • use unit coordinates
  • programmatically get single interpolated values (as Numbers) or along the whole x axis (as an Array)
  • no dependency
  • Retina automatically adjusted
  • lock a point over the x and/or the y axis when adding programmatically
  • make a point safe when adding programmatically so that it cannot be removed

How to install

Directly in the browser:

<script src="dist/CanvasSpliner.min.js"></script>

Or with a bundler

npm install --save jonathanlurie/CanvasSpliner

how to use

// create a new CanvasSpliner, with a parent DIV id, a width and a heigh
var cs = new CanvasSpliner("parent", 300, 300);

// Optional: Add points (with unit coordinates)
cs.add( {x:0, y: 0, xLocked: true, yLocked: true, safe: true} );
cs.add( {x:0.1, y: 0.4} );
cs.add( {x:0.3, y: 0.45} );
cs.add( {x:0.6, y: 1} );
cs.add( {x:1, y: 0.6, xLocked: true, safe: true} );

Adding points programmatically

As shown above, we can add points directly from the code. Those points can have other attributes than (x, y) coordinates.

  • we can lock a point so that it does not move along the x axis:
s.add( {x:0.1, y: 0.5, xLocked: true} );
  • we can lock a point so that it does not move along the y axis:
s.add( {x:0.12, y: 0.6, yLocked: true} );
  • We can make a point safe so that the user cannot remove it with a double-click:
s.add( {x:0.8, y: 0.4, safe: true} );
  • We could also use all that together:
cs.add( {x:0.4, y: 0.1, xLocked: true, yLocked: true, safe: true} );

What kind of cubic spline to use?

CanvasSpliner allows two kinds of cubic spline: natural and monotonic. The math behind does not matter, just remember the natural is a bit curvyer and the monotonic is a bit stiffer. You can enable one mode or the other two diferent ways:

  • with the constructor optional last argument:
var cs = new CanvasSpliner("parent", 300, 300, "natural");

Note that this is the default mode when the argument is omited.

  • with the setter
cs.setSplineType( "monotonic" );
// or
cs.setSplineType( "natural" );

Control the UI

  • click on a point: it is selected, you can move it (unless it is xLocked or yLocked)
  • double click on a point: deletes it (unless it is safe)
  • double click somewhere else in the canvas: adds a point

Other methods

Get an interpolated value, in unit coordinates:

var interpolatedY = cs.getValue( 0.15 );

Get all the spectrum of x coordinates, in a normalized space:

var arrayOfX = cs.getXSeriesInterpolated();

Note that if your canvas is 500px wide, you will get 500 values from 0 to 1 with regular spacing.

Along with this regular x go the interpolated y array:

var interpolatedYs = cs.getYSeriesInterpolated();

Events

CanvasSpliner can trigger two events:

  • When grabbing a point and moving it, called at every mousemove event
cs.on( "movePoint", function( csObj ){
  // here, csObj is the same object as cs. We can then call
  // cs.getXSeriesInterpolated() or
  // cs.getYSeriesInterpolated()
})
  • When a point was grabed but id just released
cs.on( "releasePoint", function( csObj ){
  // here, csObj is the same object as cs. We can then call
  // cs.getXSeriesInterpolated() or
  // cs.getYSeriesInterpolated()
})

Styling

If you want to adapt the styling of CanvasSpliner to match you own project, you can. After changing the styling, be sure you call the method cs.draw() to refresh!

  • Change the size of the control point with a number of pixels:
cs.setControlPointRadius( 6 )
  • Set the color of the control points, depending on the state. The state can be "idle", "hovered" or "grabbed" and the color must be a css-like string (ie. "rgba(244, 66, 167, 0.5)")
cs.setControlPointColor( state, color )
  • Set the color of the curve, depending on its state. The state can be "idle" or "moving" and the color must be a css-like string (ie. "rgba(244, 66, 167, 0.5)")
cs.setCurveColor( state, color )
  • Set the color of the grid with a css-like string (ie. "rgba(244, 66, 167, 0.5)")
cs.setGridColor( color )
  • Set the step of the grid (both horizontal and vertical) in normalized size ( in [0, 1])
cs.setGridStep( gs )
  • Set the color of the text with a css-like string (ie. "rgba(244, 66, 167, 0.5)")
cs.setTextColor( color )
  • Set the thickness of the curve in pixel
cs.setCurveThickness( t )
  • Set the color of the background with a css-like string (ie. "rgba(244, 66, 167, 0.5)") of set it to null / false or 0 to have a transparent background.
cs.setBackgroundColor( color )

Bundled in this module

CanvasSpliner uses George MacKerron's splines, wrapped in a propper npm package by Marcio Augusto Guimarães.