aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-08 12:59:03 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-08 12:59:03 +0100
commitae04f7599c822dec52af60c51aeb2ab135818c5d (patch)
tree200f63b61b5c265da460bd3a27767a2d34398cfc
parent6cafed8db9588c0b6b3b367aa822d9a0547e8e27 (diff)
downloadsvg.js-ae04f7599c822dec52af60c51aeb2ab135818c5d.tar.gz
svg.js-ae04f7599c822dec52af60c51aeb2ab135818c5d.zip
implements `round()` (#916)
-rw-r--r--CHANGELOG.md1
-rw-r--r--dist/svg.js22
-rw-r--r--spec/spec/element.js18
-rw-r--r--src/elements/Dom.js19
4 files changed, 59 insertions, 1 deletions
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 <wout@mick-wout.com>
* @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()