diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/arrange.js | 9 | ||||
-rw-r--r-- | src/clip.js | 9 | ||||
-rw-r--r-- | src/container.js | 30 | ||||
-rw-r--r-- | src/doc.js | 3 | ||||
-rw-r--r-- | src/element.js | 23 | ||||
-rw-r--r-- | src/ellipse.js | 4 | ||||
-rw-r--r-- | src/gradient.js | 37 | ||||
-rw-r--r-- | src/path.js | 2 | ||||
-rw-r--r-- | src/sugar.js | 32 | ||||
-rw-r--r-- | src/svg.js | 3 | ||||
-rw-r--r-- | src/text.js | 23 |
11 files changed, 115 insertions, 60 deletions
diff --git a/src/arrange.js b/src/arrange.js index 001da75..ac53e43 100644 --- a/src/arrange.js +++ b/src/arrange.js @@ -2,7 +2,7 @@ // add backward / forward functionality to elements SVG.extend(SVG.Element, { - // get all siblings, including me + // get all siblings, including myself siblings: function() { return this.parent.children(); }, @@ -10,9 +10,8 @@ SVG.extend(SVG.Element, { // send given element one step forwards forward: function() { var i = this.siblings().indexOf(this); - this.parent.remove(this).add(this, i + 1); - return this; + return this.parent.remove(this).put(this, i + 1); }, // send given element one step backwards @@ -29,9 +28,7 @@ SVG.extend(SVG.Element, { // send given element all the way to the front front: function() { - this.parent.remove(this).add(this); - - return this; + return this.parent.remove(this).put(this); }, // send given element all the way to the back diff --git a/src/clip.js b/src/clip.js index 9b106b4..6d820df 100644 --- a/src/clip.js +++ b/src/clip.js @@ -4,6 +4,8 @@ var clipID = 0; SVG.Clip = function Clip() { this.constructor.call(this, SVG.create('clipPath')); + + // set unique id this.id = 'svgjs_clip_' + (clipID++); this.attr('id', this.id); }; @@ -35,12 +37,9 @@ SVG.extend(SVG.Element, { // add def-specific functions SVG.extend(SVG.Defs, { - // define clippath + // create clippath clip: function() { - var e = new SVG.Clip(); - this.add(e); - - return e; + return this.put(new SVG.Clip()); } });
\ No newline at end of file diff --git a/src/container.js b/src/container.js index f436d4d..6482450 100644 --- a/src/container.js +++ b/src/container.js @@ -1,6 +1,8 @@ +// methods for container elements like SVG.Doc, SVG.Group, SVG.Defs, ... SVG.Container = { + // add given element at goven position add: function(e, i) { if (!this.has(e)) { i = i == null ? this.children().length : i; @@ -12,23 +14,28 @@ SVG.Container = { return this; }, + // basically does the same as add() but returns the added element rather than 'this' put: function(e, i) { this.add(e, i); return e; }, + // chacks if the given element is a child has: function(e) { return this.children().indexOf(e) >= 0; }, + // returns all child elements and initializes store array if non existant children: function() { return this._children || (this._children = []); }, + // remove a given child element remove: function(e) { return this.removeAt(this.children().indexOf(e)); }, + // remove child element at a given position removeAt: function(i) { if (0 <= i && i < this.children().length) { var e = this.children()[i]; @@ -40,58 +47,63 @@ SVG.Container = { return this; }, + // returns defs element and initializes store array if non existant defs: function() { - if (this._defs == null) { - this._defs = new SVG.Defs(); - this.add(this._defs, 0); - } - - return this._defs; + return this._defs || (this._defs = this.put(new SVG.Defs(), 0)); }, + // re-level defs to first positon in element stack level: function() { - var d = this.defs(); - - return this.remove(d).add(d, 0); + return this.remove(d).put(this.defs(), 0); }, + // create a group element group: function() { return this.put(new SVG.G()); }, + // create a rect element rect: function(w, h) { return this.put(new SVG.Rect().size(w, h)); }, + // create circle element, based on ellipse circle: function(d) { return this.ellipse(d); }, + // create and ellipse ellipse: function(w, h) { return this.put(new SVG.Ellipse().size(w, h)); }, + // create a polyline element polyline: function(p) { return this.put(new SVG.Polyline().plot(p)); }, + // create a polygon element polygon: function(p) { return this.put(new SVG.Polygon().plot(p)); }, + // create a path element path: function(d) { return this.put(new SVG.Path().plot(d)); }, + // create image element, load image and set its size image: function(s, w, h) { w = w != null ? w : 100; return this.put(new SVG.Image().load(s).size(w, h != null ? h : w)); }, + // create text element text: function(t) { return this.put(new SVG.Text().text(t)); }, + // create element in defs gradient: function(t, b) { return this.defs().gradient(t, b); }, @@ -16,10 +16,11 @@ SVG.Doc = function Doc(e) { attr('xlink', SVG.xlink, SVG.ns). defs(); + // add elements e.appendChild(w); w.appendChild(this.node); - // ensure + // ensure correct rendering for safari this.stage(); }; diff --git a/src/element.js b/src/element.js index e849867..e7701ce 100644 --- a/src/element.js +++ b/src/element.js @@ -1,7 +1,12 @@ SVG.Element = function Element(n) { + // keep reference to the element node this.node = n; + + // initialize attribute store this.attrs = {}; + + // initialize transformations store this.trans = { x: 0, y: 0, @@ -11,8 +16,6 @@ SVG.Element = function Element(n) { skewX: 0, skewY: 0 }; - - this._s = ('size family weight stretch variant style').split(' '); }; // Add element-specific functions @@ -48,9 +51,11 @@ SVG.extend(SVG.Element, { // set svg element attribute attr: function(a, v, n) { if (arguments.length < 2) { + // apply every attribute individually if an object is passed if (typeof a == 'object') for (v in a) this.attr(v, a[v]); - + + // act as a getter for style attributes else if (this._isStyle(a)) return a == 'text' ? this.content : @@ -58,19 +63,26 @@ SVG.extend(SVG.Element, { this[a] : this.style[a]; + // act as a getter if the first and only argument is not an object else return this.attrs[a]; } else { + // store value this.attrs[a] = v; + + // treat x differently on text elements if (a == 'x' && this._isText()) for (var i = this.lines.length - 1; i >= 0; i--) this.lines[i].attr(a, v); + + // set the actual attribute else n != null ? this.node.setAttributeNS(n, a, v) : this.node.setAttribute(a, v); + // if the passed argument belongs to the style as well, add it there if (this._isStyle(a)) { a == 'text' ? this.text(v) : @@ -150,14 +162,15 @@ SVG.extend(SVG.Element, { // private: find svg parent _parent: function(pt) { var e = this; - + + // find ancestor with given type while (e != null && !(e instanceof pt)) e = e.parent; return e; }, - // private: is this text style + // private: tester method for style detection _isStyle: function(a) { return typeof a == 'string' && this._isText() ? (/^font|text|leading/).test(a) : false; }, diff --git a/src/ellipse.js b/src/ellipse.js index 6280792..018cd4f 100644 --- a/src/ellipse.js +++ b/src/ellipse.js @@ -27,8 +27,8 @@ SVG.extend(SVG.Ellipse, { // position element by its center center: function(x, y) { return this.attr({ - cx: x || ((this.attrs.x || 0) + (this.attrs.rx || 0)), - cy: y || ((this.attrs.y || 0) + (this.attrs.ry || 0)) + cx: x || (this.attrs.x || 0) + (this.attrs.rx || 0), + cy: y || (this.attrs.y || 0) + (this.attrs.ry || 0) }); } diff --git a/src/gradient.js b/src/gradient.js index 0239b15..b9007ef 100644 --- a/src/gradient.js +++ b/src/gradient.js @@ -4,10 +4,13 @@ var gradID = 0; SVG.Gradient = function Gradient(t) { this.constructor.call(this, SVG.create(t + 'Gradient')); - this.id = 'svgjs_grad_' + (gradID++); - this.type = t; + // set unique id + this.id = 'svgjs_grad_' + (gradID++); this.attr('id', this.id); + + // store type + this.type = t; }; // inherit from SVG.Element @@ -40,21 +43,20 @@ SVG.extend(SVG.Gradient, { this; }, - // add color stops + // add a color stop at: function(o) { - var m = new SVG.Stop(o); - this.add(m); - - return m; + return this.put(new SVG.Stop(o)); }, // update gradient update: function(b) { + // remove all stops while (this.node.hasChildNodes()) this.node.removeChild(this.node.lastChild); + // invoke passed block b(this); - + return this; }, @@ -70,8 +72,9 @@ SVG.extend(SVG.Defs, { // define clippath gradient: function(t, b) { - var e = new SVG.Gradient(t); - this.add(e); + var e = this.put(new SVG.Gradient(t)); + + // invoke passed block b(e); return e; @@ -83,6 +86,7 @@ SVG.extend(SVG.Defs, { SVG.Stop = function Stop(o) { this.constructor.call(this, SVG.create('stop')); + // immediatelly build stop this.update(o); }; @@ -94,15 +98,18 @@ SVG.extend(SVG.Stop, { // add color stops update: function(o) { - var s = '', + var i, + s = '', a = ['opacity', 'color']; - - for (var i = a.length - 1; i >= 0; i--) + + // build style attribute + for (i = a.length - 1; i >= 0; i--) if (o[a[i]] != null) s += 'stop-' + a[i] + ':' + o[a[i]] + ';'; - + + // set attributes return this.attr({ - offset: (o.offset != null ? o.offset : this.attr('offset') || 0) + '%', + offset: (o.offset != null ? o.offset : this.attrs.offset || 0) + '%', style: s }); } diff --git a/src/path.js b/src/path.js index fef5809..5b71e50 100644 --- a/src/path.js +++ b/src/path.js @@ -14,7 +14,7 @@ SVG.extend(SVG.Path, { return this.attr('d', d || 'M0,0'); }, - // move path using translate + // move path using translate, path's don't take x and y move: function(x, y) { return this.transform({ x: x, y: y }); } diff --git a/src/sugar.js b/src/sugar.js index 10a8716..47ab35a 100644 --- a/src/sugar.js +++ b/src/sugar.js @@ -1,28 +1,40 @@ +// define list of available attributes for stroke and fill +var _strokeAttr = ['width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset'], + _fillAttr = ['opacity', 'rule']; + + // Add shape-specific functions SVG.extend(SVG.Shape, { // set fill color and opacity fill: function(f) { + var i; + + // set fill color if not null if (f.color != null) this.attr('fill', f.color); - - if (f.opacity != null) - this.attr('fill-opacity', f.opacity); - + + // set all attributes from _fillAttr list with prependes 'fill-' if not null + for (i = _fillAttr.length - 1; i >= 0; i--) + if (f[_fillAttr[i]] != null) + this.attr('fill-' + _fillAttr[i], f[_fillAttr[i]]); + return this; }, // set stroke color and opacity stroke: function(s) { + var i; + + // set stroke color if not null if (s.color) this.attr('stroke', s.color); - var a = ('width opacity linecap linejoin miterlimit dasharray dashoffset').split(' '); - - for (var i = a.length - 1; i >= 0; i--) - if (s[a[i]] != null) - this.attr('stroke-' + a[i], s[a[i]]); + // set all attributes from _strokeAttr list with prependes 'stroke-' if not null + for (i = _strokeAttr.length - 1; i >= 0; i--) + if (s[_strokeAttr[i]] != null) + this.attr('stroke-' + _strokeAttr[i], s[_strokeAttr[i]]); return this; } @@ -75,7 +87,7 @@ SVG.extend(SVG.Text, { a[k] = o[k] : k == 'anchor' ? a['text-anchor'] = o[k] : - this._s.indexOf(k) > -1 ? + _styleAttr.indexOf(k) > -1 ? a['font-'+ k] = o[k] : void 0; @@ -1,12 +1,15 @@ this.SVG = { + // define default namespaces ns: 'http://www.w3.org/2000/svg', xlink: 'http://www.w3.org/1999/xlink', + // method for element creation create: function(e) { return document.createElementNS(this.ns, e); }, + // method for extending objects extend: function(o, m) { for (var k in m) o.prototype[k] = m[k]; diff --git a/src/text.js b/src/text.js index 301373c..3fe3f0b 100644 --- a/src/text.js +++ b/src/text.js @@ -1,10 +1,14 @@ +// list font style attributes as they should be applied to style +var _styleAttr = ['size', 'family', 'weight', 'stretch', 'variant', 'style']; + + SVG.Text = function Text() { this.constructor.call(this, SVG.create('text')); + // define default style this.style = { 'font-size': 16, 'font-family': 'Helvetica', 'text-anchor': 'start' }; this.leading = 1.2; - this.lines = []; }; // inherit from SVG.Element @@ -14,6 +18,7 @@ SVG.Text.prototype = new SVG.Shape(); SVG.extend(SVG.Text, { text: function(t) { + // update the content this.content = t = t || 'text'; this.lines = []; @@ -23,31 +28,37 @@ SVG.extend(SVG.Text, { a = t.split("\n"), f = this.style['font-size']; + // remove existing child nodes while (this.node.hasChildNodes()) this.node.removeChild(this.node.lastChild); + // build new lines for (i = 0, l = a.length; i < l; i++) { + // create new tspan and set attributes n = new TSpan(). text(a[i]). attr({ dy: f * this.leading - (i == 0 ? f * 0.3 : 0), - x: (this.attr('x') || 0), + x: (this.attrs.x || 0), style: s }); + // add new tspan this.node.appendChild(n.node); this.lines.push(n); }; + // set style return this.attr('style', s); }, + // build style based on _styleAttr _style: function() { - var i, o = '', s = this._s; + var i, o = ''; - for (i = s.length - 1; i >= 0; i--) - if (this.style['font-' + s[i]] != null) - o += 'font-' + s[i] + ':' + this.style['font-' + s[i]] + ';'; + for (i = _styleAttr.length - 1; i >= 0; i--) + if (this.style['font-' + _styleAttr[i]] != null) + o += 'font-' + _styleAttr[i] + ':' + this.style['font-' + _styleAttr[i]] + ';'; o += 'text-anchor:' + this.style['text-anchor'] + ';'; |