summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2016-08-04 08:58:29 +0200
committerwout <wout@impinc.co.uk>2016-08-04 08:58:29 +0200
commitb0a8b25f387d882e54dfddbbbc208a7c3ca39730 (patch)
tree6dcba89a33b2debf0f335033b3f27e241b71d6b0 /src
parent86c99607a393b43aeb50979e97ab69853293847f (diff)
downloadsvg.js-b0a8b25f387d882e54dfddbbbc208a7c3ca39730.tar.gz
svg.js-b0a8b25f387d882e54dfddbbbc208a7c3ca39730.zip
Speed improvements on parent element
Diffstat (limited to 'src')
-rw-r--r--src/element.js2
-rw-r--r--src/ellipse.js2
-rw-r--r--src/helpers.js14
-rw-r--r--src/line.js2
-rw-r--r--src/parent.js21
-rw-r--r--src/path.js2
-rw-r--r--src/poly.js2
-rw-r--r--src/utilities.js23
8 files changed, 42 insertions, 26 deletions
diff --git a/src/element.js b/src/element.js
index 2bf912a..3713aac 100644
--- a/src/element.js
+++ b/src/element.js
@@ -54,7 +54,7 @@ SVG.Element = SVG.invent({
}
// Set element size to given width and height
, size: function(width, height) {
- var p = proportionalSize(this.bbox(), width, height)
+ var p = proportionalSize(this, width, height)
return this
.width(new SVG.Number(p.width))
diff --git a/src/ellipse.js b/src/ellipse.js
index b1ae4fc..043084c 100644
--- a/src/ellipse.js
+++ b/src/ellipse.js
@@ -80,7 +80,7 @@ SVG.extend(SVG.Circle, SVG.Ellipse, {
}
// Custom size function
, size: function(width, height) {
- var p = proportionalSize(this.bbox(), width, height)
+ var p = proportionalSize(this, width, height)
return this
.rx(new SVG.Number(p.width).divide(2))
diff --git a/src/helpers.js b/src/helpers.js
index 4de813f..aa23bbe 100644
--- a/src/helpers.js
+++ b/src/helpers.js
@@ -36,11 +36,15 @@ function compToHex(comp) {
}
// Calculate proportional width and height values when necessary
-function proportionalSize(box, width, height) {
- if (height == null)
- height = box.height / box.width * width
- else if (width == null)
- width = box.width / box.height * height
+function proportionalSize(element, width, height) {
+ if (width == null || height == null) {
+ var box = element.bbox()
+
+ if (width == null)
+ width = box.width / box.height * height
+ else if (height == null)
+ height = box.height / box.width * width
+ }
return {
width: width
diff --git a/src/line.js b/src/line.js
index 8b8d1f3..cfe0533 100644
--- a/src/line.js
+++ b/src/line.js
@@ -29,7 +29,7 @@ SVG.Line = SVG.invent({
}
// Set element size to given width and height
, size: function(width, height) {
- var p = proportionalSize(this.bbox(), width, height)
+ var p = proportionalSize(this, width, height)
return this.attr(this.array().size(p.width, p.height).toLine())
}
diff --git a/src/parent.js b/src/parent.js
index 30f7662..ee99e4d 100644
--- a/src/parent.js
+++ b/src/parent.js
@@ -17,13 +17,10 @@ SVG.Parent = SVG.invent({
}
// Add given element at a position
, add: function(element, i) {
- if (!this.has(element)) {
- // define insertion index if none given
- i = i == null ? this.children().length : i
-
- // add element references
- this.node.insertBefore(element.node, SVG.utils.filterSVGElements(this.node.childNodes)[i] || null)
- }
+ if (i == null)
+ this.node.appendChild(element.node)
+ else if (element.node != this.node.childNodes[i])
+ this.node.insertBefore(element.node, this.node.childNodes[i])
return this
}
@@ -38,19 +35,19 @@ SVG.Parent = SVG.invent({
}
// Gets index of given element
, index: function(element) {
- return this.children().indexOf(element)
+ return [].slice.call(this.node.childNodes).indexOf(element.node)
}
// Get a element at the given index
, get: function(i) {
- return this.children()[i]
+ return SVG.adopt(this.node.childNodes[i])
}
- // Get first child, skipping the defs node
+ // Get first child
, first: function() {
- return this.children()[0]
+ return this.get(0)
}
// Get the last child
, last: function() {
- return this.children()[this.children().length - 1]
+ return this.get(this.node.childNodes.length - 1)
}
// Iterates over all children and invokes a given block
, each: function(block, deep) {
diff --git a/src/path.js b/src/path.js
index 8282ce7..f49e950 100644
--- a/src/path.js
+++ b/src/path.js
@@ -31,7 +31,7 @@ SVG.Path = SVG.invent({
}
// Set element size to given width and height
, size: function(width, height) {
- var p = proportionalSize(this.bbox(), width, height)
+ var p = proportionalSize(this, width, height)
return this.attr('d', this.array().size(p.width, p.height))
}
diff --git a/src/poly.js b/src/poly.js
index 0f3b1e6..26ca603 100644
--- a/src/poly.js
+++ b/src/poly.js
@@ -46,7 +46,7 @@ SVG.extend(SVG.Polyline, SVG.Polygon, {
}
// Set element size to given width and height
, size: function(width, height) {
- var p = proportionalSize(this.bbox(), width, height)
+ var p = proportionalSize(this, width, height)
return this.attr('points', this.array().size(p.width, p.height))
}
diff --git a/src/utilities.js b/src/utilities.js
index 70495cd..aeae87f 100644
--- a/src/utilities.js
+++ b/src/utilities.js
@@ -1,6 +1,6 @@
SVG.utils = {
- // Map function
- map: function(array, block) {
+ // Map function
+ map: function(array, block) {
var i
, il = array.length
, result = []
@@ -11,16 +11,31 @@ SVG.utils = {
return result
}
+ // Filter function
+, filter: function(array, block) {
+ var i
+ , il = array.length
+ , result = []
+
+ for (i = 0; i < il; i++)
+ if (block(array[i]))
+ result.push(array[i])
+
+ return result
+ }
+
// Degrees to radians
, radians: function(d) {
return d % 360 * Math.PI / 180
}
+
// Radians to degrees
, degrees: function(r) {
return r * 180 / Math.PI % 360
}
-, filterSVGElements: function(p) {
- return [].filter.call(p, function(el){ return el instanceof SVGElement })
+
+, filterSVGElements: function(nodes) {
+ return this.filter( nodes, function(el) { return el instanceof SVGElement })
}
} \ No newline at end of file