source: Dev/trunk/src/client/dojox/gfx/svgext.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 6.4 KB
Line 
1define([
2        "dojo/dom",
3        "dojo/_base/array",
4        "dojo/_base/window",
5        "./_base",
6        "./svg"],
7        function(dom, array, win, gfx, svg){
8
9        /*=====
10        return {
11                // summary:
12                //              A module that adds svg-specific features to the gfx api. You should require this module
13                //              when your application specifically targets the SVG renderer.
14        }
15        =====*/
16
17        var svgext = gfx.svgext = {};
18        var toIgnore = {
19                primitives:null,
20                tag:null,
21                children:null
22        };
23
24        function buildFilterPrimitivesDOM(primitive, parentNode){
25                var node =  parentNode.ownerDocument.createElementNS(svg.xmlns.svg, primitive.tag);
26                parentNode.appendChild(node);
27                for(var p in primitive){
28                        if(!(p in toIgnore)){
29                                node.setAttribute(p, primitive[p]);
30                        }
31                }
32                if(primitive.children){
33                        array.forEach(primitive.children, function(f){buildFilterPrimitivesDOM(f, node);});
34                }
35                return node;
36        }
37
38        /*=====
39        declare("dojox.gfx.svgext.__FilterPrimitiveArgs", null, {
40                // summary:
41                //              Represents an SVG filter primitive.
42                // description:
43                //              In addition to the following properties, a FilterPrimitiveArgs should define the properties specific to
44                //              this primitive, as defined by the SVG spec.
45                // example:
46                //              Define a simple feGaussianBlur primitive:
47                //      |       var blurPrimitive = {
48                //      |               "tag": "feGaussianBlur",
49                //      |               "in": "SourceAlpha",
50                //      |               "stdDeviation":"4",
51                //      |               "result":"blur"
52                //      |       };
53                //
54                // example:
55                //              Define a feSpecularLighting primitive with one fePointLight child
56                //      |       var lighting = {
57                //      |               "tag": "feSpecularLighting",
58                //      |               "in":"blur",
59                //      |               "surfaceScale":5,
60                //      |               "specularConstant":.75,
61                //      |               "specularExponent":20,
62                //      |               "lighting-color":"#bbbbbb",
63                //      |               "result":"specOut"
64                //      |               "children": [
65                //      |                       "tag": "fePointLight"
66                //      |                       "x":-5000,
67                //      |                       "y":-10000,
68                //      |                       "z":20000
69                //      |               ]
70                //      |       };
71
72                // tag: String?
73                //              The type of the primitive, as specified by the SVG spec (http://www.w3.org/TR/SVG/filters.html)
74                tag: null,
75
76                // children: dojox.gfx.svgext.__FilterPrimitiveArgs[]?
77                //              An array of child primitives, if any.
78                children: null
79        });
80        =====*/
81
82
83        /*=====
84        declare("dojox.gfx.svgext.__FilterArgs", null, {
85                // summary:
86                //              The filter arguments passed to the dojox/gfx/svgext/Shape.setFilter method.
87                // description:
88                //              An object defining the properties of a SVG Filter.
89                // example:
90                //              Define a drop shadow filter:
91                //      |       var filter = {
92                //      |               "id": "fastSmallDropShadow",
93                //      |               "x": "-10%",
94                //      |               "y": "-10%",
95                //      |               "width": "125%",
96                //      |               "height": "125%",
97                //      |               "primitives": [{
98                //      |                       "tag": "feColorMatrix",
99                //      |                       "in": "SourceAlpha",
100                //      |                       "type": "matrix",
101                //      |                       "result": "grey",
102                //      |                       "values": "0.2125,0.7154,0.0721,0,0,0.2125,0.7154,0.0721,0,0,0.2125,0.7154,0.0721,0,0,0,0,0,0.7,0"
103                //      |               },{
104                //      |                       "tag": "feOffset",
105                //      |                       "dx": 3,
106                //      |                       "dy": 3,
107                //      |                       "result": "offsetBlur"
108                //      |               },{
109                //      |                       "tag": "feMerge",
110                //      |                       "children":[{
111                //      |                               "tag": "feMergeNode",
112                //      |                               "in": "offsetBlur"
113                //      |                       },{
114                //      |                               "tag": "feMergeNode",
115                //      |                               "in": "SourceGraphic"
116                //      |                       }]
117                //      |               }]
118                //      |       };
119
120
121                // id: String?
122                //              The filter identifier. If none is provided, a generated id will be used.
123                id: null,
124
125                // filterUnits: String?
126                //              The coordinate system of the filter. Default is "userSpaceOnUse".
127                filterUnits: "userSpaceOnUse",
128
129                // primitives: dojox.gfx.svgext.__FilterPrimitiveArgs[]
130                //              An array of filter primitives that define this filter.
131                primitives: []
132        });
133        =====*/
134
135        svg.Shape.extend({
136                addRenderingOption: function(/*String*/option, /*String*/value){
137                        // summary:
138                        //              Adds the specified SVG rendering option on this shape.
139                        // option: String
140                        //              The name of the rendering option to add to this shape, as specified by the
141                        //              SVG specification (http://www.w3.org/TR/SVG/painting.html#RenderingProperties)
142                        // value: String
143                        //              the option value.
144                        this.rawNode.setAttribute(option, value);
145                        return this; // self
146                },
147
148                setFilter: function(/*dojox.gfx.svgext.__FilterArgs*/filter){
149                        // summary:
150                        //              Sets the specified SVG Filter on this shape.
151                        // filter: dojox.gfx.svgext.__FilterArgs
152                        //              An object that defines the filter properties. Note that in order to make the creation of such filter
153                        //              easier, the dojox/gfx/filters module defines both a simple API to easily create filter objects and
154                        //              also a set of predefined filters like drop shadows, blurs, textures effects (among others).
155                        // example:
156                        //              Sets a drop shadow filter:
157                        //      |       var filter = {
158                        //      |               "id": "fastSmallDropShadow",
159                        //      |               "x": "-10%",
160                        //      |               "y": "-10%",
161                        //      |               "width": "125%",
162                        //      |               "height": "125%",
163                        //      |               "primitives": [{
164                        //      |                       "tag": "feColorMatrix",
165                        //      |                       "in": "SourceAlpha",
166                        //      |                       "type": "matrix",
167                        //      |                       "result": "grey",
168                        //      |                       "values": "0.2125,0.7154,0.0721,0,0,0.2125,0.7154,0.0721,0,0,0.2125,0.7154,0.0721,0,0,0,0,0,0.7,0"
169                        //      |               },{
170                        //      |                       "tag": "feOffset",
171                        //      |                       "dx": 3,
172                        //      |                       "dy": 3,
173                        //      |                       "result": "offsetBlur"
174                        //      |               },{
175                        //      |                       "tag": "feMerge",
176                        //      |                       "in": "offsetBlur",
177                        //      |                       "in2": "SourceGraphic"
178                        //      |               }]
179                        //      |       };
180                        //      |       var shape = surface.createRect().setFilter(filter);
181                        //
182                        // example:
183                        //              Sets a predefined filter from the dojox/gfx/filters module:
184                        //      |       require(["dojox/gfx/filters","dojox/gfx/svgext",...], function(filters, svgext){
185                        //      |               ...
186                        //      |               var filter = filters.textures.paper({"id":"myFilter","x":0,"y":0,"width":100,"height":100});
187                        //      |               var shape = surface.createRect().setFilter(filter);
188
189                        if(!filter){
190                                this.rawNode.removeAttribute("filter");
191                                this.filter = null;
192                                return this;
193                        }
194                        this.filter = filter;
195                        filter.id = filter.id || gfx._base._getUniqueId();
196                        var filterNode = dom.byId(filter.id);
197                        if(!filterNode){
198                                filterNode = this.rawNode.ownerDocument.createElementNS(svg.xmlns.svg, "filter");
199                                filterNode.setAttribute("filterUnits", "userSpaceOnUse");
200                                for(var p in filter){
201                                        if(!(p in toIgnore)){
202                                                filterNode.setAttribute(p, filter[p]);
203                                        }
204                                }
205                                array.forEach(filter.primitives, function(p){
206                                        buildFilterPrimitivesDOM(p, filterNode);
207                                });
208                                var surface = this._getParentSurface();
209                                if(surface){
210                                        var defs = surface.defNode;
211                                        defs.appendChild(filterNode);
212                                }
213                        }
214                        this.rawNode.setAttribute("filter", "url(#"+filter.id+")");
215                        return this;
216                },
217
218                getFilter: function(){
219                        // summary:
220                        //              Gets the shape SVG Filter.
221                        return this.filter;
222                }
223        });
224        return svgext;
225});
Note: See TracBrowser for help on using the repository browser.