comparison nodejs/animate.js @ 895:3136db0ac01b abs_n_rel_center

Merged from default branch
author Thinker K.F. Li <thinker@branda.to>
date Tue, 28 Sep 2010 17:27:01 +0800
parents 3ce9daa9558b 460b2629be30
children b38161a3abae
comparison
equal deleted inserted replaced
891:96bc29e948cc 895:3136db0ac01b
7 * machines, ffs can be decreased or increased respective. 7 * machines, ffs can be decreased or increased respective.
8 */ 8 */
9 var ffs = 12; 9 var ffs = 12;
10 var frame_interval = 1000 / ffs; 10 var frame_interval = 1000 / ffs;
11 11
12 function linear_draw() { 12 function shift_draw(percent) {
13 var percent;
14 var x, y; 13 var x, y;
15 14
16 percent = (Date.now() - this._start_tm) / this.duration; 15 x = (this.targetx - this.startposx) * percent + this.startposx;
17 if(percent >= 1) { 16 y = (this.targety - this.startposy) * percent + this.startposy;
18 percent = 1;
19 if (this.obj.timer) {
20 this.obj.timer.stop();
21 delete this.obj.timer;
22 }
23 }
24 x = (this.targetx-this.startposx)*percent+this.startposx;
25 y = (this.targety-this.startposy)*percent+this.startposy;
26 this.obj.center.move(x, y); 17 this.obj.center.move(x, y);
27 this.app.refresh(); 18 this.app.refresh();
28 } 19 }
29 20
30 function linear_draw_start() { 21 function shift(app,obj,shiftx,shifty) {
31 var obj = this.obj; 22 obj.animated_shift = this;
32 var self = this;
33
34 if(obj.timer)
35 obj.timer.stop();
36
37 this._start_tm = Date.now();
38 obj.timer = setInterval(function() { self.draw(); }, frame_interval);
39 }
40
41 function linear(app,obj,shiftx,shifty,duration) {
42 obj.animated_linear = this;
43 this.app = app; 23 this.app = app;
44 this.obj = obj; 24 this.obj = obj;
45 this.end = 0; 25 this.end = 0;
46 this.targetx = shiftx + obj.center.x; 26 this.targetx = shiftx + obj.center.x;
47 this.targety = shifty + obj.center.y; 27 this.targety = shifty + obj.center.y;
48 this.startposx = obj.center.x; 28 this.startposx = obj.center.x;
49 this.startposy = obj.center.y; 29 this.startposy = obj.center.y;
50 this.duration = duration*1000; 30 }
51 } 31
52 32 exports.shift = shift;
53 exports.linear = linear; 33 shift.prototype.draw = shift_draw;
54 linear.prototype.start = linear_draw_start;
55 linear.prototype.draw = linear_draw;
56 34
57 /* ------------------------------------------------------------ */ 35 /* ------------------------------------------------------------ */
58 function rotate(app, obj, ang, duration) { 36 function rotate(app, obj, ang, duration) {
59 this._app = app; 37 this._app = app;
60 this._obj = obj; 38 this._obj = obj;
61 this._ang = ang; 39 this._ang = ang;
62 this._duration = duration * 1000;
63 }
64
65 function rotate_start() {
66 var obj = this._obj;
67 var self = this;
68
69 if(obj.timer)
70 obj.timer.stop();
71
72 this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; 40 this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]];
73 this._start_tm = Date.now(); 41 }
74 obj.timer = setInterval(function() { self.draw(); }, frame_interval); 42
75 } 43 function rotate_draw(percent) {
76
77 function rotate_draw() {
78 var percent; 44 var percent;
79 var ang; 45 var ang;
80 var sv, cv; 46 var sv, cv;
81 var obj = this._obj; 47 var obj = this._obj;
82 var mtx, shift; 48 var mtx, shift;
83 49
84 percent = (Date.now() - this._start_tm) / this._duration;
85 if(percent >= 1) {
86 percent = 1;
87 obj.timer.stop();
88 }
89 50
90 ang = percent * this._ang; 51 ang = percent * this._ang;
91 sv = Math.sin(ang); 52 sv = Math.sin(ang);
92 cv = Math.cos(ang); 53 cv = Math.cos(ang);
93 mtx = [cv, -sv, 0, sv, cv, 0]; 54 mtx = [cv, -sv, 0, sv, cv, 0];
94 55 sys.puts('x='+obj.center.x+',y='+obj.center.y);
95 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y]; 56 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y];
96 mtx = multiply(mtx, shift); 57 mtx = multiply(mtx, shift);
97 shift = [1, 0, obj.center.x, 0, 1, obj.center.y]; 58 shift = [1, 0, obj.center.x, 0, 1, obj.center.y];
98 mtx = multiply(shift, mtx); 59 mtx = multiply(shift, mtx);
99 mtx = multiply(mtx, this._start_mtx); 60 mtx = multiply(mtx, this._start_mtx);
106 obj[5] = mtx[5]; 67 obj[5] = mtx[5];
107 68
108 this._app.refresh(); 69 this._app.refresh();
109 } 70 }
110 71
111 rotate.prototype.start = rotate_start;
112 rotate.prototype.draw = rotate_draw; 72 rotate.prototype.draw = rotate_draw;
113 exports.rotate = rotate; 73 exports.rotate = rotate;
114 74
115 function multiply(s,d) { 75 function multiply(s,d) {
116 var m=[]; 76 var m=[];
122 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; 82 m[5] = s[3]*d[2]+s[4]*d[5]+s[5];
123 return m; 83 return m;
124 } 84 }
125 85
126 86
127 function scale_draw() { 87 function scale_draw(percent) {
128 if (this.end == 1) return;
129 var percent = (Date.now() - this.starttime)/this.duration;
130 if (percent > 1) percent = 1;
131 var sx = 1 + (this.totalsx - 1) * percent; 88 var sx = 1 + (this.totalsx - 1) * percent;
132 var sy = 1 + (this.totalsy - 1) * percent; 89 var sy = 1 + (this.totalsy - 1) * percent;
133 var sh1 = [1, 0, -this.center_x, 0, 1, -this.center_y]; 90 var sh1 = [1, 0, -this.center_x, 0, 1, -this.center_y];
134 var sh2 = [1, 0, this.center_x, 0, 1, this.center_y]; 91 var sh2 = [1, 0, this.center_x, 0, 1, this.center_y];
135 var scale=[sx, 0, 0, 0, sy, 0]; 92 var scale = [sx, 0, 0, 0, sy, 0];
136 var obj = this.obj; 93 var obj = this.obj;
137 var mtx; 94 var mtx;
138 95
139 mtx = multiply(scale, sh1); 96 mtx = multiply(scale, sh1);
140 mtx = multiply(sh2, mtx); 97 mtx = multiply(sh2, mtx);
145 obj[3] = mtx[3]; 102 obj[3] = mtx[3];
146 obj[4] = mtx[4]; 103 obj[4] = mtx[4];
147 obj[5] = mtx[5]; 104 obj[5] = mtx[5];
148 105
149 this.app.refresh(); 106 this.app.refresh();
150 var self = this;
151 if (percent < 1) {
152 obj.timer=setTimeout(function() { self.draw();}, frame_interval);
153 return;
154 }
155 this.app.refresh();
156 obj.animated_scale = null;
157 } 107 }
158 108
159 function scale(app, obj, fact_x, fact_y, duration) { 109 function scale(app, obj, fact_x, fact_y, duration) {
160 var bbox; 110 var bbox;
161 111
182 this.orig_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; 132 this.orig_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]];
183 } 133 }
184 134
185 135
186 exports.scale = scale; 136 exports.scale = scale;
187 scale.prototype.start = scale_draw;
188 scale.prototype.draw = scale_draw; 137 scale.prototype.draw = scale_draw;
189 138
190 function holder(app, coord) { 139 function holder(app, coord) {
191 var mtx = [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]]; 140 var mtx = [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]];
192 141
223 172
224 exports.holder = holder; 173 exports.holder = holder;
225 174
226 175
227 176
228 function alpha_draw() { 177 function alpha_draw(percent) {
229 178
230 if (this.end == 1) return; 179 if (this.end == 1) return;
231 var percent = (Date.now() - this.starttime)/this.duration;
232 if (percent > 1) percent = 1;
233 var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha; 180 var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha;
234 this.obj.opacity=sx; 181 this.obj.opacity=sx;
235
236 this.app.refresh();
237 var self = this;
238 if (percent < 1) {
239 this.obj.timer=setTimeout(function() { self.draw();}, frame_interval);
240 return;
241 }
242 this.app.refresh(); 182 this.app.refresh();
243 this.obj.animated_alpha = null; 183 this.obj.animated_alpha = null;
244 } 184 }
245 185
246 function alpha(app,obj,alpha, duration) { 186 function alpha(app,obj,alpha, duration) {
259 this.startalpha = obj.opacity; 199 this.startalpha = obj.opacity;
260 this.targetalpha = alpha; 200 this.targetalpha = alpha;
261 this.duration = duration*1000; 201 this.duration = duration*1000;
262 } 202 }
263 203
264 alpha.prototype.start = alpha_draw;
265 alpha.prototype.draw = alpha_draw; 204 alpha.prototype.draw = alpha_draw;
266 exports.alpha = alpha; 205 exports.alpha = alpha;
206
207 function linear_update()
208 {
209 var now = Date.now();
210 var i;
211
212 sys.puts("real time is "+now);
213 sys.puts("end is "+this.end);
214 if (now >= this.end) {
215 this.timer.stop();
216 now = this.end;
217 }
218 if (now < this.startmove) return;
219 sys.puts("now is "+now+" offset is "+(now-this.startmove));
220 var per = (now-this.startmove)/this.duration/1000;
221 if (per > 1) per = 1;
222 this.action.draw(per);
223 }
224
225 function linear_start()
226 {
227 var self = this;
228 if (this.timer)
229 this.timer.stop();
230 this.timer = setInterval(function() { self.update();}, frame_interval);
231 this.startmove = Date.now()+this.starttime*1000;
232 this.end = this.startmove+this.duration*1000;
233 }
234 function linear_stop()
235 {
236 if (this.timer) {
237 this.timer.stop();
238 this.timer = null;
239 }
240 }
241
242 function linear_finish()
243 {
244 this.action.draw(1);
245 }
246 function linear(action,start, duration)
247 {
248 this.action = action;
249 this.duration = duration;
250 this.starttime = start;
251 this.timer=null;
252 }
253 linear.prototype.update = linear_update;
254 linear.prototype.start = linear_start;
255 linear.prototype.stop = linear_stop;
256 linear.prototype.finish = linear_finish;
257 exports.linear = linear;
258
259
260 function program(words)
261 {
262 this.words = wrods;
263 }
264
265 program.prototype.start=function() {
266 for(w in this.words) {
267 w.start();
268 }
269 }
270
271 program.prototype.stop=function(s) {
272 for(w in this.words) {
273 w.stop();
274 }
275 }
276 program.prototype.finish=function() {
277 for(w in this.words) {
278 w.finish();
279 }
280 }
281
282 exports.run = function(actions,start,duration) {
283 for(a in actions) {
284 var li = new linear(actions[a],start,duration);
285 sys.puts(li);
286 li.start();
287 }
288 }
289 exports.runexp=function(actions,start,exp) {
290 }