diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-08 12:59:03 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-08 12:59:03 +0100 |
commit | ae04f7599c822dec52af60c51aeb2ab135818c5d (patch) | |
tree | 200f63b61b5c265da460bd3a27767a2d34398cfc /src | |
parent | 6cafed8db9588c0b6b3b367aa822d9a0547e8e27 (diff) | |
download | svg.js-ae04f7599c822dec52af60c51aeb2ab135818c5d.tar.gz svg.js-ae04f7599c822dec52af60c51aeb2ab135818c5d.zip |
implements `round()` (#916)
Diffstat (limited to 'src')
-rw-r--r-- | src/elements/Dom.js | 19 |
1 files changed, 19 insertions, 0 deletions
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() |