Mercurial > MadButterfly
comparison nodejs/animate.js @ 906:62ef69e02e85
branch merge
author | Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com> |
---|---|
date | Sun, 03 Oct 2010 18:15:00 +0800 |
parents | f4fd7a16361d |
children | 55767e444436 |
comparison
equal
deleted
inserted
replaced
905:e3a5e05f00c1 | 906:62ef69e02e85 |
---|---|
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 x, y; |
14 | 14 |
15 percent = (Date.now() - this._start_tm) / this.duration; | 15 x = (this.targetx - this.startposx) * percent + this.startposx; |
16 if(percent >= 1) { | 16 y = (this.targety - this.startposy) * percent + this.startposy; |
17 percent = 1; | 17 this.obj.center.move(x, y); |
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; | |
24 this.obj.y = (this.targety-this.startposy)*percent+this.startposy; | |
25 this.app.refresh(); | 18 this.app.refresh(); |
26 } | 19 } |
27 | 20 |
28 function linear_draw_start() { | 21 function shift(app,obj,shiftx,shifty) { |
29 var obj = this.obj; | 22 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; | 23 this.app = app; |
42 this.obj = obj; | 24 this.obj = obj; |
43 this.end = 0; | 25 this.end = 0; |
44 this.targetx = shiftx + obj.x; | 26 this.targetx = shiftx + obj.center.x; |
45 this.targety = shifty + obj.y; | 27 this.targety = shifty + obj.center.y; |
46 this.startposx = obj.x; | 28 this.startposx = obj.center.x; |
47 this.startposy = obj.y; | 29 this.startposy = obj.center.y; |
48 this.duration = duration*1000; | 30 } |
49 } | 31 |
50 | 32 exports.shift = shift; |
51 exports.linear = linear; | 33 shift.prototype.draw = shift_draw; |
52 linear.prototype.start = linear_draw_start; | |
53 linear.prototype.draw = linear_draw; | |
54 | 34 |
55 /* ------------------------------------------------------------ */ | 35 /* ------------------------------------------------------------ */ |
56 function rotate(app, obj, ang, duration) { | 36 function rotate(app, obj, ang, duration) { |
57 this._app = app; | 37 this._app = app; |
58 this._obj = obj; | 38 this._obj = obj; |
59 this._ang = ang; | 39 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]]; | 40 this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; |
68 this._start_tm = Date.now(); | 41 } |
69 obj.timer = setInterval(function() { self.draw(); }, frame_interval); | 42 |
70 } | 43 function rotate_draw(percent) { |
71 | |
72 function rotate_draw() { | |
73 var percent; | 44 var percent; |
74 var ang; | 45 var ang; |
75 var sv, cv; | 46 var sv, cv; |
76 var obj = this._obj; | 47 var obj = this._obj; |
77 var mtx, shift; | 48 var mtx, shift; |
78 | 49 |
79 percent = (Date.now() - this._start_tm) / this._duration; | |
80 if(percent >= 1) { | |
81 percent = 1; | |
82 obj.timer.stop(); | |
83 } | |
84 | 50 |
85 ang = percent * this._ang; | 51 ang = percent * this._ang; |
86 sv = Math.sin(ang); | 52 sv = Math.sin(ang); |
87 cv = Math.cos(ang); | 53 cv = Math.cos(ang); |
88 mtx = [cv, -sv, 0, sv, cv, 0]; | 54 mtx = [cv, -sv, 0, sv, cv, 0]; |
89 | 55 sys.puts('x='+obj.center.x+',y='+obj.center.y); |
90 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y]; | 56 shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y]; |
91 mtx = multiply(mtx, shift); | 57 mtx = multiply(mtx, shift); |
92 shift = [1, 0, obj.center.x, 0, 1, obj.center.y]; | 58 shift = [1, 0, obj.center.x, 0, 1, obj.center.y]; |
93 mtx = multiply(shift, mtx); | 59 mtx = multiply(shift, mtx); |
94 mtx = multiply(mtx, this._start_mtx); | 60 mtx = multiply(mtx, this._start_mtx); |
101 obj[5] = mtx[5]; | 67 obj[5] = mtx[5]; |
102 | 68 |
103 this._app.refresh(); | 69 this._app.refresh(); |
104 } | 70 } |
105 | 71 |
106 rotate.prototype.start = rotate_start; | |
107 rotate.prototype.draw = rotate_draw; | 72 rotate.prototype.draw = rotate_draw; |
108 exports.rotate = rotate; | 73 exports.rotate = rotate; |
109 | 74 |
110 function multiply(s,d) { | 75 function multiply(s,d) { |
111 var m=[]; | 76 var m=[]; |
117 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]; |
118 return m; | 83 return m; |
119 } | 84 } |
120 | 85 |
121 | 86 |
122 function scale_draw() { | 87 function scale_draw(percent) { |
123 if (this.end == 1) return; | 88 var sx = 1 + (this.totalsx - 1) * percent; |
124 var percent = (Date.now() - this.starttime)/this.duration; | 89 var sy = 1 + (this.totalsy - 1) * percent; |
125 if (percent > 1) percent = 1; | 90 var sh1 = [1, 0, -this.center_x, 0, 1, -this.center_y]; |
126 var sx = (this.targetx-this.startsx)*percent+this.startsx; | 91 var sh2 = [1, 0, this.center_x, 0, 1, this.center_y]; |
127 var sy = (this.targety-this.startsy)*percent+this.startsy; | 92 var scale = [sx, 0, 0, 0, sy, 0]; |
128 var t=[sx,0,0,0,sy,0]; | 93 var obj = this.obj; |
129 this.obj[0] = sx; | 94 var mtx; |
130 this.obj[4] = sy; | 95 |
131 this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x; | 96 mtx = multiply(scale, sh1); |
132 this.obj[5] = this.origin_offset_y - (sy-this.startsy)*this.obj.center.y; | 97 mtx = multiply(sh2, mtx); |
98 mtx = multiply(this.orig_mtx, mtx); | |
99 obj[0] = mtx[0]; | |
100 obj[1] = mtx[1]; | |
101 obj[2] = mtx[2]; | |
102 obj[3] = mtx[3]; | |
103 obj[4] = mtx[4]; | |
104 obj[5] = mtx[5]; | |
133 | 105 |
134 this.app.refresh(); | 106 this.app.refresh(); |
135 var self = this; | 107 } |
136 if (percent < 1) { | 108 |
137 this.obj.timer=setTimeout(function() { self.draw();}, frame_interval); | 109 function scale(app, obj, fact_x, fact_y, duration) { |
138 return; | 110 var bbox; |
139 } | 111 |
140 this.app.refresh(); | |
141 this.obj.animated_scale = null; | |
142 } | |
143 | |
144 function scale(app,obj,targetx,targety, duration) { | |
145 try { | 112 try { |
146 if (obj.animated_scale) { | 113 if (obj.animated_scale) { |
147 //obj[0] = obj.animated_scale.targetx; | |
148 //obj[4] = obj.animated_scale.targety; | |
149 //obj[2] = obj.animated_scale.final_offset_x; | |
150 //obj[5] = obj.aninated_scale.final_offset_y; | |
151 obj.animated_scale.end = 1; | 114 obj.animated_scale.end = 1; |
152 } | 115 } |
153 } catch(e) { | 116 } catch(e) { |
154 | 117 |
155 } | 118 } |
119 | |
120 bbox = obj.bbox; | |
121 bbox.update(); | |
156 obj.animated_scale = this; | 122 obj.animated_scale = this; |
157 this.app = app; | 123 this.app = app; |
158 this.obj = obj; | 124 this.obj = obj; |
159 this.end = 0; | 125 this.end = 0; |
160 this.starttime = Date.now(); | 126 this.starttime = Date.now(); |
161 this.startsx = obj[0]; | 127 this.totalsx = fact_x * bbox.orig.width / bbox.width; |
162 this.startsy = obj[4]; | 128 this.totalsy = fact_y * bbox.orig.height / bbox.height; |
163 this.targetx = targetx; | |
164 this.targety = targety; | |
165 this.duration = duration*1000; | 129 this.duration = duration*1000; |
166 this.origin_offset_x = obj[2]; | 130 this.center_x = obj.center.rel.x; |
167 this.origin_offset_y = obj[5]; | 131 this.center_y = obj.center.rel.y; |
168 this.final_offset_x = this.origin_offset_x-(targetx-this.startsx)*obj.center.x; | 132 this.orig_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]; |
169 this.final_offset_y = this.origin_offset_y-(targety-this.startsy)*obj.center.y; | |
170 } | 133 } |
171 | 134 |
172 | 135 |
173 exports.scale = scale; | 136 exports.scale = scale; |
174 scale.prototype.start = scale_draw; | |
175 scale.prototype.draw = scale_draw; | 137 scale.prototype.draw = scale_draw; |
176 | 138 |
177 function holder(app, coord) { | 139 function holder(app, coord) { |
178 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]]; |
179 | 141 |
210 | 172 |
211 exports.holder = holder; | 173 exports.holder = holder; |
212 | 174 |
213 | 175 |
214 | 176 |
215 function alpha_draw() { | 177 function alpha_draw(percent) { |
216 | 178 |
217 if (this.end == 1) return; | 179 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; | 180 var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha; |
221 this.obj.opacity=sx; | 181 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(); | 182 this.app.refresh(); |
230 this.obj.animated_alpha = null; | 183 this.obj.animated_alpha = null; |
231 } | 184 } |
232 | 185 |
233 function alpha(app,obj,alpha, duration) { | 186 function alpha(app,obj,alpha, duration) { |
246 this.startalpha = obj.opacity; | 199 this.startalpha = obj.opacity; |
247 this.targetalpha = alpha; | 200 this.targetalpha = alpha; |
248 this.duration = duration*1000; | 201 this.duration = duration*1000; |
249 } | 202 } |
250 | 203 |
251 alpha.prototype.start = alpha_draw; | |
252 alpha.prototype.draw = alpha_draw; | 204 alpha.prototype.draw = alpha_draw; |
253 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 /* Following functions is not defined completed. | |
261 Mark it out to prevent error message. | |
262 | |
263 function multilinear(action,start,stages) { | |
264 sys.puts("Multilinear word is not implemented yet"); | |
265 } | |
266 | |
267 exports.multilinear = multilinear; | |
268 multilinear.prototype.update = multilinear_update; | |
269 multilinear.prototype.start = multilinear_start; | |
270 multilinear.prototype.stop = multilinear_stop; | |
271 multilinear.prototype.finish = multilinear_finish; | |
272 | |
273 function exponential(action,start,stages) { | |
274 sys.puts("exponential word is not implemented yet"); | |
275 } | |
276 | |
277 exports.exponential = exponential; | |
278 exponential.prototype.update = exponential_update; | |
279 exponential.prototype.start = exponential_start; | |
280 exponential.prototype.stop = exponential_stop; | |
281 exponential.prototype.finish = exponential_finish; | |
282 | |
283 */ | |
284 | |
285 function program(words) | |
286 { | |
287 this.words = wrods; | |
288 } | |
289 | |
290 program.prototype.start=function() { | |
291 for(w in this.words) { | |
292 w.start(); | |
293 } | |
294 } | |
295 | |
296 program.prototype.step=function(s) { | |
297 sys.puts("This function is not implemented yet"); | |
298 } | |
299 program.prototype.stop=function() { | |
300 for(w in this.words) { | |
301 w.stop(); | |
302 } | |
303 } | |
304 program.prototype.finish=function() { | |
305 for(w in this.words) { | |
306 w.finish(); | |
307 } | |
308 } | |
309 | |
310 exports.run = function(actions,start,duration) { | |
311 for(a in actions) { | |
312 var li = new linear(actions[a],start,duration); | |
313 sys.puts(li); | |
314 li.start(); | |
315 } | |
316 } | |
317 exports.runexp=function(actions,start,exp) { | |
318 sys.puts("This function is not implemented yet"); | |
319 } | |
320 |