diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-05-20 09:36:17 +1000 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-05-20 09:36:17 +1000 |
commit | 0890c4d55fd39356347a5e7a2bb37c9436d2555c (patch) | |
tree | 7e268897fc3c33472576a77f2cc95c05aa73b969 /src | |
parent | 4ea53725a9021a136f6d81122dd78dc97a3e7da0 (diff) | |
download | svg.js-0890c4d55fd39356347a5e7a2bb37c9436d2555c.tar.gz svg.js-0890c4d55fd39356347a5e7a2bb37c9436d2555c.zip |
added geometry and positioning methods to `A` (#1110)
Diffstat (limited to 'src')
-rw-r--r-- | src/elements/A.js | 5 | ||||
-rw-r--r-- | src/elements/G.js | 75 | ||||
-rw-r--r-- | src/modules/core/containerGeometry.js | 69 |
3 files changed, 77 insertions, 72 deletions
diff --git a/src/elements/A.js b/src/elements/A.js index 0388314..478c36d 100644 --- a/src/elements/A.js +++ b/src/elements/A.js @@ -1,7 +1,8 @@ -import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js' +import { nodeOrNew, register, wrapWithAttrCheck, extend } from '../utils/adopter.js' import { registerMethods } from '../utils/methods.js' import { xlink } from '../modules/core/namespaces.js' import Container from './Container.js' +import * as containerGeometry from '../modules/core/containerGeometry.js' export default class A extends Container { constructor (node, attrs = node) { @@ -20,6 +21,8 @@ export default class A extends Container { } +extend(A, containerGeometry) + registerMethods({ Container: { // Create a hyperlink element diff --git a/src/elements/G.js b/src/elements/G.js index b460269..b3a999e 100644 --- a/src/elements/G.js +++ b/src/elements/G.js @@ -1,83 +1,16 @@ -import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js' -import { proportionalSize } from '../utils/utils.js' +import { nodeOrNew, register, wrapWithAttrCheck, extend } from '../utils/adopter.js' import { registerMethods } from '../utils/methods.js' import Container from './Container.js' -import Matrix from '../types/Matrix.js' -import Point from '../types/Point.js' +import * as containerGeometry from '../modules/core/containerGeometry.js' export default class G extends Container { constructor (node, attrs = node) { super(nodeOrNew('g', node), attrs) } - - dmove (dx, dy) { - this.children().forEach((child, i) => { - // Get the childs bbox - const bbox = child.bbox() - // Get childs matrix - const m = new Matrix(child) - // Translate childs matrix by amount and - // transform it back into parents space - const matrix = m.translate(dx, dy).transform(m.inverse()) - // Calculate new x and y from old box - const p = new Point(bbox.x, bbox.y).transform(matrix) - // Move element - child.move(p.x, p.y) - }) - - return this - } - - dx (dx) { - return this.dmove(dx, 0) - } - - dy (dy) { - return this.dmove(0, dy) - } - - height (height, box = this.bbox()) { - if (height == null) return box.height - return this.size(box.width, height, box) - } - - move (x = 0, y = 0, box = this.bbox()) { - const dx = x - box.x - const dy = y - box.y - - return this.dmove(dx, dy) - } - - size (width, height, box = this.bbox()) { - const p = proportionalSize(this, width, height, box) - const scaleX = p.width / box.width - const scaleY = p.height / box.height - - this.children().forEach((child, i) => { - const o = new Point(box).transform(new Matrix(child).inverse()) - child.scale(scaleX, scaleY, o.x, o.y) - }) - - return this - } - - width (width, box = this.bbox()) { - if (width == null) return box.width - return this.size(width, box.height, box) - } - - x (x, box = this.bbox()) { - if (x == null) return box.x - return this.move(x, box.y, box) - } - - y (y, box = this.bbox()) { - if (y == null) return box.y - return this.move(box.x, y, box) - } - } +extend(G, containerGeometry) + registerMethods({ Container: { // Create a group element diff --git a/src/modules/core/containerGeometry.js b/src/modules/core/containerGeometry.js new file mode 100644 index 0000000..50342b8 --- /dev/null +++ b/src/modules/core/containerGeometry.js @@ -0,0 +1,69 @@ +import Matrix from '../../types/Matrix.js' +import Point from '../../types/Point.js' +import { proportionalSize } from '../../utils/utils.js' + +export function dmove (dx, dy) { + this.children().forEach((child, i) => { + // Get the childs bbox + const bbox = child.bbox() + // Get childs matrix + const m = new Matrix(child) + // Translate childs matrix by amount and + // transform it back into parents space + const matrix = m.translate(dx, dy).transform(m.inverse()) + // Calculate new x and y from old box + const p = new Point(bbox.x, bbox.y).transform(matrix) + // Move element + child.move(p.x, p.y) + }) + + return this +} + +export function dx (dx) { + return this.dmove(dx, 0) +} + +export function dy (dy) { + return this.dmove(0, dy) +} + +export function height (height, box = this.bbox()) { + if (height == null) return box.height + return this.size(box.width, height, box) +} + +export function move (x = 0, y = 0, box = this.bbox()) { + const dx = x - box.x + const dy = y - box.y + + return this.dmove(dx, dy) +} + +export function size (width, height, box = this.bbox()) { + const p = proportionalSize(this, width, height, box) + const scaleX = p.width / box.width + const scaleY = p.height / box.height + + this.children().forEach((child, i) => { + const o = new Point(box).transform(new Matrix(child).inverse()) + child.scale(scaleX, scaleY, o.x, o.y) + }) + + return this +} + +export function width (width, box = this.bbox()) { + if (width == null) return box.width + return this.size(width, box.height, box) +} + +export function x (x, box = this.bbox()) { + if (x == null) return box.x + return this.move(x, box.y, box) +} + +export function y (y, box = this.bbox()) { + if (y == null) return box.y + return this.move(box.x, y, box) +} |