Mercurial > MadButterfly
annotate nodejs/animate.js @ 795:46a4cd4d382b
Remove dependent on system time to gain frame rate
Date.now() would read system time. It is ineffeciency in for some
implementation. To gain frame rate, we should rely on accuration of
setInterval().
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 31 Aug 2010 10:04:15 +0800 |
parents | a27606be2cab |
children | 210e4d24a3ba |
rev | line source |
---|---|
794 | 1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
2 // vim: sw=4:ts=8:sts=4:ai | |
727 | 3 var sys=require("sys"); |
4 | |
795
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
5 /* |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
6 * This is configuration for animate module. For slower or speeder |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
7 * machines, ffs can be decreased or increased respective. |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
8 */ |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
9 var ffs = 20; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
10 var frame_interval = 1000 / ffs; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
11 |
727 | 12 function linear_draw() { |
795
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
13 var percent; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
14 |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
15 this.percent = this.percent + this.step; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
16 percent = this.percent; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
17 if (percent > 1) percent = 1; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
18 |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
19 this.c++; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
20 if(percent >= 1) { |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
21 this.obj.timer.stop(); |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
22 delete this.obj.timer; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
23 } |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
24 |
728 | 25 this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx; |
26 this.obj[5] = (this.targety-this.startposy)*percent+this.startposy; | |
795
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
27 this.app.refresh(); |
727 | 28 } |
795
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
29 |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
30 function linear_draw_start() { |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
31 var obj = this.obj; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
32 var self = this; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
33 |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
34 if(obj.timer) |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
35 obj.timer.stop(); |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
36 |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
37 this.startposx = obj[2]; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
38 this.startposy = obj[5]; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
39 this.c = 0; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
40 this.step = 1000 / (this.duration * ffs); |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
41 this.percent = 0; |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
42 obj.timer = setInterval(function() { self.draw(); }, frame_interval); |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
43 } |
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
44 |
728 | 45 function linear(app,obj,targetx,targety,duration) { |
794 | 46 obj.animated_linear = this; |
47 this.app = app; | |
48 this.obj = obj; | |
49 this.end = 0; | |
728 | 50 this.targetx = targetx; |
794 | 51 this.targety = targety; |
52 this.duration = duration*1000; | |
727 | 53 } |
54 | |
55 exports.linear = linear; | |
795
46a4cd4d382b
Remove dependent on system time to gain frame rate
Thinker K.F. Li <thinker@codemud.net>
parents:
794
diff
changeset
|
56 linear.prototype.start = linear_draw_start; |
727 | 57 linear.prototype.draw = linear_draw; |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
58 |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
59 function multiply(s,d) { |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
60 var m=[]; |
794 | 61 m[0] = s[0]*d[0]+s[1]*d[3]; |
62 m[1] = s[0]*d[1]+s[1]*d[4]; | |
63 m[2] = s[0]*d[2]+s[1]*d[5]+s[2]; | |
64 m[3] = s[3]*d[0]+s[4]*d[3]; | |
65 m[4] = s[3]*d[1]+s[4]*d[4]; | |
66 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; | |
67 s[0] = m[0]; | |
68 s[1] = m[1]; | |
69 s[2] = m[2]; | |
70 s[3] = m[3]; | |
71 s[4] = m[4]; | |
72 s[5] = m[5]; | |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
73 } |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
74 |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
75 |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
76 function scale_draw() { |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
77 if (this.end == 1) return; |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
78 var percent = (Date.now() - this.starttime)/this.duration; |
794 | 79 if (percent > 1) percent = 1; |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
80 var sx = (this.targetx-this.startsx)*percent+this.startsx; |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
81 var sy = (this.targety-this.startsy)*percent+this.startsy; |
794 | 82 var t=[sx,0,0,0,sy,0]; |
83 this.obj[0] = sx; | |
84 this.obj[4] = sy; | |
85 this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x; | |
86 this.obj[5] = this.origin_offset_y - (sy-this.startsy)*this.obj.center.y; | |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
87 |
794 | 88 this.app.refresh(); |
89 var self = this; | |
90 if (percent < 1) { | |
91 this.obj.timer=setTimeout(function() { self.draw();}, 20); | |
92 return; | |
93 } | |
94 this.app.refresh(); | |
95 this.obj.animated_scale = null; | |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
96 } |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
97 |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
98 function scale(app,obj,targetx,targety, duration) { |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
99 try { |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
100 if (obj.animated_scale) { |
794 | 101 //obj[0] = obj.animated_scale.targetx; |
102 //obj[4] = obj.animated_scale.targety; | |
103 //obj[2] = obj.animated_scale.final_offset_x; | |
104 //obj[5] = obj.aninated_scale.final_offset_y; | |
105 obj.animated_scale.end = 1; | |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
106 } |
794 | 107 } catch(e) { |
108 | |
109 } | |
110 obj.animated_scale = this; | |
111 this.app = app; | |
112 this.obj = obj; | |
113 this.end = 0; | |
114 this.starttime = Date.now(); | |
115 this.startsx = obj[0]; | |
116 this.startsy = obj[4]; | |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
117 this.targetx = targetx; |
794 | 118 this.targety = targety; |
119 this.duration = duration*1000; | |
120 this.origin_offset_x = obj[2]; | |
121 this.origin_offset_y = obj[5]; | |
122 this.final_offset_x = this.origin_offset_x-(targetx-this.startsx)*obj.center.x; | |
123 this.final_offset_y = this.origin_offset_y-(targety-this.startsy)*obj.center.y; | |
758
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
124 } |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
125 |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
126 |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
127 exports.scale = scale; |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
128 scale.prototype.start = scale_draw; |
d11b0900f03c
Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents:
728
diff
changeset
|
129 scale.prototype.draw = scale_draw; |
788
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
130 |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
131 function holder(app, coord) { |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
132 var mtx = [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]]; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
133 |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
134 this._mtx = mtx; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
135 this._coord = coord; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
136 this._app = app; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
137 } |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
138 |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
139 holder.prototype = { |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
140 go_center: function(o) { |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
141 var sx, sy; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
142 |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
143 sx = o.center_x - this._coord.center_x; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
144 sy = o.center_y - this._coord.center_y; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
145 this.shift(sx, sy); |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
146 }, |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
147 |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
148 home: function() { |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
149 this._coord[2] = this._mtx[2]; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
150 this._coord[5] = this._mtx[5]; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
151 this._app.refresh(); |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
152 }, |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
153 |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
154 shift: function(sx, sy) { |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
155 this._coord[2] = this._mtx[2] + sx; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
156 this._coord[5] = this._mtx[5] + sy; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
157 this._app.refresh(); |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
158 } |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
159 }; |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
160 |
7ec13634c97d
Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents:
776
diff
changeset
|
161 exports.holder = holder; |