Mercurial > MadButterfly
changeset 727:468cd504800c
Rewrite the animation as a module.
author | wycc |
---|---|
date | Tue, 17 Aug 2010 05:15:50 +0800 |
parents | d479f319d7b7 |
children | a843f147c995 |
files | nodejs/animate.js nodejs/testsvg.js |
diffstat | 2 files changed, 50 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nodejs/animate.js Tue Aug 17 05:15:50 2010 +0800 @@ -0,0 +1,39 @@ +var sys=require("sys"); + +function linear_draw() { + if (this.end == 1) return; + var percent = (Date.now() - this.starttime)/this.duration; + if (percent > 1) percent = 1; + this.obj[5] = (this.target-this.startpos)*percent+this.startpos; + 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; +} +function linear(app,obj,target,duration) { + try { + if (obj.animated_linear) { + obj[5] = obj.animated_linear.target; + 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.startpos = obj[5]; + this.target = target; + this.duration = duration*1000; +} + + +exports.linear = linear; +linear.prototype.start = linear_draw; +linear.prototype.draw = linear_draw;
--- a/nodejs/testsvg.js Mon Aug 16 07:34:21 2010 +0800 +++ b/nodejs/testsvg.js Tue Aug 17 05:15:50 2010 +0800 @@ -1,6 +1,7 @@ var svg = require("./svg"); var mbapp = require("./mbapp"); var sys=require("sys"); +var animate=require("./animate"); app = new mbapp.app(); app.loadSVG("test.svg"); @@ -8,44 +9,24 @@ item=1; lightbar[5] = app.get("item"+item)[5]; -function animated(app,obj) { - var d=obj.animated_loc - obj[5]; - var dd=d; - if (dd<0) dd = -dd; - - if (dd > 5) { - if (d > 0) d = 5; else d = -5; - } - - obj[5] += d; - sys.puts(d); - app.refresh() - if (dd > 1) { - setTimeout(function() { animated(app,obj);}, 20); - return; - } - app.animated_end = 1; -} -function animated_start(app,obj,target) { - if (obj.animated_end==0) - obj[5] = obj.animated_loc; - obj.animated_loc = target[5]; - obj.animated_end = 0; - animated(app,obj); -} -lightbar.animated_loc = 1; app.addKeyboardListener(mbapp.EVT_KB_PRESS, function(evt) { if (evt.keycode == mbapp.KEY_UP) { item = item - 1; if (item == 0) item = 1; - else - animated_start(app,lightbar,app.get("item"+item)); + else { + var target = app.get("item"+item); + var an = new animate.linear(app,lightbar,target[5],0.3); + an.start(); + } } else if (evt.keycode == mbapp.KEY_DOWN) { item = item + 1; if (item == 10) { item = 9; - } else - animated_start(app,lightbar,app.get("item"+item)); + } else { + var target = app.get("item"+item); + var an = new animate.linear(app,lightbar,target[5],0.3); + an.start(); + } } }); app.loop();