Mercurial > MadButterfly
comparison 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 |
comparison
equal
deleted
inserted
replaced
794:a27606be2cab | 795:46a4cd4d382b |
---|---|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- | 1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
2 // vim: sw=4:ts=8:sts=4:ai | 2 // vim: sw=4:ts=8:sts=4:ai |
3 var sys=require("sys"); | 3 var sys=require("sys"); |
4 | 4 |
5 /* | |
6 * This is configuration for animate module. For slower or speeder | |
7 * machines, ffs can be decreased or increased respective. | |
8 */ | |
9 var ffs = 20; | |
10 var frame_interval = 1000 / ffs; | |
11 | |
5 function linear_draw() { | 12 function linear_draw() { |
6 if (this.end == 1) return; | 13 var percent; |
7 var percent = (Date.now() - this.starttime)/this.duration; | 14 |
8 if (percent > 1) percent = 1; | 15 this.percent = this.percent + this.step; |
16 percent = this.percent; | |
17 if (percent > 1) percent = 1; | |
18 | |
19 this.c++; | |
20 if(percent >= 1) { | |
21 this.obj.timer.stop(); | |
22 delete this.obj.timer; | |
23 } | |
24 | |
9 this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx; | 25 this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx; |
10 this.obj[5] = (this.targety-this.startposy)*percent+this.startposy; | 26 this.obj[5] = (this.targety-this.startposy)*percent+this.startposy; |
11 this.app.refresh(); | 27 this.app.refresh(); |
12 var self = this; | |
13 if (percent < 1) { | |
14 this.obj.timer=setTimeout(function() { self.draw();}, 20); | |
15 return; | |
16 } | |
17 this.app.refresh(); | |
18 this.obj.animated_linear = null; | |
19 } | 28 } |
29 | |
30 function linear_draw_start() { | |
31 var obj = this.obj; | |
32 var self = this; | |
33 | |
34 if(obj.timer) | |
35 obj.timer.stop(); | |
36 | |
37 this.startposx = obj[2]; | |
38 this.startposy = obj[5]; | |
39 this.c = 0; | |
40 this.step = 1000 / (this.duration * ffs); | |
41 this.percent = 0; | |
42 obj.timer = setInterval(function() { self.draw(); }, frame_interval); | |
43 } | |
44 | |
20 function linear(app,obj,targetx,targety,duration) { | 45 function linear(app,obj,targetx,targety,duration) { |
21 try { | |
22 if (obj.animated_linear) { | |
23 obj[5] = obj.animated_linear.targety; | |
24 obj[2] = obj.animated_linear.targetx; | |
25 obj.animated_linear.end = 1; | |
26 } | |
27 } catch(e) { | |
28 | |
29 } | |
30 obj.animated_linear = this; | 46 obj.animated_linear = this; |
31 this.app = app; | 47 this.app = app; |
32 this.obj = obj; | 48 this.obj = obj; |
33 this.end = 0; | 49 this.end = 0; |
34 this.starttime = Date.now(); | |
35 this.startposx = obj[2]; | |
36 this.startposy = obj[5]; | |
37 this.targetx = targetx; | 50 this.targetx = targetx; |
38 this.targety = targety; | 51 this.targety = targety; |
39 this.duration = duration*1000; | 52 this.duration = duration*1000; |
40 } | 53 } |
41 | 54 |
42 exports.linear = linear; | 55 exports.linear = linear; |
43 linear.prototype.start = linear_draw; | 56 linear.prototype.start = linear_draw_start; |
44 linear.prototype.draw = linear_draw; | 57 linear.prototype.draw = linear_draw; |
45 | 58 |
46 function multiply(s,d) { | 59 function multiply(s,d) { |
47 var m=[]; | 60 var m=[]; |
48 m[0] = s[0]*d[0]+s[1]*d[3]; | 61 m[0] = s[0]*d[0]+s[1]*d[3]; |