diff options
author | wout <wout@impinc.co.uk> | 2014-06-21 18:25:29 +0200 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2014-06-21 18:25:29 +0200 |
commit | 19b6fd4338246d1fd9ca4806e57c7aebcec63826 (patch) | |
tree | 3dd1d207de9f586f7e4d4cd36917e347ab022a9e /src/doc.js | |
parent | bb0e6be2da0cea98c5303871c0be055f5f70d7a9 (diff) | |
download | svg.js-19b6fd4338246d1fd9ca4806e57c7aebcec63826.tar.gz svg.js-19b6fd4338246d1fd9ca4806e57c7aebcec63826.zip |
Removed all structural references
Diffstat (limited to 'src/doc.js')
-rwxr-xr-x | src/doc.js | 81 |
1 files changed, 25 insertions, 56 deletions
@@ -1,32 +1,24 @@ SVG.Doc = SVG.invent({ // Initialize node create: function(element) { - /* ensure the presence of a html element */ - this.parent = typeof element == 'string' ? + /* 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. */ - this.constructor - .call(this, this.parent.nodeName == 'svg' ? this.parent : SVG.create('svg')) + 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 */ this .attr({ xmlns: SVG.ns, version: '1.1', width: '100%', height: '100%' }) .attr('xmlns:xlink', SVG.xlink, SVG.xmlns) - - /* create the <defs> node */ - this._defs = new SVG.Defs - this._defs.parent = this - this.node.appendChild(this._defs.node) - - /* turn off sub pixel offset by default */ - this.doSpof = false - - /* ensure correct rendering */ - if (this.parent != this.node) - this.stage() } // Inherit from @@ -34,49 +26,26 @@ SVG.Doc = SVG.invent({ // Add class methods , extend: { - /* enable drawing */ - stage: function() { - var element = this - - /* insert element */ - this.parent.appendChild(this.node) - - /* fix sub-pixel offset */ - element.spof() - - /* make sure sub-pixel offset is fixed every time the window is resized */ - SVG.on(window, 'resize', function() { - element.spof() - }) - - return this - } - // Creates and returns defs element - , defs: function() { - return this._defs - } - - // Fix for possible sub-pixel offset. See: - // https://bugzilla.mozilla.org/show_bug.cgi?id=608812 - , spof: function() { - if (this.doSpof) { - var pos = this.node.getScreenCTM() - - if (pos) - this - .style('left', (-pos.e % 1) + 'px') - .style('top', (-pos.f % 1) + 'px') + defs: function() { + if (!this._defs) { + var defs + + // Find or create a defs element in this instance + if (defs = this.node.getElementsByTagName('defs')[0]) + this._defs = SVG.adopt(defs) + else + this._defs = new SVG.Defs + + // Make sure the defs node is at the end of the stack + this.node.appendChild(this._defs.node) } - - return this - } - - // Enable sub-pixel offset - , fixSubPixelOffset: function() { - this.doSpof = true - return this + return this._defs + } + // custom parent method + , parent: function() { + return this.node.parentNode.nodeName == '#document' ? null : this.node.parentNode } } |