* You can checkout the appendix table for commands jpaths supports
jpaths.define()
You can extend your shape by jpaths.define() easily
// define the `rect` shape
jpaths.define('rect', function(params) {
if (params.length != 2) throw new Error('The rect command should have 2 parameters');
var dx = params.shift();
var dy = params.shift();
return ['h', dx, 'v', dy, 'h', -dx, 'v', -dy, 'z'];
});
// append the `rect` shape
var path = jpaths();
path.append('M', 100, 100);
path.append('rect', 40, 30); // path.toString() will be 'M100,100h40v30h-40v-30z'
Conversions
The JPath object can convert to different representations
var path = jpaths(['M', 0, 0, 'R', 10, 0, 0, 10, 10]);
// ziped
console.log(path.toString()); // 'M0,0A10,10,0,0,0,10,10'
// formated
console.log(path.toString('%s')); // 'M 0,0 A 10 10 0 0 0 10,10' only coordinate use comma to split
console.log(path.toString('%n')); // 'M 0,0\nA 10 10 0 0 0 10,10' commands are split with line-break
.toArray() / .valueOf()
Converts JPath object to a 2-dimension array, see example below.
var path = jpaths('M0,0h3v4L5,6');
console.log(path.toArray()); // [['M', 0, 0], ['h', 3], ['v', 4], ['L', 5, 6]]
console.log(path.valueOf()); // the same way to `toArray()`
.toAbsolute()
Converts JPath object to another, which commands use absolute coordinates.
var path = jpaths('M0,0h3v4l1,1');
console.log(path.toAbsolute().toString()); // 'M0,0H3V4L4,5'
.toNormalized()
Converts JPath object to a normalized path. A normalized path string contains only 4 command types: M, L, C, z. All command coordinates are absolute.
var path = jpaths('...');
path = path.normalize(); // returns a new jpath object contains only 4 normalize command types.
.toCurve()
Converts JPath object to another which contains only M, C and z commands.
var path = jpaths('M0,0H10');
path = path.toCurve(); // path.toString() is 'M0,0C0,0,10,0,10,0'
Calculations
.length()
Calculate the path length (length-along-path)
var path = jpaths('...');
var length = path.length();
.at(position)
Calculate the point coordinate, tangent vector and tangent angle at the specific position along the path. Returns an object like this:
var path = jpaths('M0,0L10,10');
var vertex = path.at(2); // returns {
// point: [1.414, 1.414] // sqrt(2)
// tangent: [0.717, 0.717] // vector length will be 1
// rotate: 0.7854 // PI/4
// }
Notice: If the position is not in range of the path length, an error will throw
.cut(position)
Cut the JPath object into 2 JPath objects from the position along the path
var path = jpaths('M0,0H100');
var cutted = path.cut(50);
console.log(cutted[0].toString()); // 'M0,0H50'
console.log(cutted[1].toString()); // 'M50,0H100'
.sub(position, length)
Calculate the sub path from specified position and length. If length is not specified, return sub path from position to end.
var path = jpaths('M0,0H100');
var subpath = path.sub(20, 30); // 'M20,0H50'
.transform(matrix)
Transform the path using the specified matrix. The matrix should be a 6-length array.
var path = jpaths('M0,0H100');
var transformed = path.transform([1, 0, 0, 1, 10, 10]); // transformed.toString() will be 'M10,10L110,10'
.tween(dest, t)
Calculate the tween from the current JPath object to the destination JPath object along t. t ranges in [0, 1].
var path = jpaths('M0,0H100');
var dest = jpaths('M0,0H50');
var tween = path.tween(dest, 0.5); // tween.toString() will be 'M0,0L75,0'
Rendering
The JPath object can be rendered to a canvas 2d context or a svg path element.
var path = jpath('M0,0L100,0,0,100');
// render to canvas
var ctx = canvas.getContext('2d');
path.render(ctx);
ctx.fillStyle = 'red';
ctx.fill();
// render to svg path element
var pathElement = document.querySelector('svg #triangle');
path.render(pathElement); // in fact, this equals to pathElement.setAttribute('d', path);
The JPath Object
The jpath() method returns the JPath object, which is instanceof jpath.JPath. You can construct a JPath instance with new operator.
var path = new jpath.JPath();
// same with `var path = jpath();`
The detail usage of the JPath object is described above. Here’s the list:
JPaths API Design
Create
Create the
JPathobject with thejpaths()methodjpaths()
How to create a
JPathobject:Building
.append()
Append standard commands (according to SVG Path Commands)
* You can checkout the appendix table for commands jpaths supports
jpaths.define()
You can extend your shape by
jpaths.define()easilyConversions
The
JPathobject can convert to different representations.toString()
Convert the
JPathobject to a standard SVG path string. Any drawing define will be converted..toArray() / .valueOf()
Converts
JPathobject to a 2-dimension array, see example below..toAbsolute()
Converts
JPathobject to another, which commands use absolute coordinates..toNormalized()
Converts
JPathobject to a normalized path. A normalized path string contains only 4 command types:M,L,C,z. All command coordinates are absolute..toCurve()
Converts
JPathobject to another which contains onlyM,Candzcommands.Calculations
.length()
Calculate the path length (length-along-path)
.at(position)
Calculate the point coordinate, tangent vector and tangent angle at the specific position along the path. Returns an object like this:
.cut(position)
Cut the
JPathobject into 2JPathobjects from the position along the path.sub(position, length)
Calculate the sub path from specified position and length. If length is not specified, return sub path from position to end.
.transform(matrix)
Transform the path using the specified matrix. The matrix should be a 6-length array.
.tween(dest, t)
Calculate the tween from the current
JPathobject to the destinationJPathobject alongt.tranges in [0, 1].Rendering
The
JPathobject can be rendered to a canvas 2d context or a svg path element.The JPath Object
The
jpath()method returns theJPathobject, which is instanceofjpath.JPath. You can construct aJPathinstance withnewoperator.The detail usage of the
JPathobject is described above. Here’s the list:constructor jpath()constructor jpath(pathString).append(commandName, params...).append(pathString).append(pathArray).toString().toArray().toAbsolute().toNormalized().toCurve().length().at(position).cut(position).sub(position, length).transform(matrix).tween(dest, t).render(CanvasRenderingContext2D ctx).render(SVGPathElement element)Appendix.I. Supported Command Table
* Non-standard