Need help with svg-path-properties?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

rveciana
162 Stars 17 Forks MIT License 169 Commits 0 Opened issues

Description

Pure Javascript alternative to path.getPointAtLength(t) and getTotalLength() functions. Works with Canvas & Node

Services available

!
?

Need anything else?

Contributors list

# 410,971
HTML
TypeScr...
119 commits
# 457,818
TypeScr...
Node.js
HTML
mosquit...
3 commits
# 89,169
CSS
HTML
React N...
React
2 commits
# 366,025
HTML
TypeScr...
2 commits
# 175,339
C++
jupyter
pytorch
HTML
1 commit

Build Status Coverage Status

svg-path-properties

Pure Javascript alternative to getPointAtLength(t) and getTotalLength() functions. Works with Canvas objects and when Node

JavaScript can access to path elements properties in a browser, such as its length and the point at a given length. Unfortunately, this can't be achieved using a Canvas element or when working with node. This library can be used to replace this need. It has no dependencies on other JavaScript libraries.

INSTALL

To use with npm, just type

npm install svg-path-properties

You can use it int he browser directly by including svg-path-properties.min.js from the dist directory


USAGE

The available methods are:

const path = require("svg-path-properties");
const properties = new path.svgPathProperties("M0,100 Q50,-50 100,100 T200,100");
const length = properties.getTotalLength();
const point = properties.getPointAtLength(200);
const tangent = properties.getTangentAtLength(200);
const allProperties = properties.getPropertiesAtLength(200);
const parts = properties.getParts();

Node:

const path = require("svg-path-properties");
const properties = new path.svgPathProperties("M0,100 Q50,-50 100,100 T200,100");

Including it from an import:

import { svgPathProperties } from "svg-path-properties";
const properties = new svgPathProperties("M0,100 Q50,-50 100,100 T200,100");

Including the script in the browser

Once the script tag has been included,

const properties = new svgPathProperties.svgPathProperties("M0,100 Q50,-50 100,100 T200,100");

Using new

Since svgPathProperties is a class, using new is the correct way to initilize it. For backwards compatibility reasons, the object can be get without it:

const properties = svgPathProperties("M0,100 Q50,-50 100,100 T200,100");

Some usage examples

Typescript

The TypeScript declaration file is available too, since version 0.5.0 From version 1.0.0, the whole library has been rewritten using TypeScript, and the types are auto-generated.

CREDITS

Some parts of the code are taken from other libraries or questions at StackOverflow:

For Bézier curves:

  • http://bl.ocks.org/hnakamur/e7efd0602bfc15f66fc5, https://gist.github.com/tunght13488/6744e77c242cc7a94859
  • http://stackoverflow.com/questions/11854907/calculate-the-length-of-a-segment-of-a-quadratic-bezier
  • http://pomax.github.io/bezierinfo
  • Arc elements calculation: https://github.com/MadLittleMods/svg-curve-lib/tree/f07d6008a673816f4cb74a3269164b430c3a95cb

For path parsing:

  • https://github.com/jkroso/parse-svg-path

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.