summaryrefslogtreecommitdiffstats
path: root/src/transform.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-10-25 23:28:12 +0200
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-10-25 23:28:12 +0200
commitcfdfcc529dedff770dc54e78d2900d9a790f5766 (patch)
tree7b59c282a7823ded1d182aca95da5d55815456b2 /src/transform.js
parent464af8b747389b7fdb569a933591c863b9be0f6b (diff)
downloadsvg.js-cfdfcc529dedff770dc54e78d2900d9a790f5766.tar.gz
svg.js-cfdfcc529dedff770dc54e78d2900d9a790f5766.zip
convert everything to es6 classes and imports
Diffstat (limited to 'src/transform.js')
-rw-r--r--src/transform.js117
1 files changed, 57 insertions, 60 deletions
diff --git a/src/transform.js b/src/transform.js
index 96c0aec..d6516a2 100644
--- a/src/transform.js
+++ b/src/transform.js
@@ -1,70 +1,67 @@
-/* global arrayToMatrix getOrigin isMatrixLike */
+import {arrayToMatrix, getOrigin, isMatrixLike} from './helpers.js'
+import Matrix from './Matrix.js'
+import {delimiter, transforms} from './regex.js'
-SVG.extend(SVG.Element, {
- // Reset all transformations
- untransform: function () {
- return this.attr('transform', null)
- },
+// Reset all transformations
+export function untransform () {
+ return this.attr('transform', null)
+}
- // merge the whole transformation chain into one matrix and returns it
- matrixify: function () {
- var matrix = (this.attr('transform') || '')
- // split transformations
- .split(SVG.regex.transforms).slice(0, -1).map(function (str) {
- // generate key => value pairs
- var kv = str.trim().split('(')
- return [kv[0],
- kv[1].split(SVG.regex.delimiter)
- .map(function (str) { return parseFloat(str) })
- ]
- })
- .reverse()
- // merge every transformation into one matrix
- .reduce(function (matrix, transform) {
- if (transform[0] === 'matrix') {
- return matrix.lmultiply(arrayToMatrix(transform[1]))
- }
- return matrix[transform[0]].apply(matrix, transform[1])
- }, new SVG.Matrix())
+// merge the whole transformation chain into one matrix and returns it
+export function matrixify () {
+ var matrix = (this.attr('transform') || '')
+ // split transformations
+ .split(transforms).slice(0, -1).map(function (str) {
+ // generate key => value pairs
+ var kv = str.trim().split('(')
+ return [kv[0],
+ kv[1].split(delimiter)
+ .map(function (str) { return parseFloat(str) })
+ ]
+ })
+ .reverse()
+ // merge every transformation into one matrix
+ .reduce(function (matrix, transform) {
+ if (transform[0] === 'matrix') {
+ return matrix.lmultiply(arrayToMatrix(transform[1]))
+ }
+ return matrix[transform[0]].apply(matrix, transform[1])
+ }, new Matrix())
- return matrix
- },
+ return matrix
+}
- // add an element to another parent without changing the visual representation on the screen
- toParent: function (parent) {
- if (this === parent) return this
- var ctm = this.screenCTM()
- var pCtm = parent.screenCTM().inverse()
+// add an element to another parent without changing the visual representation on the screen
+export function toParent (parent) {
+ if (this === parent) return this
+ var ctm = this.screenCTM()
+ var pCtm = parent.screenCTM().inverse()
- this.addTo(parent).untransform().transform(pCtm.multiply(ctm))
+ this.addTo(parent).untransform().transform(pCtm.multiply(ctm))
- return this
- },
+ return this
+}
- // same as above with parent equals root-svg
- toDoc: function () {
- return this.toParent(this.doc())
- }
-})
-
-SVG.extend(SVG.Element, {
-
- // Add transformations
- transform: function (o, relative) {
- // Act as a getter if no object was passed
- if (o == null || typeof o === 'string') {
- var decomposed = new SVG.Matrix(this).decompose()
- return decomposed[o] || decomposed
- }
+// same as above with parent equals root-svg
+export function toDoc () {
+ return this.toParent(this.doc())
+}
- if (!isMatrixLike(o)) {
- // Set the origin according to the defined transform
- o = {...o, origin: getOrigin(o, this)}
- }
+// Add transformations
+export function transform (o, relative) {
+ // Act as a getter if no object was passed
+ if (o == null || typeof o === 'string') {
+ var decomposed = new Matrix(this).decompose()
+ return decomposed[o] || decomposed
+ }
- // The user can pass a boolean, an SVG.Element or an SVG.Matrix or nothing
- var cleanRelative = relative === true ? this : (relative || false)
- var result = new SVG.Matrix(cleanRelative).transform(o)
- return this.attr('transform', result)
+ if (!isMatrixLike(o)) {
+ // Set the origin according to the defined transform
+ o = {...o, origin: getOrigin(o, this)}
}
-})
+
+ // The user can pass a boolean, an Element or an Matrix or nothing
+ var cleanRelative = relative === true ? this : (relative || false)
+ var result = new Matrix(cleanRelative).transform(o)
+ return this.attr('transform', result)
+}