Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to return SVG as JSON Array #12

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ trace.loadImage('path/to/image.png', function(err) {

trace.getSVG(); // returns SVG document contents
trace.getPathTag(); // will return just <path> tag
trace.getJson(); // returns SVG document contents in JSON format
trace.getSymbol('traced-image'); // will return <symbol> tag with given ID
});

Expand Down Expand Up @@ -181,4 +182,4 @@ The GNU General Public License version 2 (GPLv2). Please see [License File](LICE
[multilevel-thresholding]: http://www.iis.sinica.edu.tw/page/jise/2001/200109_01.pdf
[potrace-by-kilobtye]: https://github.com/kilobtye/potrace
[potrace-js-demo]: http://kilobtye.github.io/potrace/
[jimp]: https://github.com/oliver-moran/jimp
[jimp]: https://github.com/oliver-moran/jimp
50 changes: 49 additions & 1 deletion lib/Potrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,54 @@ Potrace.prototype = {
: '') +
'\t' + this.getPathTag(this._params.color, scale) + '\n' +
'</svg>';
},

/**
* Generates a JSON object with path information
*
* @param {string} [fillColor=auto] - overrides color from parameters
* @param {Object} [scale={ x: 1, y: 1 }] - override svg scale
* @param {number} [scale.x=1] - scale of the x dimension
* @param {number} [scale.y=1] - scale of the y dimension
* @param {String} [stroke=none] - override svg stroke value
* @param {boolean} [json=false] - return result as a json array of paths
* @returns {Array.<object>}
*/
getJson: function(fillColor='auto', scale={ x: 1, y: 1 }, stroke='black') {
fillColor = arguments.length === 0 ? this._params.color : fillColor;

if (fillColor === Potrace.COLOR_AUTO) {
fillColor = this._params.blackOnWhite ? 'black' : 'white';
}

if (!this._imageLoaded) {
throw new Error('Image should be loaded first');
}

if (!this._processed) {
this._bmToPathlist();
this._processPath();
this._processed = true;
}

const tags = [];
tags.push({
width: this._params.width || this._luminanceData.width,
height: this._params.height || this._luminanceData.height
});
this._pathlist.forEach(path => {
tags.push({
d: utils.renderCurve(path.curve, scale),
fill: fillColor,
stroke: stroke,
fillRule: 'evenodd'
});
});
tags.forEach((path, index) => {
path.id = index;
});

return tags;
}
};

Expand All @@ -1167,4 +1215,4 @@ module.exports = Potrace;
* Jimp module
* @external Jimp
* @see https://www.npmjs.com/package/jimp
*/
*/