summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bbox.js68
-rw-r--r--src/element.js1
-rw-r--r--src/rbox.js60
3 files changed, 86 insertions, 43 deletions
diff --git a/src/bbox.js b/src/bbox.js
index 22c5bcf..b05e90c 100644
--- a/src/bbox.js
+++ b/src/bbox.js
@@ -1,29 +1,61 @@
SVG.BBox = function(element) {
var box
+
+ /* initialize zero box */
+ this.x = 0
+ this.y = 0
+ this.width = 0
+ this.height = 0
- /* actual, native bounding box */
- try {
- box = element.node.getBBox()
- } catch(e) {
- box = {
- x: element.node.clientLeft
- , y: element.node.clientTop
- , width: element.node.clientWidth
- , height: element.node.clientHeight
+ /* get values if element is given */
+ if (element) {
+ try {
+ /* actual, native bounding box */
+ box = element.node.getBBox()
+ } catch(e) {
+ /* fallback for some browsers */
+ box = {
+ x: element.node.clientLeft
+ , y: element.node.clientTop
+ , width: element.node.clientWidth
+ , height: element.node.clientHeight
+ }
}
+
+ /* include translations on x an y */
+ this.x = box.x + element.trans.x
+ this.y = box.y + element.trans.y
+
+ /* plain width and height */
+ this.width = box.width * element.trans.scaleX
+ this.height = box.height * element.trans.scaleY
}
- /* include translations on x an y */
- this.x = box.x + element.trans.x
- this.y = box.y + element.trans.y
-
- /* plain width and height */
- this.width = box.width * element.trans.scaleX
- this.height = box.height * element.trans.scaleY
-
/* add the center */
this.cx = this.x + this.width / 2
this.cy = this.y + this.height / 2
-} \ No newline at end of file
+}
+
+//
+SVG.extend(SVG.BBox, {
+ // merge bounding box with another, return a new instance
+ merge: function(box) {
+ var b = new SVG.BBox()
+
+ /* merge box */
+ b.x = Math.min(this.x, box.x)
+ b.y = Math.min(this.y, box.y)
+ b.width = Math.max(this.x + this.width, box.x + box.width) - b.x
+ b.height = Math.max(this.y + this.height, box.y + box.height) - b.y
+
+ /* add the center */
+ b.cx = b.x + b.width / 2
+ b.cy = b.y + b.height / 2
+
+ return b
+ }
+
+})
+
diff --git a/src/element.js b/src/element.js
index 3149d22..8d07762 100644
--- a/src/element.js
+++ b/src/element.js
@@ -178,6 +178,7 @@ SVG.extend(SVG.Element, {
}
// Manage transformations
, transform: function(o, v) {
+
if (arguments.length == 0) {
/* act as a getter if no argument is given */
return this.trans
diff --git a/src/rbox.js b/src/rbox.js
index 02405b0..5eb474f 100644
--- a/src/rbox.js
+++ b/src/rbox.js
@@ -1,32 +1,42 @@
// Get the rectangular box of a given element
SVG.RBox = function(element) {
- var box, zoom
- , e = element.doc().parent
- , zoom = element.doc().viewbox().zoom
-
- /* actual, native bounding box */
- box = element.node.getBoundingClientRect()
-
- /* get screen offset */
- this.x = box.left
- this.y = box.top
-
- /* subtract parent offset */
- this.x -= e.offsetLeft
- this.y -= e.offsetTop
-
- while (e = e.offsetParent) {
+ var e, zoom
+ , box = {}
+
+ /* initialize zero box */
+ this.x = 0
+ this.y = 0
+ this.width = 0
+ this.height = 0
+
+ if (element) {
+ e = element.doc().parent
+ zoom = element.doc().viewbox().zoom
+
+ /* actual, native bounding box */
+ box = element.node.getBoundingClientRect()
+
+ /* get screen offset */
+ this.x = box.left
+ this.y = box.top
+
+ /* subtract parent offset */
this.x -= e.offsetLeft
this.y -= e.offsetTop
- }
-
- /* calculate cumulative zoom from svg documents */
- e = element
- while (e = e.parent) {
- if (e.type == 'svg' && e.viewbox) {
- zoom *= e.viewbox().zoom
- this.x -= e.x() || 0
- this.y -= e.y() || 0
+
+ while (e = e.offsetParent) {
+ this.x -= e.offsetLeft
+ this.y -= e.offsetTop
+ }
+
+ /* calculate cumulative zoom from svg documents */
+ e = element
+ while (e = e.parent) {
+ if (e.type == 'svg' && e.viewbox) {
+ zoom *= e.viewbox().zoom
+ this.x -= e.x() || 0
+ this.y -= e.y() || 0
+ }
}
}