From 9f622f7b160fb977a4354eb0f775d4b53a2f7443 Mon Sep 17 00:00:00 2001 From: wout Date: Mon, 31 Dec 2012 12:21:45 +0100 Subject: [PATCH] Added mask.js --- README.md | 35 ++++++++++++++++++++++--- Rakefile | 2 +- dist/svg.js | 70 ++++++++++++++++++++++++++++++++++++++++++------- dist/svg.min.js | 4 +-- src/clip.js | 5 +--- src/event.js | 13 ++++++++- src/gradient.js | 5 +--- src/mask.js | 42 +++++++++++++++++++++++++++++ src/svg.js | 7 +++-- 9 files changed, 156 insertions(+), 27 deletions(-) create mode 100644 src/mask.js diff --git a/README.md b/README.md index a1dac4e..857bf8d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # svg.js -A lightweight (3k gzipped) library for manipulating SVG. +A lightweight (3.5k gzipped) library for manipulating SVG. Svg.js is licensed under the terms of the MIT License. @@ -304,14 +304,41 @@ rect.clip(function(clipPath) { You can also reuse clip paths for multiple elements using `clipTo()`. ```javascript -var clipPath = doc.defs().clip(); -clipRect = clipPath.ellipse(80, 40).move(10, 10); +var clipPath = draw.defs().clip(); +var ellipse = clipPath.ellipse(80, 40).move(10, 10); rect.clipTo(clipPath); ``` _This functionality requires the clip.js module which is included in the default distribution._ +## Masking elements +Masking elements works very much the same as clipping with either `mask()` or `maskTo()`. + +Using `mask()` creates a mask in the parents 'defs' node, and passes it to a block: + +```javascript +var gradient = draw.gradient('linear', function(stop) { + stop.at({ offset: 0, color: '#000' }); + stop.at({ offset: 90, color: '#fff' }); +}); + +rect.mask(function(mask) { + mask.ellipse(80, 40).move(10, 10).fill({ color: gradient.fill() }); +}); +``` + +You can also reuse masks for multiple elements using `maskTo()`. + +```javascript +var mask = draw.defs().mask(); +var ellipse = mask.ellipse(80, 40).move(10, 10).fill({ color: gradient.fill() }); +rect.maskTo(mask); +``` + +_This functionality requires the mask.js module which is included in the default distribution._ + + ## Arranging elements You can arrange elements within their parent SVG document using the following methods: @@ -423,7 +450,7 @@ rect.click(function() { }); ``` -Available events are `click`, `dblclick`, `mousedown`, `mouseup`, `mouseover`, `mouseout` and `mousemove`. +Available events are `click`, `dblclick`, `mousedown`, `mouseup`, `mouseover`, `mouseout`, `mousemove`, `touchstart`, `touchend`, `touchmove` and `touchcancel`. diff --git a/Rakefile b/Rakefile index a3cf0d5..1a9e67c 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ SVGJS_VERSION = '0.1a' # all available modules in the correct loading order -MODULES = %w[ svg container element event group arrange defs clip gradient doc shape rect ellipse poly path image text sugar ] +MODULES = %w[ svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar ] # how many bytes in a "kilobyte" KILO = 1024 diff --git a/dist/svg.js b/dist/svg.js index 041beb7..363e890 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,10 +1,13 @@ -/* svg.js v0.1-48-gb663ead - svg container element event group arrange defs clip gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ +/* svg.js v0.1-49-g7d3e4c4 - svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ (function() { this.SVG = { // define default namespaces - ns: 'http://www.w3.org/2000/svg', - xlink: 'http://www.w3.org/1999/xlink', + ns: 'http://www.w3.org/2000/svg', + xlink: 'http://www.w3.org/1999/xlink', + + // initialize defs id sequence + did: 0, // method for element creation create: function(e) { @@ -347,7 +350,18 @@ }); - var eventTypes = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout', 'mousemove'].forEach(function(e) { + [ 'click', + 'dblclick', + 'mousedown', + 'mouseup', + 'mouseover', + 'mouseout', + 'mousemove', + 'touchstart', + 'touchend', + 'touchmove', + 'touchcancel' ].forEach(function(e) { + // add event to SVG.Elment SVG.Element.prototype[e] = function(f) { var s = this; @@ -428,13 +442,11 @@ // include the container object SVG.extend(SVG.Defs, SVG.Container); - var clipID = 0; - SVG.Clip = function Clip() { this.constructor.call(this, SVG.create('clipPath')); // set unique id - this.id = 'svgjs_clip_' + (clipID++); + this.id = 'svgjs_' + (SVG.did++); this.attr('id', this.id); }; @@ -472,13 +484,53 @@ }); - var gradID = 0; + SVG.Mask = function Mask() { + this.constructor.call(this, SVG.create('mask')); + + // set unique id + this.id = 'svgjs_' + (SVG.did++); + this.attr('id', this.id); + }; + + // inherit from SVG.Element + SVG.Mask.prototype = new SVG.Element(); + + // include the container object + SVG.extend(SVG.Mask, SVG.Container); + + // add clipping functionality to element + SVG.extend(SVG.Element, { + + // mask element using another element + mask: function(b) { + var m = this.parent.defs().mask(); + b(m); + return this.maskTo(m); + }, + + // distribute mask to svg element + maskTo: function(m) { + return this.attr('mask', 'url(#' + m.id + ')'); + } + + }); + + // add def-specific functions + SVG.extend(SVG.Defs, { + + // create clippath + mask: function() { + return this.put(new SVG.Mask()); + } + + }); + SVG.Gradient = function Gradient(t) { this.constructor.call(this, SVG.create(t + 'Gradient')); // set unique id - this.id = 'svgjs_grad_' + (gradID++); + this.id = 'svgjs_' + (SVG.did++); this.attr('id', this.id); // store type diff --git a/dist/svg.min.js b/dist/svg.min.js index 4366f14..6288063 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js v0.1-48-gb663ead - svg container element event group arrange defs clip gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ -function svg(e){return new SVG.Doc(e)}(function(){function i(){this.constructor.call(this,SVG.create("tspan"))}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)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]||null),e.parent=this),this},put:function(e,t){return this.add(e,t),e},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e){if(typeof e=="string")return this.trans[e];var t,n=[],r=this.bbox(),i=this.attr("transform")||"",s=i.match(/[a-z]+\([^\)]+\)/g)||[];for(t in e)e[t]!=null&&(this.trans[t]=e[t]);return e=this.trans,e.rotation!=0&&n.push("rotate("+e.rotation+","+(e.cx!=null?e.cx:r.cx)+","+(e.cy!=null?e.cy:r.cy)+")"),n.push("scale("+e.scaleX+","+e.scaleY+")"),e.skewX!=0&&n.push("skewX("+e.skewX+")"),e.skewY!=0&&n.push("skewY("+e.skewY+")"),n.push("translate("+e.x+","+e.y+")"),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return{x:e.x+this.trans.x,y:e.y+this.trans.y,cx:e.x+this.trans.x+e.width/2,cy:e.y+this.trans.y+e.height/2,width:e.width,height:e.height}},show:function(){return this.node.style.display="",this},hide:function(){return this.node.style.display="none",this},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t},_isStyle:function(e){return typeof e=="string"&&this._isText()?/^font|text|leading/.test(e):!1},_isText:function(){return this instanceof SVG.Text}});var e=["click","dblclick","mousedown","mouseup","mouseover","mouseout","mousemove"].forEach(function(e){SVG.Element.prototype[e]=function(t){var n=this;return this.node["on"+e]=function(){return t.apply(n,arguments)},this}});SVG.G=function(){this.constructor.call(this,SVG.create("g"))},SVG.G.prototype=new SVG.Element,SVG.extend(SVG.G,SVG.Container),SVG.extend(SVG.Element,{siblings:function(){return this.parent.children()},forward:function(){var e=this.siblings().indexOf(this);return this.parent.remove(this).put(this,e+1)},backward:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,e-1),this},front:function(){return this.parent.remove(this).put(this)},back:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}}),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container);var t=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_clip_"+t++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.parent.defs().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){return this.put(new SVG.Clip)}});var n=0;SVG.Gradient=function(t){this.constructor.call(this,SVG.create(t+"Gradient")),this.id="svgjs_grad_"+n++,this.attr("id",this.id),this.type=t},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){return this.put(new SVG.Stop(e))},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=this.put(new SVG.Gradient(e));return t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t,n="",r=["opacity","color"];for(t=r.length-1;t>=0;t--)e[r[t]]!=null&&(n+="stop-"+r[t]+":"+e[r[t]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attrs.offset||0)+"%",style:n})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg"));var n=document.createElement("div");n.style.cssText="position:relative;width:100%;height:100%;",typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",width:"100%",height:"100%"}).attr("xlink",SVG.xlink,SVG.ns).defs(),t.appendChild(n),n.appendChild(this.node),this.stage()},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:(t!=null?t:e)/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Poly={plot:function(e){return this.attr("points",e||"0,0")},move:function(e,t){return this.transform({x:e,y:t})}},SVG.Polyline=function(){this.constructor.call(this,SVG.create("polyline"))},SVG.Polyline.prototype=new SVG.Shape,SVG.extend(SVG.Polyline,SVG.Poly),SVG.Polygon=function(){this.constructor.call(this,SVG.create("polygon"))},SVG.Polygon.prototype=new SVG.Shape,SVG.extend(SVG.Polygon,SVG.Poly),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{plot:function(e){return this.attr("d",e||"M0,0")},move:function(e,t){return this.transform({x:e,y:t})}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("xlink:href",e,SVG.xlink)}});var r=["size","family","weight","stretch","variant","style"];SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica","text-anchor":"start"},this.leading=1.2},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text",this.lines=[];var t,n,r=this._style(),s=this.parentDoc(),o=e.split("\n"),u=this.style["font-size"];while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t=0;e--)this.style["font-"+r[e]]!=null&&(t+="font-"+r[e]+":"+this.style["font-"+r[e]]+";");return t+="text-anchor:"+this.style["text-anchor"]+";",t}}),i.prototype=new SVG.Shape,SVG.extend(i,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}});var s=["width","opacity","linecap","linejoin","miterlimit","dasharray","dashoffset"],o=["opacity","rule"];SVG.extend(SVG.Shape,{fill:function(e){var t;e.color!=null&&this.attr("fill",e.color);for(t=o.length-1;t>=0;t--)e[o[t]]!=null&&this.attr("fill-"+o[t],e[o[t]]);return this},stroke:function(e){var t;e.color&&this.attr("stroke",e.color);for(t=s.length-1;t>=0;t--)e[s[t]]!=null&&this.attr("stroke-"+s[t],e[s[t]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e,t,n){var r=this.bbox();return this.transform({rotation:e||0,cx:t==null?r.cx:t,cy:n==null?r.cx:n})},skew:function(e,t){return this.transform({skewX:e||0,skewY:t||0})}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform({x:e,y:t})}}),SVG.extend(SVG.Text,{font:function(e){var t,n={};for(t in e)t=="leading"?n[t]=e[t]:t=="anchor"?n["text-anchor"]=e[t]:r.indexOf(t)>-1?n["font-"+t]=e[t]:void 0;return this.attr(n).text(this.content)}})}).call(this); \ No newline at end of file +/* svg.js v0.1-49-g7d3e4c4 - svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ +function svg(e){return new SVG.Doc(e)}(function(){function t(){this.constructor.call(this,SVG.create("tspan"))}this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",did:0,create:function(e){return document.createElementNS(this.ns,e)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]||null),e.parent=this),this},put:function(e,t){return this.add(e,t),e},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e){if(typeof e=="string")return this.trans[e];var t,n=[],r=this.bbox(),i=this.attr("transform")||"",s=i.match(/[a-z]+\([^\)]+\)/g)||[];for(t in e)e[t]!=null&&(this.trans[t]=e[t]);return e=this.trans,e.rotation!=0&&n.push("rotate("+e.rotation+","+(e.cx!=null?e.cx:r.cx)+","+(e.cy!=null?e.cy:r.cy)+")"),n.push("scale("+e.scaleX+","+e.scaleY+")"),e.skewX!=0&&n.push("skewX("+e.skewX+")"),e.skewY!=0&&n.push("skewY("+e.skewY+")"),n.push("translate("+e.x+","+e.y+")"),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return{x:e.x+this.trans.x,y:e.y+this.trans.y,cx:e.x+this.trans.x+e.width/2,cy:e.y+this.trans.y+e.height/2,width:e.width,height:e.height}},show:function(){return this.node.style.display="",this},hide:function(){return this.node.style.display="none",this},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t},_isStyle:function(e){return typeof e=="string"&&this._isText()?/^font|text|leading/.test(e):!1},_isText:function(){return this instanceof SVG.Text}}),["click","dblclick","mousedown","mouseup","mouseover","mouseout","mousemove","touchstart","touchend","touchmove","touchcancel"].forEach(function(e){SVG.Element.prototype[e]=function(t){var n=this;return this.node["on"+e]=function(){return t.apply(n,arguments)},this}}),SVG.G=function(){this.constructor.call(this,SVG.create("g"))},SVG.G.prototype=new SVG.Element,SVG.extend(SVG.G,SVG.Container),SVG.extend(SVG.Element,{siblings:function(){return this.parent.children()},forward:function(){var e=this.siblings().indexOf(this);return this.parent.remove(this).put(this,e+1)},backward:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,e-1),this},front:function(){return this.parent.remove(this).put(this)},back:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}}),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container),SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_"+SVG.did++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.parent.defs().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){return this.put(new SVG.Clip)}}),SVG.Mask=function(){this.constructor.call(this,SVG.create("mask")),this.id="svgjs_"+SVG.did++,this.attr("id",this.id)},SVG.Mask.prototype=new SVG.Element,SVG.extend(SVG.Mask,SVG.Container),SVG.extend(SVG.Element,{mask:function(e){var t=this.parent.defs().mask();return e(t),this.maskTo(t)},maskTo:function(e){return this.attr("mask","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{mask:function(){return this.put(new SVG.Mask)}}),SVG.Gradient=function(t){this.constructor.call(this,SVG.create(t+"Gradient")),this.id="svgjs_"+SVG.did++,this.attr("id",this.id),this.type=t},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){return this.put(new SVG.Stop(e))},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=this.put(new SVG.Gradient(e));return t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t,n="",r=["opacity","color"];for(t=r.length-1;t>=0;t--)e[r[t]]!=null&&(n+="stop-"+r[t]+":"+e[r[t]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attrs.offset||0)+"%",style:n})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg"));var n=document.createElement("div");n.style.cssText="position:relative;width:100%;height:100%;",typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",width:"100%",height:"100%"}).attr("xlink",SVG.xlink,SVG.ns).defs(),t.appendChild(n),n.appendChild(this.node),this.stage()},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:(t!=null?t:e)/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Poly={plot:function(e){return this.attr("points",e||"0,0")},move:function(e,t){return this.transform({x:e,y:t})}},SVG.Polyline=function(){this.constructor.call(this,SVG.create("polyline"))},SVG.Polyline.prototype=new SVG.Shape,SVG.extend(SVG.Polyline,SVG.Poly),SVG.Polygon=function(){this.constructor.call(this,SVG.create("polygon"))},SVG.Polygon.prototype=new SVG.Shape,SVG.extend(SVG.Polygon,SVG.Poly),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{plot:function(e){return this.attr("d",e||"M0,0")},move:function(e,t){return this.transform({x:e,y:t})}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("xlink:href",e,SVG.xlink)}});var e=["size","family","weight","stretch","variant","style"];SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica","text-anchor":"start"},this.leading=1.2},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text",this.lines=[];var n,r,i=this._style(),s=this.parentDoc(),o=e.split("\n"),u=this.style["font-size"];while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(n=0,l=o.length;n=0;t--)this.style["font-"+e[t]]!=null&&(n+="font-"+e[t]+":"+this.style["font-"+e[t]]+";");return n+="text-anchor:"+this.style["text-anchor"]+";",n}}),t.prototype=new SVG.Shape,SVG.extend(t,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}});var n=["width","opacity","linecap","linejoin","miterlimit","dasharray","dashoffset"],r=["opacity","rule"];SVG.extend(SVG.Shape,{fill:function(e){var t;e.color!=null&&this.attr("fill",e.color);for(t=r.length-1;t>=0;t--)e[r[t]]!=null&&this.attr("fill-"+r[t],e[r[t]]);return this},stroke:function(e){var t;e.color&&this.attr("stroke",e.color);for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&this.attr("stroke-"+n[t],e[n[t]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e,t,n){var r=this.bbox();return this.transform({rotation:e||0,cx:t==null?r.cx:t,cy:n==null?r.cx:n})},skew:function(e,t){return this.transform({skewX:e||0,skewY:t||0})}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform({x:e,y:t})}}),SVG.extend(SVG.Text,{font:function(t){var n,r={};for(n in t)n=="leading"?r[n]=t[n]:n=="anchor"?r["text-anchor"]=t[n]:e.indexOf(n)>-1?r["font-"+n]=t[n]:void 0;return this.attr(r).text(this.content)}})}).call(this); \ No newline at end of file diff --git a/src/clip.js b/src/clip.js index 6d820df..ab7bebf 100644 --- a/src/clip.js +++ b/src/clip.js @@ -1,12 +1,9 @@ -// initialize id sequence -var clipID = 0; - SVG.Clip = function Clip() { this.constructor.call(this, SVG.create('clipPath')); // set unique id - this.id = 'svgjs_clip_' + (clipID++); + this.id = 'svgjs_' + (SVG.did++); this.attr('id', this.id); }; diff --git a/src/event.js b/src/event.js index 4f587d2..32c2be2 100644 --- a/src/event.js +++ b/src/event.js @@ -1,5 +1,16 @@ -var eventTypes = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout', 'mousemove'].forEach(function(e) { +[ 'click', + 'dblclick', + 'mousedown', + 'mouseup', + 'mouseover', + 'mouseout', + 'mousemove', + 'touchstart', + 'touchend', + 'touchmove', + 'touchcancel' ].forEach(function(e) { + // add event to SVG.Elment SVG.Element.prototype[e] = function(f) { var s = this; diff --git a/src/gradient.js b/src/gradient.js index b9007ef..b9533cb 100644 --- a/src/gradient.js +++ b/src/gradient.js @@ -1,12 +1,9 @@ -// initialize id sequence -var gradID = 0; - SVG.Gradient = function Gradient(t) { this.constructor.call(this, SVG.create(t + 'Gradient')); // set unique id - this.id = 'svgjs_grad_' + (gradID++); + this.id = 'svgjs_' + (SVG.did++); this.attr('id', this.id); // store type diff --git a/src/mask.js b/src/mask.js new file mode 100644 index 0000000..a9b457e --- /dev/null +++ b/src/mask.js @@ -0,0 +1,42 @@ + +SVG.Mask = function Mask() { + this.constructor.call(this, SVG.create('mask')); + + // set unique id + this.id = 'svgjs_' + (SVG.did++); + this.attr('id', this.id); +}; + +// inherit from SVG.Element +SVG.Mask.prototype = new SVG.Element(); + +// include the container object +SVG.extend(SVG.Mask, SVG.Container); + +// add clipping functionality to element +SVG.extend(SVG.Element, { + + // mask element using another element + mask: function(b) { + var m = this.parent.defs().mask(); + b(m); + + return this.maskTo(m); + }, + + // distribute mask to svg element + maskTo: function(m) { + return this.attr('mask', 'url(#' + m.id + ')'); + } + +}); + +// add def-specific functions +SVG.extend(SVG.Defs, { + + // create clippath + mask: function() { + return this.put(new SVG.Mask()); + } + +}); \ No newline at end of file diff --git a/src/svg.js b/src/svg.js index fd5f967..1a4095f 100644 --- a/src/svg.js +++ b/src/svg.js @@ -1,8 +1,11 @@ this.SVG = { // define default namespaces - ns: 'http://www.w3.org/2000/svg', - xlink: 'http://www.w3.org/1999/xlink', + ns: 'http://www.w3.org/2000/svg', + xlink: 'http://www.w3.org/1999/xlink', + + // initialize defs id sequence + did: 0, // method for element creation create: function(e) { -- 2.39.5