From: Ulrich-Matthias Schäfer Date: Thu, 8 Nov 2018 11:59:03 +0000 (+0100) Subject: implements `round()` (#916) X-Git-Tag: 3.0.0~44 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ae04f7599c822dec52af60c51aeb2ab135818c5d;p=svg.js.git implements `round()` (#916) --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e4a8cc..8fb06b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ The document follows the conventions described in [“Keep a CHANGELOG”](http: - added `SVG.Style` which can be created with `style()` or `fontface()` (#517) - added `EventTarget` which is a baseclass to get event abilities (#641) - added `Dom` which is a baseclass to get dom abilities +- added `round()` which lets you round attribues from a node ### Removed - removed `SVG.Array.split()` function diff --git a/dist/svg.js b/dist/svg.js index 6b05383..e80e6bd 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -6,7 +6,7 @@ * @copyright Wout Fierens * @license MIT * -* BUILT: Thu Nov 08 2018 12:02:46 GMT+0100 (GMT+01:00) +* BUILT: Thu Nov 08 2018 12:57:03 GMT+0100 (GMT+01:00) */; var SVG = (function () { 'use strict'; @@ -1618,6 +1618,26 @@ var SVG = (function () { element = makeInstance(element); this.node.parentNode.replaceChild(element.node, this.node); return element; + } + }, { + key: "round", + value: function round() { + var precision = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2; + var map$$1 = arguments.length > 1 ? arguments[1] : undefined; + var factor = Math.pow(10, precision); + var attrs = this.attr(); // If we have no map, build one from attrs + + if (!map$$1) { + map$$1 = Object.keys(attrs); + } // Holds rounded attributes + + + var newAttrs = {}; + map$$1.forEach(function (key) { + newAttrs[key] = Math.round(attrs[key] * factor) / factor; + }); + this.attr(newAttrs); + return this; } // Return id on string conversion }, { diff --git a/spec/spec/element.js b/spec/spec/element.js index 6ac74c4..1cb754e 100644 --- a/spec/spec/element.js +++ b/spec/spec/element.js @@ -1033,4 +1033,22 @@ describe('Element', function() { expect(g.defs() instanceof SVG.Defs).toBeTruthy() }) }) + describe('round()', function () { + it('round all attribues of a node to a specific precision', function () { + var rect = draw.rect(100.123456, 200.987654) + + expect(rect.round(2).attr()).toEqual(jasmine.objectContaining({ + width: 100.12, + height: 200.99 + })) + }) + it('round specified attributes of a node to a specific precision', function () { + var rect = draw.rect(100.123456, 200.987654) + + expect(rect.round(2, ['width']).attr()).toEqual(jasmine.objectContaining({ + width: 100.12, + height: 200.987654 + })) + }) + }) }) diff --git a/src/elements/Dom.js b/src/elements/Dom.js index e31c15c..75f3e94 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -188,6 +188,25 @@ export default class Dom extends EventTarget { return element } + round (precision = 2, map) { + const factor = 10 ** precision + const attrs = this.attr() + + // If we have no map, build one from attrs + if (!map) { + map = Object.keys(attrs) + } + + // Holds rounded attributes + const newAttrs = {} + map.forEach((key) => { + newAttrs[key] = Math.round(attrs[key] * factor) / factor + }) + + this.attr(newAttrs) + return this + } + // Return id on string conversion toString () { return this.id()