annotate nodejs/animate.js @ 794:a27606be2cab

Change modeline
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 31 Aug 2010 09:27:16 +0800
parents 7ec13634c97d
children 46a4cd4d382b
rev   line source
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
2 // vim: sw=4:ts=8:sts=4:ai
727
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
3 var sys=require("sys");
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
4
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
5 function linear_draw() {
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
6 if (this.end == 1) return;
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
7 var percent = (Date.now() - this.starttime)/this.duration;
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
8 if (percent > 1) percent = 1;
728
a843f147c995 Add Y coordiante in the linear animation.
wycc
parents: 727
diff changeset
9 this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx;
a843f147c995 Add Y coordiante in the linear animation.
wycc
parents: 727
diff changeset
10 this.obj[5] = (this.targety-this.startposy)*percent+this.startposy;
727
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
11 this.app.refresh();
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
12 var self = this;
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
13 if (percent < 1) {
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
14 this.obj.timer=setTimeout(function() { self.draw();}, 20);
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
15 return;
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
16 }
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
17 this.app.refresh();
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
18 this.obj.animated_linear = null;
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
19 }
728
a843f147c995 Add Y coordiante in the linear animation.
wycc
parents: 727
diff changeset
20 function linear(app,obj,targetx,targety,duration) {
727
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
21 try {
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
22 if (obj.animated_linear) {
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
23 obj[5] = obj.animated_linear.targety;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
24 obj[2] = obj.animated_linear.targetx;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
25 obj.animated_linear.end = 1;
727
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
26 }
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
27 } catch(e) {
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
28
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
29 }
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
30 obj.animated_linear = this;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
31 this.app = app;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
32 this.obj = obj;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
33 this.end = 0;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
34 this.starttime = Date.now();
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
35 this.startposx = obj[2];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
36 this.startposy = obj[5];
728
a843f147c995 Add Y coordiante in the linear animation.
wycc
parents: 727
diff changeset
37 this.targetx = targetx;
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
38 this.targety = targety;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
39 this.duration = duration*1000;
727
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
40 }
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
41
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
42 exports.linear = linear;
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
43 linear.prototype.start = linear_draw;
468cd504800c Rewrite the animation as a module.
wycc
parents:
diff changeset
44 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
45
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
46 function multiply(s,d) {
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
47 var m=[];
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
48 m[0] = s[0]*d[0]+s[1]*d[3];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
49 m[1] = s[0]*d[1]+s[1]*d[4];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
50 m[2] = s[0]*d[2]+s[1]*d[5]+s[2];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
51 m[3] = s[3]*d[0]+s[4]*d[3];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
52 m[4] = s[3]*d[1]+s[4]*d[4];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
53 m[5] = s[3]*d[2]+s[4]*d[5]+s[5];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
54 s[0] = m[0];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
55 s[1] = m[1];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
56 s[2] = m[2];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
57 s[3] = m[3];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
58 s[4] = m[4];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
59 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
60 }
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
61
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
62
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
63 function scale_draw() {
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
64 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
65 var percent = (Date.now() - this.starttime)/this.duration;
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
66 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
67 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
68 var sy = (this.targety-this.startsy)*percent+this.startsy;
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
69 var t=[sx,0,0,0,sy,0];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
70 this.obj[0] = sx;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
71 this.obj[4] = sy;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
72 this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
73 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
74
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
75 this.app.refresh();
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
76 var self = this;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
77 if (percent < 1) {
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
78 this.obj.timer=setTimeout(function() { self.draw();}, 20);
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
79 return;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
80 }
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
81 this.app.refresh();
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
82 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
83 }
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
84
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
85 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
86 try {
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
87 if (obj.animated_scale) {
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
88 //obj[0] = obj.animated_scale.targetx;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
89 //obj[4] = obj.animated_scale.targety;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
90 //obj[2] = obj.animated_scale.final_offset_x;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
91 //obj[5] = obj.aninated_scale.final_offset_y;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
92 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
93 }
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
94 } catch(e) {
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
95
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
96 }
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
97 obj.animated_scale = this;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
98 this.app = app;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
99 this.obj = obj;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
100 this.end = 0;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
101 this.starttime = Date.now();
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
102 this.startsx = obj[0];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
103 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
104 this.targetx = targetx;
794
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
105 this.targety = targety;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
106 this.duration = duration*1000;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
107 this.origin_offset_x = obj[2];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
108 this.origin_offset_y = obj[5];
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
109 this.final_offset_x = this.origin_offset_x-(targetx-this.startsx)*obj.center.x;
a27606be2cab Change modeline
Thinker K.F. Li <thinker@codemud.net>
parents: 788
diff changeset
110 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
111 }
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
112
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
113
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
114 exports.scale = scale;
d11b0900f03c Check in the dynamic menu example. Currently, it illustarte a bug in the renderer.
wycc
parents: 728
diff changeset
115 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
116 scale.prototype.draw = scale_draw;
788
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
117
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
118 function holder(app, coord) {
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
119 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
120
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
121 this._mtx = mtx;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
122 this._coord = coord;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
123 this._app = app;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
124 }
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
125
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
126 holder.prototype = {
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
127 go_center: function(o) {
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
128 var sx, sy;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
129
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
130 sx = o.center_x - this._coord.center_x;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
131 sy = o.center_y - this._coord.center_y;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
132 this.shift(sx, sy);
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
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
135 home: function() {
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
136 this._coord[2] = this._mtx[2];
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
137 this._coord[5] = this._mtx[5];
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
138 this._app.refresh();
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
139 },
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
140
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
141 shift: function(sx, sy) {
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
142 this._coord[2] = this._mtx[2] + sx;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
143 this._coord[5] = this._mtx[5] + sy;
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
144 this._app.refresh();
7ec13634c97d Add holder for animate
Thinker K.F. Li <thinker@codemud.net>
parents: 776
diff changeset
145 }
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 exports.holder = holder;