changeset 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 0b1764cd53b0
files nodejs/animate.js nodejs/phone.js
diffstat 2 files changed, 38 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- 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) {
--- 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();