summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2014-06-25 15:05:03 +0200
committerwout <wout@impinc.co.uk>2014-06-25 15:05:03 +0200
commite567c774c17e6649041d4972defd1232ab602012 (patch)
tree05140fe4715a652c99f15672e1d56c0fc8426824 /src
parentec21f496bddc588e2113fd5418d713ce8ae78f39 (diff)
downloadsvg.js-e567c774c17e6649041d4972defd1232ab602012.tar.gz
svg.js-e567c774c17e6649041d4972defd1232ab602012.zip
Added SVG.Circle, removed structural internal references
Diffstat (limited to 'src')
-rw-r--r--src/adopter.js12
-rwxr-xr-xsrc/array.js3
-rwxr-xr-xsrc/doc.js43
-rwxr-xr-xsrc/element.js29
-rwxr-xr-xsrc/ellipse.js82
-rwxr-xr-xsrc/path.js14
-rwxr-xr-xsrc/patharray.js2
-rwxr-xr-xsrc/pointarray.js4
-rwxr-xr-xsrc/poly.js10
-rwxr-xr-xsrc/rect.js2
-rwxr-xr-xsrc/sugar.js8
-rwxr-xr-xsrc/svg.js2
12 files changed, 128 insertions, 83 deletions
diff --git a/src/adopter.js b/src/adopter.js
index c762a2b..57e7691 100644
--- a/src/adopter.js
+++ b/src/adopter.js
@@ -17,7 +17,13 @@ SVG.adopt = function(node) {
element = new SVG[capitalize(node.nodeName)]
// Ensure references
- element.type = node.nodeName
- element.node = node
- return node.instance = element
+ element.type = node.nodeName
+ element.node = node
+ node.instance = element
+
+ // SVG.Class specific preparations
+ if (element instanceof SVG.Doc)
+ element.namespace().defs()
+
+ return element
} \ No newline at end of file
diff --git a/src/array.js b/src/array.js
index bc79ceb..6c99698 100755
--- a/src/array.js
+++ b/src/array.js
@@ -77,5 +77,4 @@ SVG.extend(SVG.Array, {
return this
}
-})
-
+}) \ No newline at end of file
diff --git a/src/doc.js b/src/doc.js
index 1a025f3..1e19ec7 100755
--- a/src/doc.js
+++ b/src/doc.js
@@ -1,25 +1,24 @@
SVG.Doc = SVG.invent({
// Initialize node
create: function(element) {
- /* ensure the presence of a dom element */
- element = typeof element == 'string' ?
- document.getElementById(element) :
- element
-
- /* If the target is an svg element, use that element as the main wrapper.
- This allows svg.js to work with svg documents as well. */
- if (element.nodeName == 'svg') {
- this.constructor.call(this, element)
- } else {
- this.constructor.call(this, SVG.create('svg'))
- element.appendChild(this.node)
+ if (element) {
+ /* ensure the presence of a dom element */
+ element = typeof element == 'string' ?
+ document.getElementById(element) :
+ element
+
+ /* If the target is an svg element, use that element as the main wrapper.
+ This allows svg.js to work with svg documents as well. */
+ if (element.nodeName == 'svg') {
+ this.constructor.call(this, element)
+ } else {
+ this.constructor.call(this, SVG.create('svg'))
+ element.appendChild(this.node)
+ }
+
+ /* set svg element attributes and ensure defs node */
+ this.namespace().size('100%', '100%').defs()
}
-
- /* set svg element attributes and ensure defs node */
- this
- .attr({ xmlns: SVG.ns, version: '1.1', width: '100%', height: '100%' })
- .attr('xmlns:xlink', SVG.xlink, SVG.xmlns)
- .defs()
}
// Inherit from
@@ -27,8 +26,14 @@ SVG.Doc = SVG.invent({
// Add class methods
, extend: {
+ // Add namespaces
+ namespace: function() {
+ return this
+ .attr({ xmlns: SVG.ns, version: '1.1' })
+ .attr('xmlns:xlink', SVG.xlink, SVG.xmlns)
+ }
// Creates and returns defs element
- defs: function() {
+ , defs: function() {
if (!this._defs) {
var defs
diff --git a/src/element.js b/src/element.js
index c8eb5de..753ac33 100755
--- a/src/element.js
+++ b/src/element.js
@@ -125,8 +125,9 @@ SVG.Element = SVG.invent({
}
// Set svg element attribute
, attr: function(a, v, n) {
+ // Act as full getter
if (a == null) {
- /* get an object of attributes */
+ // Get an object of attributes
a = {}
v = this.node.attributes
for (n = v.length - 1; n >= 0; n--)
@@ -135,15 +136,15 @@ SVG.Element = SVG.invent({
return a
} else if (typeof a == 'object') {
- /* apply every attribute individually if an object is passed */
+ // Apply every attribute individually if an object is passed
for (v in a) this.attr(v, a[v])
} else if (v === null) {
- /* remove value */
+ // Remove value
this.node.removeAttribute(a)
} else if (v == null) {
- /* act as a getter if the first and only argument is not an object */
+ // Act as a getter if the first and only argument is not an object
v = this.node.getAttribute(a)
return v == null ?
SVG.defaults.attrs[a] :
@@ -151,17 +152,17 @@ SVG.Element = SVG.invent({
parseFloat(v) : v
} else if (a == 'style') {
- /* redirect to the style method */
+ // Redirect to the style method
return this.style(v)
} else {
- /* BUG FIX: some browsers will render a stroke if a color is given even though stroke width is 0 */
+ // BUG FIX: some browsers will render a stroke if a color is given even though stroke width is 0
if (a == 'stroke-width')
this.attr('stroke', parseFloat(v) > 0 ? this._stroke : null)
else if (a == 'stroke')
this._stroke = v
- /* convert image fill and stroke to patterns */
+ // Convert image fill and stroke to patterns
if (a == 'fill' || a == 'stroke') {
if (SVG.regex.isImage.test(v))
v = this.doc().defs().image(v, 0, 0)
@@ -172,31 +173,31 @@ SVG.Element = SVG.invent({
})
}
- /* ensure correct numeric values (also accepts NaN and Infinity) */
+ // Ensure correct numeric values (also accepts NaN and Infinity)
if (typeof v === 'number')
v = new SVG.Number(v)
- /* ensure full hex color */
+ // Ensure full hex color
else if (SVG.Color.isColor(v))
v = new SVG.Color(v)
- /* parse array values */
+ // Parse array values
else if (Array.isArray(v))
v = new SVG.Array(v)
- /* if the passed attribute is leading... */
+ // If the passed attribute is leading...
if (a == 'leading') {
- /* ... call the leading method instead */
+ // ... call the leading method instead
if (this.leading)
this.leading(v)
} else {
- /* set given attribute on node */
+ // Set given attribute on node
typeof n === 'string' ?
this.node.setAttributeNS(n, a, v.toString()) :
this.node.setAttribute(a, v.toString())
}
- /* rebuild if required */
+ // Rebuild if required
if (this.rebuild && (a == 'font-size' || a == 'x'))
this.rebuild(a, v)
}
diff --git a/src/ellipse.js b/src/ellipse.js
index 0d23e6e..aab8087 100755
--- a/src/ellipse.js
+++ b/src/ellipse.js
@@ -1,3 +1,30 @@
+SVG.Circle = SVG.invent({
+ // Initialize node
+ create: 'circle'
+
+ // Inherit from
+, inherit: SVG.Shape
+
+ // Add parent method
+, construct: {
+ // Create circle element, based on ellipse
+ circle: function(size) {
+ return this.put(new SVG.Circle).rx(new SVG.Number(size).divide(2)).move(0, 0)
+ }
+ }
+})
+
+SVG.extend(SVG.Circle, SVG.FX, {
+ // Radius x value
+ rx: function(rx) {
+ return this.attr('r', rx)
+ }
+ // Alias radius x value
+, ry: function(ry) {
+ return this.rx(ry)
+ }
+})
+
SVG.Ellipse = SVG.invent({
// Initialize node
create: 'ellipse'
@@ -5,15 +32,35 @@ SVG.Ellipse = SVG.invent({
// Inherit from
, inherit: SVG.Shape
- // Add class methods
-, extend: {
+ // Add parent method
+, construct: {
+ // Create an ellipse
+ ellipse: function(width, height) {
+ return this.put(new SVG.Ellipse).size(width, height).move(0, 0)
+ }
+ }
+})
+
+SVG.extend(SVG.Ellipse, SVG.Rect, SVG.FX, {
+ // Radius x value
+ rx: function(rx) {
+ return this.attr('rx', rx)
+ }
+ // Radius y value
+, ry: function(ry) {
+ return this.attr('ry', ry)
+ }
+})
+
+// Add common method
+SVG.extend(SVG.Circle, SVG.Ellipse, {
// Move over x-axis
x: function(x) {
- return x == null ? this.cx() - this.attr('rx') : this.cx(x + this.attr('rx'))
+ return x == null ? this.cx() - this.rx() : this.cx(x + this.rx())
}
// Move over y-axis
, y: function(y) {
- return y == null ? this.cy() - this.attr('ry') : this.cy(y + this.attr('ry'))
+ return y == null ? this.cy() - this.ry() : this.cy(y + this.ry())
}
// Move by center over x-axis
, cx: function(x) {
@@ -25,35 +72,18 @@ SVG.Ellipse = SVG.invent({
}
// Set width of element
, width: function(width) {
- return width == null ? this.attr('rx') * 2 : this.attr('rx', new SVG.Number(width).divide(2))
+ return width == null ? this.rx() * 2 : this.rx(new SVG.Number(width).divide(2))
}
// Set height of element
, height: function(height) {
- return height == null ? this.attr('ry') * 2 : this.attr('ry', new SVG.Number(height).divide(2))
+ return height == null ? this.ry() * 2 : this.ry(new SVG.Number(height).divide(2))
}
// Custom size function
, size: function(width, height) {
var p = proportionalSize(this.bbox(), width, height)
- return this.attr({
- rx: new SVG.Number(p.width).divide(2)
- , ry: new SVG.Number(p.height).divide(2)
- })
- }
-
- }
-
- // Add parent method
-, construct: {
- // Create circle element, based on ellipse
- circle: function(size) {
- return this.ellipse(size, size)
+ return this
+ .rx(new SVG.Number(p.width).divide(2))
+ .ry(new SVG.Number(p.height).divide(2))
}
- // Create an ellipse
- , ellipse: function(width, height) {
- return this.put(new SVG.Ellipse).size(width, height).move(0, 0)
- }
-
- }
-
}) \ No newline at end of file
diff --git a/src/path.js b/src/path.js
index 773b3d8..8282ce7 100755
--- a/src/path.js
+++ b/src/path.js
@@ -7,13 +7,19 @@ SVG.Path = SVG.invent({
// Add class methods
, extend: {
+ // Define morphable array
+ morphArray: SVG.PathArray
+ // Get array
+ , array: function() {
+ return this._array || (this._array = new SVG.PathArray(this.attr('d')))
+ }
// Plot new poly points
- plot: function(p) {
- return this.attr('d', (this.array = new SVG.PathArray(p, [['M', 0, 0]])))
+ , plot: function(p) {
+ return this.attr('d', (this._array = new SVG.PathArray(p)))
}
// Move by left top corner
, move: function(x, y) {
- return this.attr('d', this.array.move(x, y))
+ return this.attr('d', this.array().move(x, y))
}
// Move by left top corner over x-axis
, x: function(x) {
@@ -27,7 +33,7 @@ SVG.Path = SVG.invent({
, size: function(width, height) {
var p = proportionalSize(this.bbox(), width, height)
- return this.attr('d', this.array.size(p.width, p.height))
+ return this.attr('d', this.array().size(p.width, p.height))
}
// Set width of element
, width: function(width) {
diff --git a/src/patharray.js b/src/patharray.js
index 1a2fdd5..746cdc4 100755
--- a/src/patharray.js
+++ b/src/patharray.js
@@ -1,6 +1,6 @@
// Path points array
SVG.PathArray = function(array, fallback) {
- this.constructor.call(this, array, fallback)
+ this.constructor.call(this, array, fallback || [['M', 0, 0]])
}
// Inherit from SVG.Array
diff --git a/src/pointarray.js b/src/pointarray.js
index e246adf..67b6ad4 100755
--- a/src/pointarray.js
+++ b/src/pointarray.js
@@ -1,6 +1,6 @@
// Poly points array
-SVG.PointArray = function() {
- this.constructor.apply(this, arguments)
+SVG.PointArray = function(array, fallback) {
+ this.constructor.call(this, array, fallback || [[0,0]])
}
// Inherit from SVG.Array
diff --git a/src/poly.js b/src/poly.js
index 1586c77..2bf0b57 100755
--- a/src/poly.js
+++ b/src/poly.js
@@ -34,13 +34,17 @@ SVG.Polygon = SVG.invent({
SVG.extend(SVG.Polyline, SVG.Polygon, {
// Define morphable array
morphArray: SVG.PointArray
+ // Get array
+, array: function() {
+ return this._array || (this._array = new SVG.PointArray(this.attr('points')))
+ }
// Plot new path
, plot: function(p) {
- return this.attr('points', (this.array = new SVG.PointArray(p, [[0,0]])))
+ return this.attr('points', (this._array = new SVG.PointArray(p)))
}
// Move by left top corner
, move: function(x, y) {
- return this.attr('points', this.array.move(x, y))
+ return this.attr('points', this.array().move(x, y))
}
// Move by left top corner over x-axis
, x: function(x) {
@@ -66,7 +70,7 @@ SVG.extend(SVG.Polyline, SVG.Polygon, {
, size: function(width, height) {
var p = proportionalSize(this.bbox(), width, height)
- return this.attr('points', this.array.size(p.width, p.height))
+ return this.attr('points', this.array().size(p.width, p.height))
}
}) \ No newline at end of file
diff --git a/src/rect.js b/src/rect.js
index 313c96d..825dd32 100755
--- a/src/rect.js
+++ b/src/rect.js
@@ -11,7 +11,5 @@ SVG.Rect = SVG.invent({
rect: function(width, height) {
return this.put(new SVG.Rect().size(width, height))
}
-
}
-
}) \ No newline at end of file
diff --git a/src/sugar.js b/src/sugar.js
index 4a8fff8..493c6c7 100755
--- a/src/sugar.js
+++ b/src/sugar.js
@@ -66,15 +66,13 @@ SVG.extend(SVG.Element, SVG.FX, {
, opacity: function(value) {
return this.attr('opacity', value)
}
-
})
-SVG.extend(SVG.Rect, SVG.Ellipse, SVG.FX, {
+SVG.extend(SVG.Rect, SVG.Ellipse, SVG.Circle, SVG.FX, {
// Add x and y radius
radius: function(x, y) {
- return this.attr({ rx: x, ry: y || x })
+ return this.rx(x).ry(y == null ? x : y)
}
-
})
SVG.extend(SVG.Path, {
@@ -86,7 +84,6 @@ SVG.extend(SVG.Path, {
, pointAt: function(length) {
return this.node.getPointAtLength(length)
}
-
})
SVG.extend(SVG.Parent, SVG.Text, SVG.FX, {
@@ -103,6 +100,5 @@ SVG.extend(SVG.Parent, SVG.Text, SVG.FX, {
return this
}
-
})
diff --git a/src/svg.js b/src/svg.js
index 3c25288..a18e1ca 100755
--- a/src/svg.js
+++ b/src/svg.js
@@ -21,7 +21,7 @@ SVG.did = 1000
// Get next named element id
SVG.eid = function(name) {
- return 'Svgjs' + name.charAt(0).toUpperCase() + name.slice(1) + (SVG.did++)
+ return 'Svgjs' + capitalize(name) + (SVG.did++)
}
// Method for element creation