diff options
author | wout <wout@impinc.co.uk> | 2013-03-02 13:23:18 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2013-03-02 13:23:18 +0100 |
commit | 028bf91551d01567b4cb3d971ba51fa57c4a81ef (patch) | |
tree | aff2f760942f61f2541317a3cf91edcfc15ae077 /dist/svg.js | |
parent | e9fa07a7b33b8f19c0690b0fc3df2f57a404d224 (diff) | |
download | svg.js-028bf91551d01567b4cb3d971ba51fa57c4a81ef.tar.gz svg.js-028bf91551d01567b4cb3d971ba51fa57c4a81ef.zip |
Code refactoring on element
Diffstat (limited to 'dist/svg.js')
-rw-r--r-- | dist/svg.js | 97 |
1 files changed, 49 insertions, 48 deletions
diff --git a/dist/svg.js b/dist/svg.js index 522e03c..2f75bdb 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js v0.7 - svg viewbox bbox element container fx event group arrange defs mask pattern gradient doc shape wrap rect ellipse line poly path image text nested sugar - svgjs.com/license */ +/* svg.js v0.7-1-ge9fa07a - svg viewbox bbox element container fx event group arrange defs mask pattern gradient doc shape wrap rect ellipse line poly path image text nested sugar - svgjs.com/license */ (function() { this.svg = function(element) { @@ -107,16 +107,11 @@ } SVG.Element = function(node) { - /* keep reference to the element node */ - if (this.node = node) - this.type = node.nodeName - /* initialize attribute store with defaults */ this.attrs = { 'fill-opacity': 1 , 'stroke-opacity': 1 , 'stroke-width': 0 - , 'id': (node ? node.getAttribute('id') : null) , fill: '#000' , stroke: '#000' , opacity: 1 @@ -141,6 +136,12 @@ , skewX: 0 , skewY: 0 } + + /* keep reference to the element node */ + if (this.node = node) { + this.type = node.nodeName + this.attrs.id = node.getAttribute('id') + } } // @@ -148,25 +149,25 @@ // Move element to given x and y values move: function(x, y) { return this.attr({ - x: x, - y: y + x: x + , y: y }) - }, + } // Move element by its center - center: function(x, y) { + , center: function(x, y) { var box = this.bbox() return this.move(x - box.width / 2, y - box.height / 2) - }, + } // Set element size to given width and height - size: function(width, height) { + , size: function(width, height) { return this.attr({ - width: width, - height: height + width: width + , height: height }) - }, + } // Clone element - clone: function() { + , clone: function() { var clone /* if this is a wrapped shape */ @@ -207,23 +208,23 @@ /* apply attributes and translations */ return clone.transform({}) - }, + } // Remove element - remove: function() { + , remove: function() { if (this.parent) this.parent.remove(this) return this - }, + } // Get parent document - doc: function() { + , doc: function() { return this._parent(SVG.Doc) - }, + } // Get parent nested document - nested: function() { + , nested: function() { return this._parent(SVG.Nested) - }, + } // Set svg element attribute - attr: function(a, v, n) { + , attr: function(a, v, n) { if (arguments.length < 2) { /* apply every attribute individually if an object is passed */ if (typeof a == 'object') @@ -279,9 +280,9 @@ } return this - }, + } // Manage transformations - transform: function(o) { + , transform: function(o) { /* act as a getter if the first argument is a string */ if (typeof o === 'string') return this.trans[o] @@ -319,9 +320,9 @@ /* add only te required transformations */ return this.attr('transform', transform.join(' ')) - }, + } // Store data values on svg nodes - data: function(a, v, r) { + , data: function(a, v, r) { if (arguments.length < 2) { try { return JSON.parse(this.attr('data-' + a)) @@ -334,51 +335,51 @@ } return this - }, + } // Get bounding box - bbox: function() { + , bbox: function() { return new SVG.BBox(this) - }, + } // Checks whether the given point inside the bounding box of the element - inside: function(x, y) { + , inside: function(x, y) { var box = this.bbox() - return x > box.x && - y > box.y && - x < box.x + box.width && - y < box.y + box.height - }, + return x > box.x + && y > box.y + && x < box.x + box.width + && y < box.y + box.height + } // Show element - show: function() { + , show: function() { this.node.style.display = '' return this - }, + } // Hide element - hide: function() { + , hide: function() { this.node.style.display = 'none' return this - }, + } // Is element visible? - visible: function() { + , visible: function() { return this.node.style.display != 'none' - }, + } // Private: find svg parent by instance - _parent: function(parent) { + , _parent: function(parent) { var element = this while (element != null && !(element instanceof parent)) element = element.parent return element - }, + } // Private: tester method for style detection - _isStyle: function(attr) { + , _isStyle: function(attr) { return typeof attr == 'string' && this._isText() ? (/^font|text|leading/).test(attr) : false - }, + } // Private: element type tester - _isText: function() { + , _isText: function() { return this instanceof SVG.Text } |