Skip to content

Commit

Permalink
Add support for fractionalDigits in toPathData().
Browse files Browse the repository at this point in the history
  • Loading branch information
fdb committed Mar 16, 2018
1 parent 18fc456 commit 6ba5c0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/libraries/vg/objects/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var CLOSE_COMMAND = Object.freeze({ type: CLOSE });

var KAPPA = 0.5522847498307936; // (-1 + Math.sqrt(2)) / 3 * 4

function _roundCoord(n) {
function _roundCoord(n, fractionDigits=3) {
if (n % 1 === 0) return n;
return n.toFixed(3);
return n.toFixed(fractionDigits);
}

function _cloneCommand(cmd) {
Expand Down Expand Up @@ -444,22 +444,22 @@ Path.prototype.resampleByLength = function (segmentLength, options) {
return new Path(commands, this.fill, this.stroke, this.strokeWidth);
};

Path.prototype.toPathData = function () {
Path.prototype.toPathData = function (fractionDigits=3) {
var i, d, cmd, x, y, x1, y1, x2, y2;
d = '';
for (i = 0; i < this.commands.length; i += 1) {
cmd = this.commands[i];
if (cmd.x !== undefined) {
x = _roundCoord(math.clamp(cmd.x, -9999, 9999));
y = _roundCoord(math.clamp(cmd.y, -9999, 9999));
x = _roundCoord(math.clamp(cmd.x, -9999, 9999), fractionDigits);
y = _roundCoord(math.clamp(cmd.y, -9999, 9999), fractionDigits);
}
if (cmd.x1 !== undefined) {
x1 = _roundCoord(math.clamp(cmd.x1, -9999, 9999));
y1 = _roundCoord(math.clamp(cmd.y1, -9999, 9999));
x1 = _roundCoord(math.clamp(cmd.x1, -9999, 9999), fractionDigits);
y1 = _roundCoord(math.clamp(cmd.y1, -9999, 9999), fractionDigits);
}
if (cmd.x2 !== undefined) {
x2 = _roundCoord(math.clamp(cmd.x2, -9999, 9999));
y2 = _roundCoord(math.clamp(cmd.y2, -9999, 9999));
x2 = _roundCoord(math.clamp(cmd.x2, -9999, 9999), fractionDigits);
y2 = _roundCoord(math.clamp(cmd.y2, -9999, 9999), fractionDigits);
}
if (cmd.type === MOVETO) {
if (!isNaN(x) && !isNaN(y)) {
Expand Down
2 changes: 1 addition & 1 deletion test/vgSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('A path', function () {
assert.equal(p.toSVG(), '<path d="M10 20L30 40Z"/>');
});

it('can probably round coordinates', function () {
it('can round coordinates', function () {
var p;
p = new vg.Path();
p.moveTo(Math.PI, Math.E);
Expand Down

0 comments on commit 6ba5c0f

Please sign in to comment.