aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-12 14:51:34 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-12 14:51:34 +0100
commitbf6e2aeb13f9a4bee2be1f8f7a70ca1a73215245 (patch)
tree0614edfac92e0ab9ff44144b79aa5802211ef5e0 /src
parent334d9c73c2f74679a93b1d7b3e39b614f6444faa (diff)
downloadsvg.js-bf6e2aeb13f9a4bee2be1f8f7a70ca1a73215245.tar.gz
svg.js-bf6e2aeb13f9a4bee2be1f8f7a70ca1a73215245.zip
remove native() methods, add methods of types directly to elemenet
Diffstat (limited to 'src')
-rw-r--r--src/elements/Dom.js5
-rw-r--r--src/elements/Element.js15
-rw-r--r--src/modules/core/parser.js4
-rw-r--r--src/types/Box.js22
-rw-r--r--src/types/Matrix.js58
-rw-r--r--src/types/Point.js25
6 files changed, 46 insertions, 83 deletions
diff --git a/src/elements/Dom.js b/src/elements/Dom.js
index 6d35f1e..55d5858 100644
--- a/src/elements/Dom.js
+++ b/src/elements/Dom.js
@@ -137,11 +137,6 @@ export default class Dom extends EventTarget {
return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector)
}
- // Returns the svg node to call native svg methods on it
- native () {
- return this.node
- }
-
// Returns the parent element instance
parent (type) {
var parent = this
diff --git a/src/elements/Element.js b/src/elements/Element.js
index 3b96bf4..456ddad 100644
--- a/src/elements/Element.js
+++ b/src/elements/Element.js
@@ -1,5 +1,14 @@
-import { getClass, makeInstance, register, root } from '../utils/adopter.js'
+import { bbox, rbox } from '../types/Box.js'
+import { ctm, screenCTM } from '../types/Matrix.js'
+import {
+ extend,
+ getClass,
+ makeInstance,
+ register,
+ root
+} from '../utils/adopter.js'
import { globals } from '../utils/window.js'
+import { point } from '../types/Point.js'
import { proportionalSize } from '../utils/utils.js'
import { reference } from '../modules/core/regex.js'
import Dom from './Dom.js'
@@ -145,4 +154,8 @@ export default class Element extends Dom {
}
}
+extend(Element, {
+ bbox, rbox, point, ctm, screenCTM
+})
+
register(Element)
diff --git a/src/modules/core/parser.js b/src/modules/core/parser.js
index ccbbc54..1ff2380 100644
--- a/src/modules/core/parser.js
+++ b/src/modules/core/parser.js
@@ -1,10 +1,10 @@
-import Doc from '../../elements/Doc.js'
import { globals } from '../../utils/window.js'
+import { makeInstance } from '../../utils/adopter.js'
export default function parser () {
// Reuse cached element if possible
if (!parser.nodes) {
- let svg = new Doc().size(2, 0)
+ let svg = makeInstance().size(2, 0)
svg.node.cssText = [
'opacity: 0',
'position: absolute',
diff --git a/src/types/Box.js b/src/types/Box.js
index 8c1c4ca..e55f114 100644
--- a/src/types/Box.js
+++ b/src/types/Box.js
@@ -125,19 +125,17 @@ function getBox (cb) {
return box
}
+export function bbox () {
+ return new Box(getBox.call(this, (node) => node.getBBox()))
+}
+
+export function rbox (el) {
+ let box = new Box(getBox.call(this, (node) => node.getBoundingClientRect()))
+ if (el) return box.transform(el.screenCTM().inverse())
+ return box.addOffset()
+}
+
registerMethods({
- Element: {
- // Get bounding box
- bbox () {
- return new Box(getBox.call(this, (node) => node.getBBox()))
- },
-
- rbox (el) {
- let box = new Box(getBox.call(this, (node) => node.getBoundingClientRect()))
- if (el) return box.transform(el.screenCTM().inverse())
- return box.addOffset()
- }
- },
viewbox: {
viewbox (x, y, width, height) {
// act as getter
diff --git a/src/types/Matrix.js b/src/types/Matrix.js
index ee12488..a1eb317 100644
--- a/src/types/Matrix.js
+++ b/src/types/Matrix.js
@@ -1,12 +1,7 @@
import { delimiter } from '../modules/core/regex.js'
import { radians } from '../utils/utils.js'
-import { registerMethods } from '../utils/methods.js'
import Element from '../elements/Element.js'
import Point from './Point.js'
-import parser from '../modules/core/parser.js'
-
-// Create matrix array for looping
-const abcdef = 'abcdef'.split('')
function closeEnough (a, b, threshold) {
return Math.abs(b - a) < (threshold || 1e-6)
@@ -379,19 +374,6 @@ export default class Matrix {
return this.clone().aroundO(cx, cy, matrix)
}
- // Convert to native SVGMatrix
- native () {
- // create new matrix
- var matrix = parser().svg.node.createSVGMatrix()
-
- // update with current values
- for (var i = abcdef.length - 1; i >= 0; i--) {
- matrix[abcdef[i]] = this[abcdef[i]]
- }
-
- return matrix
- }
-
// Check if two matrices are equal
equals (other) {
var comp = new Matrix(other)
@@ -499,26 +481,20 @@ export default class Matrix {
}
}
-registerMethods({
- Element: {
- // Get current matrix
- ctm () {
- return new Matrix(this.node.getCTM())
- },
-
- // Get current screen matrix
- screenCTM () {
- /* https://bugzilla.mozilla.org/show_bug.cgi?id=1344537
- This is needed because FF does not return the transformation matrix
- for the inner coordinate system when getScreenCTM() is called on nested svgs.
- However all other Browsers do that */
- if (typeof this.isRoot === 'function' && !this.isRoot()) {
- var rect = this.rect(1, 1)
- var m = rect.node.getScreenCTM()
- rect.remove()
- return new Matrix(m)
- }
- return new Matrix(this.node.getScreenCTM())
- }
- }
-})
+export function ctm () {
+ return new Matrix(this.node.getCTM())
+}
+
+export function screenCTM () {
+ /* https://bugzilla.mozilla.org/show_bug.cgi?id=1344537
+ This is needed because FF does not return the transformation matrix
+ for the inner coordinate system when getScreenCTM() is called on nested svgs.
+ However all other Browsers do that */
+ if (typeof this.isRoot === 'function' && !this.isRoot()) {
+ var rect = this.rect(1, 1)
+ var m = rect.node.getScreenCTM()
+ rect.remove()
+ return new Matrix(m)
+ }
+ return new Matrix(this.node.getScreenCTM())
+}
diff --git a/src/types/Point.js b/src/types/Point.js
index 6a2b968..27d81ea 100644
--- a/src/types/Point.js
+++ b/src/types/Point.js
@@ -1,6 +1,3 @@
-import { registerMethods } from '../utils/methods.js'
-import parser from '../modules/core/parser.js'
-
export default class Point {
// Initialize
constructor (...args) {
@@ -28,17 +25,6 @@ export default class Point {
return new Point(this)
}
- // Convert to native SVGPoint
- native () {
- // create new point
- var point = parser().svg.node.createSVGPoint()
-
- // update with current values
- point.x = this.x
- point.y = this.y
- return point
- }
-
// transform point with matrix
transform (m) {
// Perform the matrix multiplication
@@ -54,11 +40,6 @@ export default class Point {
}
}
-registerMethods({
- Element: {
- // Get point
- point: function (x, y) {
- return new Point(x, y).transform(this.screenCTM().inverse())
- }
- }
-})
+export function point (x, y) {
+ return new Point(x, y).transform(this.screenCTM().inverse())
+}