summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2019-01-12 20:32:39 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2019-01-12 20:32:39 +0100
commit60792807eee29f1b748c9b12d308c477f7c1227d (patch)
tree27f5c46c02ea0acbec71668935ef72cf2749698e /src
parentf5eff8745af43fcfcc2e837d89d549cb66220d99 (diff)
downloadsvg.js-60792807eee29f1b748c9b12d308c477f7c1227d.tar.gz
svg.js-60792807eee29f1b748c9b12d308c477f7c1227d.zip
Fix move and size of groups, removed setting of a default font so we dont act against user intention, fixed bug in `font()`
Diffstat (limited to 'src')
-rw-r--r--src/elements/Ellipse.js2
-rw-r--r--src/elements/G.js77
-rw-r--r--src/elements/Text.js4
-rw-r--r--src/elements/Tspan.js8
-rw-r--r--src/modules/optional/sugar.js1
-rw-r--r--src/types/Box.js6
-rw-r--r--src/types/Point.js21
-rw-r--r--src/types/PointArray.js2
-rw-r--r--src/utils/adopter.js24
9 files changed, 93 insertions, 52 deletions
diff --git a/src/elements/Ellipse.js b/src/elements/Ellipse.js
index 0350f1f..beba228 100644
--- a/src/elements/Ellipse.js
+++ b/src/elements/Ellipse.js
@@ -28,7 +28,7 @@ extend(Ellipse, circled)
registerMethods('Container', {
// Create an ellipse
- ellipse: wrapWithAttrCheck(function (width, height) {
+ ellipse: wrapWithAttrCheck(function (width = 0, height = width) {
return this.put(new Ellipse()).size(width, height).move(0, 0)
})
})
diff --git a/src/elements/G.js b/src/elements/G.js
index 8171fed..a9e8b55 100644
--- a/src/elements/G.js
+++ b/src/elements/G.js
@@ -2,7 +2,8 @@ import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js'
import { proportionalSize } from '../utils/utils.js'
import { registerMethods } from '../utils/methods.js'
import Container from './Container.js'
-import SVGNumber from '../types/SVGNumber.js'
+import Matrix from '../types/Matrix.js'
+import Point from '../types/Point.js'
export default class G extends Container {
constructor (node) {
@@ -11,70 +12,68 @@ export default class G extends Container {
x (x, box = this.bbox()) {
if (x == null) return box.x
-
- this.children().dx(x - box.x)
- return this
+ return this.move(x, box.y, box)
}
y (y, box = this.bbox()) {
if (y == null) return box.y
-
- this.children().dy(y - box.y)
- return this
+ return this.move(box.x, y, box)
}
- move (x, y) {
- const box = this.bbox()
- return this.x(x, box).y(y, box)
+ move (x = 0, y = 0, box = this.bbox()) {
+ const dx = x - box.x
+ const dy = y - box.y
+
+ return this.dmove(dx, dy)
}
dx (dx) {
- return this.children().dx(dx)
+ return this.dmove(dx, 0)
}
dy (dy) {
- return this.children().dy(dy)
+ return this.dmove(0, dy)
}
- width (width, box = this.bbox()) {
- if (width == null) return box.width
-
- const scale = width / box.width
-
- this.each(function () {
- const _width = this.width()
- const _x = this.x()
-
- this.width(_width * scale)
- this.x((_x - box.x) * scale + box.x)
+ 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
}
+ width (width, box = this.bbox()) {
+ if (width == null) return box.width
+ return this.size(width, box.height, box)
+ }
+
height (height, box = this.bbox()) {
if (height == null) return box.height
-
- const scale = height / box.height
-
- this.each(function () {
- const _height = this.height()
- const _y = this.y()
-
- this.height(_height * scale)
- this.y((_y - box.y) * scale + box.y)
- })
-
- return this
+ return this.size(box.width, height, box)
}
- size (width, height) {
- const box = this.bbox()
+ 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(new SVGNumber(p.width), box)
- .height(new SVGNumber(p.height), box)
}
}
diff --git a/src/elements/Text.js b/src/elements/Text.js
index db9c2ee..cfa3f51 100644
--- a/src/elements/Text.js
+++ b/src/elements/Text.js
@@ -5,7 +5,6 @@ import {
register,
wrapWithAttrCheck
} from '../utils/adopter.js'
-import { attrs } from '../modules/core/defaults.js'
import { registerMethods } from '../utils/methods.js'
import SVGNumber from '../types/SVGNumber.js'
import Shape from './Shape.js'
@@ -20,9 +19,6 @@ export default class Text extends Shape {
this.dom.leading = new SVGNumber(1.3) // store leading value for rebuilding
this._rebuild = true // enable automatic updating of dy values
this._build = false // disable build mode for adding multiple lines
-
- // set default font
- this.attr('font-family', attrs['font-family'])
}
// Move over x-axis
diff --git a/src/elements/Tspan.js b/src/elements/Tspan.js
index abd032f..64895fc 100644
--- a/src/elements/Tspan.js
+++ b/src/elements/Tspan.js
@@ -4,7 +4,9 @@ import {
register,
wrapWithAttrCheck
} from '../utils/adopter.js'
+import { globals } from '../utils/window.js'
import { registerMethods } from '../utils/methods.js'
+import SVGNumber from '../types/SVGNumber.js'
import Text from './Text.js'
import * as textable from '../modules/core/textable.js'
@@ -41,8 +43,12 @@ export default class Tspan extends Text {
// mark new line
this.dom.newLined = true
+ var fontSize = globals.window.getComputedStyle(this.node)
+ .getPropertyValue('font-size')
+ var dy = t.dom.leading * new SVGNumber(fontSize)
+
// apply new position
- return this.dy(t.dom.leading * t.attr('font-size')).attr('x', t.x())
+ return this.dy(dy).attr('x', t.x())
}
}
diff --git a/src/modules/optional/sugar.js b/src/modules/optional/sugar.js
index c05512d..7aba0f7 100644
--- a/src/modules/optional/sugar.js
+++ b/src/modules/optional/sugar.js
@@ -136,6 +136,7 @@ registerMethods([ 'Element', 'Runner' ], {
font: function (a, v) {
if (typeof a === 'object') {
for (v in a) this.font(v, a[v])
+ return this
}
return a === 'leading'
diff --git a/src/types/Box.js b/src/types/Box.js
index eb43d07..8cbda30 100644
--- a/src/types/Box.js
+++ b/src/types/Box.js
@@ -59,6 +59,10 @@ export default class Box {
}
transform (m) {
+ if (!(m instanceof Matrix)) {
+ m = new Matrix(m)
+ }
+
let xMin = Infinity
let xMax = -Infinity
let yMin = Infinity
@@ -130,7 +134,7 @@ export function bbox () {
clone.remove()
return box
} catch (e) {
- throw new Error('Getting bbox of element "' + el.node.nodeName + '" is not possible')
+ throw new Error('Getting bbox of element "' + el.node.nodeName + '" is not possible. ' + e.toString())
}
}))
}
diff --git a/src/types/Point.js b/src/types/Point.js
index f1c85a1..329b37d 100644
--- a/src/types/Point.js
+++ b/src/types/Point.js
@@ -1,3 +1,5 @@
+import Matrix from './Matrix.js'
+
export default class Point {
// Initialize
constructor (...args) {
@@ -25,14 +27,23 @@ export default class Point {
return new Point(this)
}
- // transform point with matrix
transform (m) {
+ return this.clone().transformO(m)
+ }
+
+ // Transform point with matrix
+ transformO (m) {
+ if (!Matrix.isMatrixLike(m)) {
+ m = new Matrix(m)
+ }
+
+ let { x, y } = this
+
// Perform the matrix multiplication
- var x = m.a * this.x + m.c * this.y + m.e
- var y = m.b * this.x + m.d * this.y + m.f
+ this.x = m.a * x + m.c * y + m.e
+ this.y = m.b * x + m.d * y + m.f
- // Return the required point
- return new Point(x, y)
+ return this
}
toArray () {
diff --git a/src/types/PointArray.js b/src/types/PointArray.js
index 2246bbd..6a869d7 100644
--- a/src/types/PointArray.js
+++ b/src/types/PointArray.js
@@ -63,7 +63,7 @@ extend(PointArray, {
// Odd number of coordinates is an error. In such cases, drop the last odd coordinate.
if (array.length % 2 !== 0) array.pop()
- // wrap points in two-tuples and parse points as floats
+ // wrap points in two-tuples
for (var i = 0, len = array.length; i < len; i = i + 2) {
points.push([ array[i], array[i + 1] ])
}
diff --git a/src/utils/adopter.js b/src/utils/adopter.js
index f091c96..45e9bd3 100644
--- a/src/utils/adopter.js
+++ b/src/utils/adopter.js
@@ -138,3 +138,27 @@ export function wrapWithAttrCheck (fn) {
}
}
}
+
+export function invent (config) {
+ // Create element initializer
+ var initializer = typeof config.create === 'function'
+ ? config.create
+ : function (node) {
+ this.constructor(node || create(config.create))
+ }
+
+ // Inherit prototype
+ if (config.inherit) {
+ /* eslint new-cap: off */
+ initializer.prototype = new config.inherit()
+ initializer.prototype.constructor = initializer
+ }
+
+ // Extend with methods
+ if (config.extend) { extend(initializer, config.extend) }
+
+ // Attach construct method to parent
+ if (config.construct) { extend(config.parent || elements['Container'], config.construct) }
+
+ return initializer
+}