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");
5 dojo.require("dojox.gfx.matrix");
8 var d = dojo, g = dojox.gfx, m = g.matrix;
10 // Generic interpolators. Should they be moved to dojox.fx?
12 var InterpolNumber = function(start, end){
13 this.start = start, this.end = end;
15 d.extend(InterpolNumber, {
16 getValue: function(r){
17 return (this.end - this.start) * r + this.start;
21 var InterpolUnit = function(start, end, unit){
22 this.start = start, this.end = end;
25 d.extend(InterpolUnit, {
26 getValue: function(r){
27 return (this.end - this.start) * r + this.start + this.unit;
31 var InterpolColor = function(start, end){
32 this.start = start, this.end = end;
33 this.temp = new dojo.Color();
35 d.extend(InterpolColor, {
36 getValue: function(r){
37 return d.blendColors(this.start, this.end, r, this.temp);
41 var InterpolValues = function(values){
43 this.length = values.length;
45 d.extend(InterpolValues, {
46 getValue: function(r){
47 return this.values[Math.min(Math.floor(r * this.length), this.length - 1)];
51 var InterpolObject = function(values, def){
53 this.def = def ? def : {};
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);
65 var InterpolTransform = function(stack, original){
67 this.original = original;
69 d.extend(InterpolTransform, {
70 getValue: function(r){
72 dojo.forEach(this.stack, function(t){
73 if(t instanceof m.Matrix2D){
77 if(t.name == "original" && this.original){
78 ret.push(this.original);
81 if(!(t.name in m)){ return; }
83 if(typeof f != "function"){
88 var val = dojo.map(t.start, function(v, i){
89 return (t.end[i] - v) * r + v;
91 matrix = f.apply(m, val);
92 if(matrix instanceof m.Matrix2D){
100 var transparent = new d.Color(0, 0, 0, 0);
102 var getColorInterpol = function(prop, obj, name, def){
104 return new InterpolValues(prop.values);
106 var value, start, end;
108 start = g.normalizeColor(prop.start);
110 start = value = obj ? (name ? obj[name] : obj) : def;
113 end = g.normalizeColor(prop.end);
116 value = obj ? (name ? obj[name] : obj) : def;
120 return new InterpolColor(start, end);
123 var getNumberInterpol = function(prop, obj, name, def){
125 return new InterpolValues(prop.values);
127 var value, start, end;
131 start = value = obj ? obj[name] : def;
136 if(typeof value != "number"){
137 value = obj ? obj[name] : def;
141 return new InterpolNumber(start, end);
144 g.fx.animateStroke = function(/*Object*/ args){
146 // returns the animation, which will change stroke properties over time
148 // | dojox.gfx.fx.animateStroke{{
151 // | color: {start: "red", end: "green"},
152 // | width: {end: 15},
153 // | join: {values: ["miter", "bevel", "round"]}
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;
161 values.color = getColorInterpol(prop, stroke, "color", transparent);
164 if(prop && prop.values){
165 values.style = new InterpolValues(prop.values);
169 values.width = getNumberInterpol(prop, stroke, "width", 1);
172 if(prop && prop.values){
173 values.cap = new InterpolValues(prop.values);
178 values.join = new InterpolValues(prop.values);
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);
187 this.curve = new InterpolObject(values, stroke);
189 d.connect(anim, "onAnimate", shape, "setStroke");
193 g.fx.animateFill = function(/*Object*/ args){
195 // returns the animation, which will change fill color over time,
196 // only solid fill color is supported at the moment
198 // | dojox.gfx.fx.animateFill{{
201 // | color: {start: "red", end: "green"}
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 = {};
209 this.curve = getColorInterpol(prop, fill, "", transparent);
212 d.connect(anim, "onAnimate", shape, "setFill");
216 g.fx.animateFont = function(/*Object*/ args){
218 // returns the animation, which will change font properties over time
220 // | dojox.gfx.fx.animateFont{{
223 // | variant: {values: ["normal", "small-caps"]},
224 // | size: {end: 10, unit: "pt"}
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);
235 if(prop && prop.values){
236 values.variant = new InterpolValues(prop.values);
239 if(prop && prop.values){
240 values.weight = new InterpolValues(prop.values);
243 if(prop && prop.values){
244 values.family = new InterpolValues(prop.values);
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);
252 this.curve = new InterpolObject(values, font);
254 d.connect(anim, "onAnimate", shape, "setFont");
258 g.fx.animateTransform = function(/*Object*/ args){
260 // returns the animation, which will change transformation over time
262 // | dojox.gfx.fx.animateTransform{{
266 // | {name: "translate", start: [0, 0], end: [200, 200]},
267 // | {name: "original"}
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);
276 d.connect(anim, "onAnimate", shape, "setTransform");