aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/boxes.js22
-rw-r--r--src/container.js17
-rw-r--r--src/element.js7
-rw-r--r--src/line.js2
-rw-r--r--src/patharray.js2
-rw-r--r--src/regex.js20
-rw-r--r--src/viewbox.js29
7 files changed, 55 insertions, 44 deletions
diff --git a/src/boxes.js b/src/boxes.js
index 3066be2..1e9d0eb 100644
--- a/src/boxes.js
+++ b/src/boxes.js
@@ -7,11 +7,15 @@ SVG.BBox = SVG.invent({
// yes this is ugly, but Firefox can be a bitch when it comes to elements that are not yet rendered
try {
+
+ // the element is NOT in the dom, throw error
+ if(!document.documentElement.contains(element.node)) throw new Exception('Element not in the dom')
+
// find native bbox
box = element.node.getBBox()
} catch(e) {
if(element instanceof SVG.Shape){
- var clone = element.clone().addTo(SVG.parser.draw)
+ var clone = element.clone(SVG.parser.draw)
box = clone.bbox()
clone.remove()
}else{
@@ -23,7 +27,7 @@ SVG.BBox = SVG.invent({
}
}
}
-
+
// plain x and y
this.x = box.x
this.y = box.y
@@ -57,7 +61,7 @@ SVG.TBox = SVG.invent({
if (element) {
var t = element.ctm().extract()
, box = element.bbox()
-
+
// width and height including transformations
this.width = box.width * t.scaleX
this.height = box.height * t.scaleY
@@ -92,20 +96,20 @@ SVG.RBox = SVG.invent({
var e = element.doc().parent()
, box = element.node.getBoundingClientRect()
, zoom = 1
-
+
// 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) {
this.x -= e.offsetLeft
this.y -= e.offsetTop
}
-
+
// calculate cumulative zoom from svg documents
e = element
while (e.parent && (e = e.parent())) {
@@ -120,7 +124,7 @@ SVG.RBox = SVG.invent({
this.width = box.width /= zoom
this.height = box.height /= zoom
}
-
+
// add center, right and bottom
fullBox(this)
@@ -155,7 +159,7 @@ SVG.RBox = SVG.invent({
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
-
+
return fullBox(b)
}
diff --git a/src/container.js b/src/container.js
index 33427c6..68adf93 100644
--- a/src/container.js
+++ b/src/container.js
@@ -7,21 +7,4 @@ SVG.Container = SVG.invent({
// Inherit from
, inherit: SVG.Parent
- // Add class methods
-, extend: {
- // Get the viewBox and calculate the zoom value
- viewbox: function(v) {
- if (arguments.length == 0)
- // act as a getter if there are no arguments
- return new SVG.ViewBox(this)
-
- // otherwise act as a setter
- v = arguments.length == 1 ?
- [v.x, v.y, v.width, v.height] :
- [].slice.call(arguments)
-
- return this.attr('viewBox', v)
- }
- }
-
}) \ No newline at end of file
diff --git a/src/element.js b/src/element.js
index 552fd5b..2bf912a 100644
--- a/src/element.js
+++ b/src/element.js
@@ -61,12 +61,13 @@ SVG.Element = SVG.invent({
.height(new SVG.Number(p.height))
}
// Clone element
- , clone: function() {
+ , clone: function(parent) {
// clone element and assign new id
var clone = assignNewId(this.node.cloneNode(true))
- // insert the clone after myself
- this.after(clone)
+ // insert the clone in the given parent or after myself
+ if(parent) parent.add(clone)
+ else this.after(clone)
return clone
}
diff --git a/src/line.js b/src/line.js
index 0fcf63f..8b8d1f3 100644
--- a/src/line.js
+++ b/src/line.js
@@ -16,7 +16,7 @@ SVG.Line = SVG.invent({
}
// Overwrite native plot() method
, plot: function(x1, y1, x2, y2) {
- if (arguments.length == 4)
+ if (typeof y1 !== 'undefined')
x1 = { x1: x1, y1: y1, x2: x2, y2: y2 }
else
x1 = new SVG.PointArray(x1).toLine()
diff --git a/src/patharray.js b/src/patharray.js
index 03a9b7f..90d0558 100644
--- a/src/patharray.js
+++ b/src/patharray.js
@@ -166,7 +166,7 @@ SVG.extend(SVG.PathArray, {
// upper case
if(s == seg[0]){
- if(s == 'M' || s == 'L' || s == 'C' || s == 'Q'){
+ if(s == 'M' || s == 'L' || s == 'C' || s == 'Q' || s == 'S' || s == 'T'){
x = seg[paramCnt[seg[0]]-1]
y = seg[paramCnt[seg[0]]]
}else if(s == 'V'){
diff --git a/src/regex.js b/src/regex.js
index 265a11a..ce89b9b 100644
--- a/src/regex.js
+++ b/src/regex.js
@@ -2,37 +2,37 @@
SVG.regex = {
// Parse unit value
numberAndUnit: /^([+-]?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?)([a-z%]*)$/i
-
+
// Parse hex value
, hex: /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i
-
+
// Parse rgb value
, rgb: /rgb\((\d+),(\d+),(\d+)\)/
-
+
// Parse reference id
, reference: /#([a-z0-9\-_]+)/i
-
+
// Parse matrix wrapper
, matrix: /matrix\(|\)/g
// Elements of a matrix
, matrixElements: /,*\s+|,/
-
+
// Whitespace
, whitespace: /\s/g
// Test hex value
, isHex: /^#[a-f0-9]{3,6}$/i
-
+
// Test rgb value
, isRgb: /^rgb\(/
-
+
// Test css declaration
, isCss: /[^:]+:[^;]+;?/
-
+
// Test for blank string
, isBlank: /^(\s+)?$/
-
+
// Test for numeric string
, isNumber: /^[+-]?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i
@@ -52,7 +52,7 @@ SVG.regex = {
// Replaces all hyphens
, hyphen: /\-/g
-
+
// Replaces and tests for all path letters
, pathLetters: /[MLHVCSQTAZ]/gi
diff --git a/src/viewbox.js b/src/viewbox.js
index bb106b0..7bf307d 100644
--- a/src/viewbox.js
+++ b/src/viewbox.js
@@ -2,12 +2,12 @@
SVG.ViewBox = SVG.invent({
create: function(source) {
- var i, base = [1, 0, 0, 1]
+ var i, base = [0, 0, 0, 0]
var x, y, width, height, box, view, we, he
, wm = 1 // width multiplier
, hm = 1 // height multiplier
- , reg = /-?[\d\.]+/g
+ , reg = /[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?/gi
if(source instanceof SVG.Element){
@@ -60,6 +60,7 @@ SVG.ViewBox = SVG.invent({
}
}else{
+
// ensure source as object
source = typeof source === 'string' ?
source.match(reg).map(function(el){ return parseFloat(el) }) :
@@ -92,7 +93,7 @@ SVG.ViewBox = SVG.invent({
[].slice.call(arguments)
this.destination = new SVG.ViewBox(v)
-
+
return this
}
@@ -112,4 +113,26 @@ SVG.ViewBox = SVG.invent({
}
+ // Define parent
+, parent: SVG.Container
+
+ // Add parent method
+, construct: {
+
+ // get/set viewbox
+ viewbox: function(v) {
+ if (arguments.length == 0)
+ // act as a getter if there are no arguments
+ return new SVG.ViewBox(this)
+
+ // otherwise act as a setter
+ v = arguments.length == 1 ?
+ [v.x, v.y, v.width, v.height] :
+ [].slice.call(arguments)
+
+ return this.attr('viewBox', v)
+ }
+
+ }
+
}) \ No newline at end of file