]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/gfx/fx.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / gfx / fx.js
1 if(!dojo._hasResource["dojox.gfx.fx"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.gfx.fx"] = true;
3 dojo.provide("dojox.gfx.fx");
4
5 dojo.require("dojox.gfx.matrix");
6
7 (function(){
8         var d = dojo, g = dojox.gfx, m = g.matrix;
9         
10         // Generic interpolators. Should they be moved to dojox.fx?
11         
12         var InterpolNumber = function(start, end){
13                 this.start = start, this.end = end;
14         };
15         d.extend(InterpolNumber, {
16                 getValue: function(r){
17                         return (this.end - this.start) * r + this.start;
18                 }
19         });
20         
21         var InterpolUnit = function(start, end, unit){
22                 this.start = start, this.end = end;
23                 this.unit = unit;
24         };
25         d.extend(InterpolUnit, {
26                 getValue: function(r){
27                         return (this.end - this.start) * r + this.start + this.unit;
28                 }
29         });
30         
31         var InterpolColor = function(start, end){
32                 this.start = start, this.end = end;
33                 this.temp = new dojo.Color();
34         };
35         d.extend(InterpolColor, {
36                 getValue: function(r){
37                         return d.blendColors(this.start, this.end, r, this.temp);
38                 }
39         });
40         
41         var InterpolValues = function(values){
42                 this.values = values;
43                 this.length = values.length;
44         };
45         d.extend(InterpolValues, {
46                 getValue: function(r){
47                         return this.values[Math.min(Math.floor(r * this.length), this.length - 1)];
48                 }
49         });
50
51         var InterpolObject = function(values, def){
52                 this.values = values;
53                 this.def = def ? def : {};
54         };
55         d.extend(InterpolObject, {
56                 getValue: function(r){
57                         var ret = dojo.clone(this.def);
58                         for(var i in this.values){
59                                 ret[i] = this.values[i].getValue(r);
60                         }
61                         return ret;
62                 }
63         });
64         
65         var InterpolTransform = function(stack, original){
66                 this.stack = stack;
67                 this.original = original;
68         };
69         d.extend(InterpolTransform, {
70                 getValue: function(r){
71                         var ret = [];
72                         dojo.forEach(this.stack, function(t){
73                                 if(t instanceof m.Matrix2D){
74                                         ret.push(t);
75                                         return;
76                                 }
77                                 if(t.name == "original" && this.original){
78                                         ret.push(this.original);
79                                         return;
80                                 }
81                                 if(!(t.name in m)){ return; }
82                                 var f = m[t.name];
83                                 if(typeof f != "function"){
84                                         // constant
85                                         ret.push(f);
86                                         return;
87                                 }
88                                 var val = dojo.map(t.start, function(v, i){
89                                                                 return (t.end[i] - v) * r + v;
90                                                         }),
91                                         matrix = f.apply(m, val);
92                                 if(matrix instanceof m.Matrix2D){
93                                         ret.push(matrix);
94                                 }
95                         }, this);
96                         return ret;
97                 }
98         });
99         
100         var transparent = new d.Color(0, 0, 0, 0);
101         
102         var getColorInterpol = function(prop, obj, name, def){
103                 if(prop.values){
104                         return new InterpolValues(prop.values);
105                 }
106                 var value, start, end;
107                 if(prop.start){
108                         start = g.normalizeColor(prop.start);
109                 }else{
110                         start = value = obj ? (name ? obj[name] : obj) : def;
111                 }
112                 if(prop.end){
113                         end = g.normalizeColor(prop.end);
114                 }else{
115                         if(!value){
116                                 value = obj ? (name ? obj[name] : obj) : def;
117                         }
118                         end = value;
119                 }
120                 return new InterpolColor(start, end);
121         };
122         
123         var getNumberInterpol = function(prop, obj, name, def){
124                 if(prop.values){
125                         return new InterpolValues(prop.values);
126                 }
127                 var value, start, end;
128                 if(prop.start){
129                         start = prop.start;
130                 }else{
131                         start = value = obj ? obj[name] : def;
132                 }
133                 if(prop.end){
134                         end = prop.end;
135                 }else{
136                         if(typeof value != "number"){
137                                 value = obj ? obj[name] : def;
138                         }
139                         end = value;
140                 }
141                 return new InterpolNumber(start, end);
142         };
143         
144         g.fx.animateStroke = function(/*Object*/ args){
145                 // summary:
146                 //      returns the animation, which will change stroke properties over time
147                 // example:
148                 //      |       dojox.gfx.fx.animateStroke{{
149                 //      |               shape: shape,
150                 //      |               duration: 500,
151                 //      |               color: {start: "red", end: "green"},
152                 //      |               width: {end: 15},
153                 //      |               join:  {values: ["miter", "bevel", "round"]}
154                 //      |       }).play();
155                 if(!args.easing){ args.easing = d._defaultEasing; }
156                 var anim = new d._Animation(args), shape = args.shape, stroke;
157                 d.connect(anim, "beforeBegin", anim, function(){
158                         stroke = shape.getStroke();
159                         var prop = args.color, values = {}, value, start, end;
160                         if(prop){
161                                 values.color = getColorInterpol(prop, stroke, "color", transparent);
162                         }
163                         prop = args.style;
164                         if(prop && prop.values){
165                                 values.style = new InterpolValues(prop.values);
166                         }
167                         prop = args.width;
168                         if(prop){
169                                 values.width = getNumberInterpol(prop, stroke, "width", 1);
170                         }
171                         prop = args.cap;
172                         if(prop && prop.values){
173                                 values.cap = new InterpolValues(prop.values);
174                         }
175                         prop = args.join;
176                         if(prop){
177                                 if(prop.values){
178                                         values.join = new InterpolValues(prop.values);
179                                 }else{
180                                         start = prop.start ? prop.start : (stroke && stroke.join || 0);
181                                         end = prop.end ? prop.end : (stroke && stroke.join || 0);
182                                         if(typeof start == "number" && typeof end == "number"){
183                                                 values.join = new InterpolNumber(start, end);
184                                         }
185                                 }
186                         }
187                         this.curve = new InterpolObject(values, stroke);
188                 });
189                 d.connect(anim, "onAnimate", shape, "setStroke");
190                 return anim;
191         };
192
193         g.fx.animateFill = function(/*Object*/ args){
194                 // summary:
195                 //      returns the animation, which will change fill color over time,
196                 //      only solid fill color is supported at the moment
197                 // example:
198                 //      |       dojox.gfx.fx.animateFill{{
199                 //      |               shape: shape,
200                 //      |               duration: 500,
201                 //      |               color: {start: "red", end: "green"}
202                 //      |       }).play();
203                 if(!args.easing){ args.easing = d._defaultEasing; }
204                 var anim = new d._Animation(args), shape = args.shape, fill;
205                 d.connect(anim, "beforeBegin", anim, function(){
206                         fill = shape.getFill();
207                         var prop = args.color, values = {};
208                         if(prop){
209                                 this.curve = getColorInterpol(prop, fill, "", transparent);
210                         }
211                 });
212                 d.connect(anim, "onAnimate", shape, "setFill");
213                 return anim;
214         };
215
216         g.fx.animateFont = function(/*Object*/ args){
217                 // summary:
218                 //      returns the animation, which will change font properties over time
219                 // example:
220                 //      |       dojox.gfx.fx.animateFont{{
221                 //      |               shape: shape,
222                 //      |               duration: 500,
223                 //      |               variant: {values: ["normal", "small-caps"]},
224                 //      |               size:  {end: 10, unit: "pt"}
225                 //      |       }).play();
226                 if(!args.easing){ args.easing = d._defaultEasing; }
227                 var anim = new d._Animation(args), shape = args.shape, font;
228                 d.connect(anim, "beforeBegin", anim, function(){
229                         font = shape.getFont();
230                         var prop = args.style, values = {}, value, start, end;
231                         if(prop && prop.values){
232                                 values.style = new InterpolValues(prop.values);
233                         }
234                         prop = args.variant;
235                         if(prop && prop.values){
236                                 values.variant = new InterpolValues(prop.values);
237                         }
238                         prop = args.weight;
239                         if(prop && prop.values){
240                                 values.weight = new InterpolValues(prop.values);
241                         }
242                         prop = args.family;
243                         if(prop && prop.values){
244                                 values.family = new InterpolValues(prop.values);
245                         }
246                         prop = args.size;
247                         if(prop && prop.unit){
248                                 start = parseFloat(prop.start ? prop.start : (shape.font && shape.font.size || "0"));
249                                 end = parseFloat(prop.end ? prop.end : (shape.font && shape.font.size || "0"));
250                                 values.size = new InterpolUnit(start, end, prop.unit);
251                         }
252                         this.curve = new InterpolObject(values, font);
253                 });
254                 d.connect(anim, "onAnimate", shape, "setFont");
255                 return anim;
256         };
257
258         g.fx.animateTransform = function(/*Object*/ args){
259                 // summary:
260                 //      returns the animation, which will change transformation over time
261                 // example:
262                 //      |       dojox.gfx.fx.animateTransform{{
263                 //      |               shape: shape,
264                 //      |               duration: 500,
265                 //      |               transform: [
266                 //      |                       {name: "translate", start: [0, 0], end: [200, 200]},
267                 //      |                       {name: "original"}
268                 //      |               ]
269                 //      |       }).play();
270                 if(!args.easing){ args.easing = d._defaultEasing; }
271                 var anim = new d._Animation(args), shape = args.shape, original;
272                 d.connect(anim, "beforeBegin", anim, function(){
273                         original = shape.getTransform();
274                         this.curve = new InterpolTransform(args.transform, original);
275                 });
276                 d.connect(anim, "onAnimate", shape, "setTransform");
277                 return anim;
278         };
279 })();
280
281 }