Mercurial > MadButterfly
comparison nodejs/animate.js @ 893:cad38ddb1253
Backed out changeset 23dffb564ace
author | wycc |
---|---|
date | Mon, 27 Sep 2010 23:00:51 +0800 |
parents | 23dffb564ace |
children | 460b2629be30 |
comparison
equal
deleted
inserted
replaced
892:23dffb564ace | 893:cad38ddb1253 |
---|---|
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 shift_draw(percent) { | 12 function linear_draw() { |
13 var percent; | 13 var percent; |
14 | |
15 percent = (Date.now() - this._start_tm) / this.duration; | |
16 if(percent >= 1) { | |
17 percent = 1; | |
18 if (this.obj.timer) { | |
19 this.obj.timer.stop(); | |
20 delete this.obj.timer; | |
21 } | |
22 } | |
14 this.obj.x = (this.targetx-this.startposx)*percent+this.startposx; | 23 this.obj.x = (this.targetx-this.startposx)*percent+this.startposx; |
15 this.obj.y = (this.targety-this.startposy)*percent+this.startposy; | 24 this.obj.y = (this.targety-this.startposy)*percent+this.startposy; |
16 this.app.refresh(); | 25 this.app.refresh(); |
17 } | 26 } |
18 | 27 |
19 function shift(app,obj,shiftx,shifty) { | 28 function linear_draw_start() { |
20 obj.animated_shift = this; | 29 var obj = this.obj; |
30 var self = this; | |
31 | |
32 if(obj.timer) | |
33 obj.timer.stop(); | |
34 | |
35 this._start_tm = Date.now(); | |
36 obj.timer = setInterval(function() { self.draw(); }, frame_interval); | |
37 } | |
38 | |
39 function linear(app,obj,shiftx,shifty,duration) { | |
40 obj.animated_linear = this; | |
21 this.app = app; | 41 this.app = app; |
22 this.obj = obj; | 42 this.obj = obj; |
23 this.end = 0; | 43 this.end = 0; |
24 this.targetx = shiftx + obj.x; | 44 this.targetx = shiftx + obj.x; |
25 this.targety = shifty + obj.y; | 45 this.targety = shifty + obj.y; |
26 this.startposx = obj.x; | 46 this.startposx = obj.x; |
27 this.startposy = obj.y; | 47 this.startposy = obj.y; |
28 } | 48 this.duration = duration*1000; |
29 | 49 } |
30 exports.shift = shift; | 50 |
31 shift.prototype.draw = shift_draw; | 51 exports.linear = linear; |
52 linear.prototype.start = linear_draw_start; | |
53 linear.prototype.draw = linear_draw; | |
32 | 54 |
33 /* ------------------------------------------------------------ */ | 55 /* ------------------------------------------------------------ */ |
34 function rotate(app, obj, ang, duration) { | 56 function rotate(app, obj, ang, duration) { |
35 this._app = app; | 57 this._app = app; |
36 this._obj = obj; | 58 this._obj = obj; |
37 this._ang = ang; | 59 this._ang = ang; |
60 this._duration = duration * 1000; | |
61 } | |
62 | |
63 function rotate_start() { | |
64 var obj = this._obj; | |
65 var self = this; | |
66 | |
38 this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; | 67 this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; |
39 } | 68 this._start_tm = Date.now(); |
40 | 69 obj.timer = setInterval(function() { self.draw(); }, frame_interval); |
41 function rotate_draw(percent) { | 70 } |
71 | |
72 function rotate_draw() { | |
42 var percent; | 73 var percent; |
43 var ang; | 74 var ang; |
44 var sv, cv; | 75 var sv, cv; |
45 var obj = this._obj; | 76 var obj = this._obj; |
46 var mtx, shift; | 77 var mtx, shift; |
47 | 78 |
79 percent = (Date.now() - this._start_tm) / this._duration; | |
80 if(percent >= 1) { | |
81 percent = 1; | |
82 obj.timer.stop(); | |
83 } | |
48 | 84 |
49 ang = percent * this._ang; | 85 ang = percent * this._ang; |
50 sv = Math.sin(ang); | 86 sv = Math.sin(ang); |
51 cv = Math.cos(ang); | 87 cv = Math.cos(ang); |
52 mtx = [cv, -sv, 0, sv, cv, 0]; | 88 mtx = [cv, -sv, 0, sv, cv, 0]; |
53 sys.puts('x='+obj.center.x+',y='+obj.center.y); | 89 |
54 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y]; | 90 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y]; |
55 mtx = multiply(mtx, shift); | 91 mtx = multiply(mtx, shift); |
56 shift = [1, 0, obj.center.x, 0, 1, obj.center.y]; | 92 shift = [1, 0, obj.center.x, 0, 1, obj.center.y]; |
57 mtx = multiply(shift, mtx); | 93 mtx = multiply(shift, mtx); |
58 mtx = multiply(mtx, this._start_mtx); | 94 mtx = multiply(mtx, this._start_mtx); |
65 obj[5] = mtx[5]; | 101 obj[5] = mtx[5]; |
66 | 102 |
67 this._app.refresh(); | 103 this._app.refresh(); |
68 } | 104 } |
69 | 105 |
106 rotate.prototype.start = rotate_start; | |
70 rotate.prototype.draw = rotate_draw; | 107 rotate.prototype.draw = rotate_draw; |
71 exports.rotate = rotate; | 108 exports.rotate = rotate; |
72 | 109 |
73 function multiply(s,d) { | 110 function multiply(s,d) { |
74 var m=[]; | 111 var m=[]; |
80 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; | 117 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; |
81 return m; | 118 return m; |
82 } | 119 } |
83 | 120 |
84 | 121 |
85 function scale_draw(percent) { | 122 function scale_draw() { |
86 if (this.end == 1) return; | 123 if (this.end == 1) return; |
124 var percent = (Date.now() - this.starttime)/this.duration; | |
125 if (percent > 1) percent = 1; | |
87 var sx = (this.targetx-this.startsx)*percent+this.startsx; | 126 var sx = (this.targetx-this.startsx)*percent+this.startsx; |
88 var sy = (this.targety-this.startsy)*percent+this.startsy; | 127 var sy = (this.targety-this.startsy)*percent+this.startsy; |
89 var t=[sx,0,0,0,sy,0]; | 128 var t=[sx,0,0,0,sy,0]; |
90 this.obj[0] = sx; | 129 this.obj[0] = sx; |
91 this.obj[4] = sy; | 130 this.obj[4] = sy; |
92 this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x; | 131 this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x; |
93 this.obj[5] = this.origin_offset_y - (sy-this.startsy)*this.obj.center.y; | 132 this.obj[5] = this.origin_offset_y - (sy-this.startsy)*this.obj.center.y; |
94 sys.puts('x='+this.obj.center.x+',y='+this.obj.center.y); | 133 |
95 | 134 this.app.refresh(); |
96 this.app.refresh(); | 135 var self = this; |
97 var self = this; | 136 if (percent < 1) { |
137 this.obj.timer=setTimeout(function() { self.draw();}, frame_interval); | |
138 return; | |
139 } | |
98 this.app.refresh(); | 140 this.app.refresh(); |
99 this.obj.animated_scale = null; | 141 this.obj.animated_scale = null; |
100 } | 142 } |
101 | 143 |
102 function scale(app,obj,targetx,targety, duration) { | 144 function scale(app,obj,targetx,targety, duration) { |
127 this.final_offset_y = this.origin_offset_y-(targety-this.startsy)*obj.center.y; | 169 this.final_offset_y = this.origin_offset_y-(targety-this.startsy)*obj.center.y; |
128 } | 170 } |
129 | 171 |
130 | 172 |
131 exports.scale = scale; | 173 exports.scale = scale; |
174 scale.prototype.start = scale_draw; | |
132 scale.prototype.draw = scale_draw; | 175 scale.prototype.draw = scale_draw; |
133 | 176 |
134 function holder(app, coord) { | 177 function holder(app, coord) { |
135 var mtx = [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]]; | 178 var mtx = [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]]; |
136 | 179 |
167 | 210 |
168 exports.holder = holder; | 211 exports.holder = holder; |
169 | 212 |
170 | 213 |
171 | 214 |
172 function alpha_draw(percent) { | 215 function alpha_draw() { |
173 | 216 |
174 if (this.end == 1) return; | 217 if (this.end == 1) return; |
218 var percent = (Date.now() - this.starttime)/this.duration; | |
219 if (percent > 1) percent = 1; | |
175 var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha; | 220 var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha; |
176 this.obj.opacity=sx; | 221 this.obj.opacity=sx; |
222 | |
223 this.app.refresh(); | |
224 var self = this; | |
225 if (percent < 1) { | |
226 this.obj.timer=setTimeout(function() { self.draw();}, frame_interval); | |
227 return; | |
228 } | |
177 this.app.refresh(); | 229 this.app.refresh(); |
178 this.obj.animated_alpha = null; | 230 this.obj.animated_alpha = null; |
179 } | 231 } |
180 | 232 |
181 function alpha(app,obj,alpha, duration) { | 233 function alpha(app,obj,alpha, duration) { |
194 this.startalpha = obj.opacity; | 246 this.startalpha = obj.opacity; |
195 this.targetalpha = alpha; | 247 this.targetalpha = alpha; |
196 this.duration = duration*1000; | 248 this.duration = duration*1000; |
197 } | 249 } |
198 | 250 |
251 alpha.prototype.start = alpha_draw; | |
199 alpha.prototype.draw = alpha_draw; | 252 alpha.prototype.draw = alpha_draw; |
200 exports.alpha = alpha; | 253 exports.alpha = alpha; |
201 | |
202 function linear_update() | |
203 { | |
204 var now = Date.now(); | |
205 sys.puts("real time is "+now); | |
206 sys.puts("end is "+this.end); | |
207 if (now >= this.end) { | |
208 this.timer.stop(); | |
209 now = this.end; | |
210 } | |
211 if (now < this.startmove) return; | |
212 sys.puts("now is "+now+" offset is "+(now-this.startmove)); | |
213 var per = (now-this.startmove)/this.duration/1000; | |
214 if (per > 1) per = 1; | |
215 this.action.draw(per); | |
216 } | |
217 | |
218 function linear_start() | |
219 { | |
220 var self = this; | |
221 if (this.timer) | |
222 this.timer.stop(); | |
223 this.timer = setInterval(function() { self.update();}, frame_interval); | |
224 this.startmove = Date.now()+this.starttime*1000; | |
225 this.end = this.startmove+this.duration*1000; | |
226 } | |
227 function linear_stop() | |
228 { | |
229 if (this.timer) { | |
230 this.timer.stop(); | |
231 this.timer = null; | |
232 } | |
233 } | |
234 | |
235 function linear_finish() | |
236 { | |
237 this.action.draw(1); | |
238 } | |
239 function linear(action,start, duration) | |
240 { | |
241 this.action = action; | |
242 this.duration = duration; | |
243 this.starttime = start; | |
244 this.timer=null; | |
245 } | |
246 linear.prototype.update = linear_update; | |
247 linear.prototype.start = linear_start; | |
248 linear.prototype.stop = linear_stop; | |
249 linear.prototype.finish = linear_finish; | |
250 exports.linear = linear; | |
251 | |
252 | |
253 function program(words) | |
254 { | |
255 this.words = wrods; | |
256 } | |
257 | |
258 program.prototype.start=function() { | |
259 for(w in this.words) { | |
260 w.start(); | |
261 } | |
262 } | |
263 | |
264 program.prototype.stop=function(s) { | |
265 for(w in this.words) { | |
266 w.stop(); | |
267 } | |
268 } | |
269 program.prototype.finish=function() { | |
270 for(w in this.words) { | |
271 w.finish(); | |
272 } | |
273 } | |
274 | |
275 exports.run = function(actions,start,duration) { | |
276 for(a in actions) { | |
277 var li = new linear(actions[a],start,duration); | |
278 sys.puts(li); | |
279 li.start(); | |
280 } | |
281 } | |
282 exports.runexp=function(actions,start,exp) { | |
283 } |