From 8cab4433d5911bd6575dfae2ec5e8f1f2f64792c Mon Sep 17 00:00:00 2001 From: wout Date: Mon, 17 Dec 2012 21:26:17 +0100 Subject: [PATCH] Updated docs --- README.md | 64 +++++++++++++++++++++++++++++++++---------------- dist/svg.js | 33 +++++++++++++++++++++++++ dist/svg.min.js | 2 +- src/shape.js | 35 ++++++++++++++++++++++++++- 4 files changed, 111 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index d06097f..2a6d204 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ svg.js is licensed under the terms of the MIT License. ### Create a SVG document -Use the svg() function to create a SVG document within a given html element: +Use the 'svg()' function to create a SVG document within a given html element: ```javascript var draw = svg('paper').size(300, 300); @@ -28,7 +28,48 @@ This will generate the following output: ``` -### Path elements +### Manipulating elements + +#### Setting attributes +You can set an element's attributes directly with 'attr()': + +```javascript +// set a single attribute +rect.attr('x', 50); + +// set multiple attributes at once +rect.attr({ + fill: '#f06', + 'fill-opacity': 0.5, + stroke: '#000', + 'stroke-width': 10 +}); + +// set an attribute with a namespace +rect.setAttribute('x', 50, 'http://www.w3.org/2000/svg'); +``` + +#### Move +Move the element to a given x and y position by its upper left corner: +```javascript +rect.move(200, 350); +``` +Note that you can also use the following code to move elements around: +```javascript +rect.attr({ x: 20, y: 60 }) +``` +Although 'move()' is much more convenient because it will also use the upper left corner as the position reference, whereas with using 'attr()' the x an y reference differ between element types. For example, rect uses the upper left corner and circle uses the center. + + +#### Size +Set the size of an element by a given width and height: +```javascript +rect.size(200, 300); +``` +Same as with 'move()' the size of an element could be set by using 'attr()'. But because every type of element is handles its size differently the 'size()' function is much more convenient. + + +### Path element ```javascript var path = draw.path({ data: "M10,20L30,40" }).attr({ fill: '#9dffd3' }); @@ -67,25 +108,6 @@ clipRect = clipPath.rect({ x:10, y:10, width:80, height:80 }); rect.clipTo(clipPath); ``` -### Setting attributes -You can set an element's attributes directly with 'attr()': - -```javascript -// set a single attribute -rect.attr('x', 50); - -// set multiple attributes at once -rect.attr({ - fill: '#f06', - 'fill-opacity': 0.5, - stroke: '#000', - 'stroke-width': 10 -}); - -// set an attribute with a namespace -rect.setAttribute('x', 50, 'http://www.w3.org/2000/svg'); -``` - ## Compatibility ### Desktop diff --git a/dist/svg.js b/dist/svg.js index 62d32af..f0b8cc8 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -311,6 +311,39 @@ // inherit from SVG.Element SVG.Shape.prototype = new SVG.Element(); + + // Add shape-specific functions + SVG.Utils.merge(SVG.Shape, { + + // set fill color and opacity + fill: function(f) { + if (f.color != null) + this.attr('fill', f.color); + + if (f.opacity != null) + this.attr('fill-opacity', f.opacity); + + return this; + }, + + // set stroke color and opacity + stroke: function(s) { + if (s.color) + this.attr('stroke', s.color); + + if (s.width != null) + this.attr('stroke-width', s.width); + + if (s.opacity != null) + this.attr('stroke-opacity', s.opacity); + + if (this.attrs['fill-opacity'] == null) + this.fill({ opacity: 0 }); + + return this; + } + + }); SVG.Rect = function Rect() { this.constructor.call(this, SVG.create('rect')); diff --git a/dist/svg.min.js b/dist/svg.min.js index e5c2696..c8f59e5 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ /* svg.js 0.1 - svg utils container element doc defs shape rect circle ellipse path image g clip - svgjs.com/license */ -(function(){this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",create:function(e){return document.createElementNS(this.ns,e)}},this.svg=function(e){return new SVG.Doc(e)},SVG.Utils={merge:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e){return this.addAt(e)},addAt:function(e,t){return this.contains(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t+1]),e.parent=this),this},contains:function(e){return Array.prototype.indexOf.call(this.children(),e)>=0},children:function(){return this._children||[]},sendBack:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t-1)},bringForward:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t+1)},bringToFront:function(e){return this.contains(e)&&this.remove(e).add(e),this},sendToBottom:function(e){return this.contains(e)&&this.remove(e).addAt(e,0),this},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e=0},children:function(){return this._children||[]},sendBack:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t-1)},bringForward:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t+1)},bringToFront:function(e){return this.contains(e)&&this.remove(e).add(e),this},sendToBottom:function(e){return this.contains(e)&&this.remove(e).addAt(e,0),this},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e