# HG changeset patch # User Thinker K.F. Li # Date 1283220255 -28800 # Node ID 46a4cd4d382b0d2eced46ce016b46c4f0f93528f # Parent a27606be2cabe9b044053522c4dcf711d3feefee 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(). diff -r a27606be2cab -r 46a4cd4d382b nodejs/animate.js --- a/nodejs/animate.js Tue Aug 31 09:27:16 2010 +0800 +++ b/nodejs/animate.js Tue Aug 31 10:04:15 2010 +0800 @@ -2,45 +2,58 @@ // vim: sw=4:ts=8:sts=4:ai var sys=require("sys"); +/* + * This is configuration for animate module. For slower or speeder + * machines, ffs can be decreased or increased respective. + */ +var ffs = 20; +var frame_interval = 1000 / ffs; + function linear_draw() { - if (this.end == 1) return; - var percent = (Date.now() - this.starttime)/this.duration; - if (percent > 1) percent = 1; + var percent; + + this.percent = this.percent + this.step; + percent = this.percent; + if (percent > 1) percent = 1; + + this.c++; + if(percent >= 1) { + this.obj.timer.stop(); + delete this.obj.timer; + } + this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx; this.obj[5] = (this.targety-this.startposy)*percent+this.startposy; - this.app.refresh(); - var self = this; - if (percent < 1) { - this.obj.timer=setTimeout(function() { self.draw();}, 20); - return; - } - this.app.refresh(); - this.obj.animated_linear = null; + this.app.refresh(); } + +function linear_draw_start() { + var obj = this.obj; + var self = this; + + if(obj.timer) + obj.timer.stop(); + + this.startposx = obj[2]; + this.startposy = obj[5]; + this.c = 0; + this.step = 1000 / (this.duration * ffs); + this.percent = 0; + obj.timer = setInterval(function() { self.draw(); }, frame_interval); +} + function linear(app,obj,targetx,targety,duration) { - try { - if (obj.animated_linear) { - obj[5] = obj.animated_linear.targety; - obj[2] = obj.animated_linear.targetx; - obj.animated_linear.end = 1; - } - } catch(e) { - - } obj.animated_linear = this; this.app = app; this.obj = obj; this.end = 0; - this.starttime = Date.now(); - this.startposx = obj[2]; - this.startposy = obj[5]; this.targetx = targetx; this.targety = targety; this.duration = duration*1000; } exports.linear = linear; -linear.prototype.start = linear_draw; +linear.prototype.start = linear_draw_start; linear.prototype.draw = linear_draw; function multiply(s,d) { diff -r a27606be2cab -r 46a4cd4d382b nodejs/phone.js --- a/nodejs/phone.js Tue Aug 31 09:27:16 2010 +0800 +++ b/nodejs/phone.js Tue Aug 31 10:04:15 2010 +0800 @@ -47,7 +47,7 @@ var sw = 0; var dock_up = new animate.linear(app, dock, 0, -300, 0.5); -var dock_down = new animate.linear(app, dock, 0, 0, 0.5); +var dock_down = new animate.linear(app, dock, 0, 0, 0.2); dock.mouse_event.add_event_observer(4, function(evt) { if(sw == 0) { dock_up.start();