The SVG Path Interpolator produces point data representing interpolated values within an SVG path.
npm install svg-path-interpolatorbash
npm install svg-path-interpolator --save
`
or as a cli
`bash
npm install -g svg-path-interpolator
`
Usage
$3
Create a config.json somewhere in your project. See the sample.config.json for configuration options.
`json
{
"joinPathData": false,
"minDistance": 0.5,
"roundToNearest": 0.25,
"sampleFrequency": 0.001,
"pretty": false,
"prettyIndent": 0
}
`
Then from your terminal, type
`bash
svgpi ./path/to/config.json ./path/to/target.svg ./output/fileName.json
`
$3
Copy all files in the lib/ directory to svg-interpolator/ on your web server then point a script tag to it.
`html
`
The SVGPathInterpolator will be defined as a global and can be used anywhere
`html
`
$3
`ts
import { createInterpolator } from 'svg-path-interpolator';
// ...
const interpolator = await createInterpolator({
joinPathData: true,
minDistance: 0.5,
roundToNearest: 0.25,
sampleFrequency: 0.001,
}, '../svg-interpolator/sax-wasm.wasm');
// ------ Get the SVG as a Uint8Array Via fetch ----------
const response = await fetch('./path-to-svg.svg');
if (!response.ok) {
return;
}
const bytes = new Uint8Array(await response.arrayBuffer());
// -------------------------------------------------------
// --------------- OR from a DOM element -----------------
const svg = document.querySelector('svg.my-svg');
const bytes = new TextEncoder().encode(svg.outerHTML);
//--------------------------------------------------------
const paths = interpolator.processSVG(bytes);
`
$3
When joinPathData is true, all path data is joined in a single array as the output. When false, each path is separated by the path id attribute in a json object as the output. If no id attribute exists on the path, a unique id is created.
$3
minDistance is the minimum distance between the current and previous points when sampling. If a sample results in a distance less than the specified value, the point is discarded.
$3
roundToNearest is useful when snapping to fractional pixel values. For example, if roundToNearest is .25, a sample resulting in the point 2.343200092,4.6100923 will round to 2.25,4.5
$3
sampleFrequency determines the increment of t when sampling. If sampleFrequency is set to .001 , since t iterates from 0 to 1, there will be 1000 points sampled per command but only points that are greater than minDistance are captured.
$3
When true, pretty creates formatted json output
$3
Then number of spaces to indent when pretty is true`