Mercurial > MadButterfly
comparison nodejs/animate.js @ 894:460b2629be30
Commit the change again
author | wycc |
---|---|
date | Mon, 27 Sep 2010 23:02:45 +0800 |
parents | cad38ddb1253 |
children | 3136db0ac01b |
comparison
equal
deleted
inserted
replaced
893:cad38ddb1253 | 894:460b2629be30 |
---|---|
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; | 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 } | |
23 this.obj.x = (this.targetx-this.startposx)*percent+this.startposx; | 14 this.obj.x = (this.targetx-this.startposx)*percent+this.startposx; |
24 this.obj.y = (this.targety-this.startposy)*percent+this.startposy; | 15 this.obj.y = (this.targety-this.startposy)*percent+this.startposy; |
25 this.app.refresh(); | 16 this.app.refresh(); |
26 } | 17 } |
27 | 18 |
28 function linear_draw_start() { | 19 function shift(app,obj,shiftx,shifty) { |
29 var obj = this.obj; | 20 obj.animated_shift = this; |
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; | |
41 this.app = app; | 21 this.app = app; |
42 this.obj = obj; | 22 this.obj = obj; |
43 this.end = 0; | 23 this.end = 0; |
44 this.targetx = shiftx + obj.x; | 24 this.targetx = shiftx + obj.x; |
45 this.targety = shifty + obj.y; | 25 this.targety = shifty + obj.y; |
46 this.startposx = obj.x; | 26 this.startposx = obj.x; |
47 this.startposy = obj.y; | 27 this.startposy = obj.y; |
48 this.duration = duration*1000; | 28 } |
49 } | 29 |
50 | 30 exports.shift = shift; |
51 exports.linear = linear; | 31 shift.prototype.draw = shift_draw; |
52 linear.prototype.start = linear_draw_start; | |
53 linear.prototype.draw = linear_draw; | |
54 | 32 |
55 /* ------------------------------------------------------------ */ | 33 /* ------------------------------------------------------------ */ |
56 function rotate(app, obj, ang, duration) { | 34 function rotate(app, obj, ang, duration) { |
57 this._app = app; | 35 this._app = app; |
58 this._obj = obj; | 36 this._obj = obj; |
59 this._ang = ang; | 37 this._ang = ang; |
60 this._duration = duration * 1000; | |
61 } | |
62 | |
63 function rotate_start() { | |
64 var obj = this._obj; | |
65 var self = this; | |
66 | |
67 this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; | 38 this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; |
68 this._start_tm = Date.now(); | 39 } |
69 obj.timer = setInterval(function() { self.draw(); }, frame_interval); | 40 |
70 } | 41 function rotate_draw(percent) { |
71 | |
72 function rotate_draw() { | |
73 var percent; | 42 var percent; |
74 var ang; | 43 var ang; |
75 var sv, cv; | 44 var sv, cv; |
76 var obj = this._obj; | 45 var obj = this._obj; |
77 var mtx, shift; | 46 var mtx, shift; |
78 | 47 |
79 percent = (Date.now() - this._start_tm) / this._duration; | |
80 if(percent >= 1) { | |
81 percent = 1; | |
82 obj.timer.stop(); | |
83 } | |
84 | 48 |
85 ang = percent * this._ang; | 49 ang = percent * this._ang; |
86 sv = Math.sin(ang); | 50 sv = Math.sin(ang); |
87 cv = Math.cos(ang); | 51 cv = Math.cos(ang); |
88 mtx = [cv, -sv, 0, sv, cv, 0]; | 52 mtx = [cv, -sv, 0, sv, cv, 0]; |
89 | 53 sys.puts('x='+obj.center.x+',y='+obj.center.y); |
90 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y]; | 54 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y]; |
91 mtx = multiply(mtx, shift); | 55 mtx = multiply(mtx, shift); |
92 shift = [1, 0, obj.center.x, 0, 1, obj.center.y]; | 56 shift = [1, 0, obj.center.x, 0, 1, obj.center.y]; |
93 mtx = multiply(shift, mtx); | 57 mtx = multiply(shift, mtx); |
94 mtx = multiply(mtx, this._start_mtx); | 58 mtx = multiply(mtx, this._start_mtx); |
101 obj[5] = mtx[5]; | 65 obj[5] = mtx[5]; |
102 | 66 |
103 this._app.refresh(); | 67 this._app.refresh(); |
104 } | 68 } |
105 | 69 |
106 rotate.prototype.start = rotate_start; | |
107 rotate.prototype.draw = rotate_draw; | 70 rotate.prototype.draw = rotate_draw; |
108 exports.rotate = rotate; | 71 exports.rotate = rotate; |
109 | 72 |
110 function multiply(s,d) { | 73 function multiply(s,d) { |
111 var m=[]; | 74 var m=[]; |
117 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; | 80 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; |
118 return m; | 81 return m; |
119 } | 82 } |
120 | 83 |
121 | 84 |
122 function scale_draw() { | 85 function scale_draw(percent) { |
123 if (this.end == 1) return; | 86 if (this.end == 1) return; |
124 var percent = (Date.now() - this.starttime)/this.duration; | |
125 if (percent > 1) percent = 1; | |
126 var sx = (this.targetx-this.startsx)*percent+this.startsx; | 87 var sx = (this.targetx-this.startsx)*percent+this.startsx; |
127 var sy = (this.targety-this.startsy)*percent+this.startsy; | 88 var sy = (this.targety-this.startsy)*percent+this.startsy; |
128 var t=[sx,0,0,0,sy,0]; | 89 var t=[sx,0,0,0,sy,0]; |
129 this.obj[0] = sx; | 90 this.obj[0] = sx; |
130 this.obj[4] = sy; | 91 this.obj[4] = sy; |
131 this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x; | 92 this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x; |
132 this.obj[5] = this.origin_offset_y - (sy-this.startsy)*this.obj.center.y; | 93 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 } | |
140 this.app.refresh(); | 98 this.app.refresh(); |
141 this.obj.animated_scale = null; | 99 this.obj.animated_scale = null; |
142 } | 100 } |
143 | 101 |
144 function scale(app,obj,targetx,targety, duration) { | 102 function scale(app,obj,targetx,targety, duration) { |
169 this.final_offset_y = this.origin_offset_y-(targety-this.startsy)*obj.center.y; | 127 this.final_offset_y = this.origin_offset_y-(targety-this.startsy)*obj.center.y; |
170 } | 128 } |
171 | 129 |
172 | 130 |
173 exports.scale = scale; | 131 exports.scale = scale; |
174 scale.prototype.start = scale_draw; | |
175 scale.prototype.draw = scale_draw; | 132 scale.prototype.draw = scale_draw; |
176 | 133 |
177 function holder(app, coord) { | 134 function holder(app, coord) { |
178 var mtx = [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]]; | 135 var mtx = [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]]; |
179 | 136 |
210 | 167 |
211 exports.holder = holder; | 168 exports.holder = holder; |
212 | 169 |
213 | 170 |
214 | 171 |
215 function alpha_draw() { | 172 function alpha_draw(percent) { |
216 | 173 |
217 if (this.end == 1) return; | 174 if (this.end == 1) return; |
218 var percent = (Date.now() - this.starttime)/this.duration; | |
219 if (percent > 1) percent = 1; | |
220 var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha; | 175 var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha; |
221 this.obj.opacity=sx; | 176 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 } | |
229 this.app.refresh(); | 177 this.app.refresh(); |
230 this.obj.animated_alpha = null; | 178 this.obj.animated_alpha = null; |
231 } | 179 } |
232 | 180 |
233 function alpha(app,obj,alpha, duration) { | 181 function alpha(app,obj,alpha, duration) { |
246 this.startalpha = obj.opacity; | 194 this.startalpha = obj.opacity; |
247 this.targetalpha = alpha; | 195 this.targetalpha = alpha; |
248 this.duration = duration*1000; | 196 this.duration = duration*1000; |
249 } | 197 } |
250 | 198 |
251 alpha.prototype.start = alpha_draw; | |
252 alpha.prototype.draw = alpha_draw; | 199 alpha.prototype.draw = alpha_draw; |
253 exports.alpha = alpha; | 200 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 } |