changeset 857:ea1e88c40548 abs_n_rel_center

Make scale work on center of an object
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 20 Sep 2010 22:43:44 +0800
parents 88f4916a0691
children ed49dc0a26f1
files nodejs/animate.js nodejs/svg.js nodejs/testdesktop.js
diffstat 3 files changed, 35 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/animate.js	Mon Sep 20 22:43:43 2010 +0800
+++ b/nodejs/animate.js	Mon Sep 20 22:43:44 2010 +0800
@@ -128,22 +128,33 @@
     if (this.end == 1) return;
     var percent = (Date.now() - this.starttime)/this.duration;
     if (percent > 1) percent = 1;
-    var sx = (this.targetx-this.startsx)*percent+this.startsx;
-    var sy = (this.targety-this.startsy)*percent+this.startsy;
-    var t=[sx,0,0,0,sy,0];
-    this.obj[0] = sx;
-    this.obj[4] = sy;
-    this.obj[2] = this.origin_offset_x - (sx-this.startsx)*this.obj.center.x;
-    this.obj[5] = this.origin_offset_y - (sy-this.startsy)*this.obj.center.y;
+    var sx = 1 + (this.targetx - 1) * percent;
+    var sy = 1 + (this.targety - 1) * percent;
+    var sh1 = [1, 0, -this.center_x, 0, 1, -this.center_y];
+    var sh2 = [1, 0, this.center_x, 0, 1, this.center_y];
+    var scale=[sx, 0, 0, 0, sy, 0];
+    var obj = this.obj;
+    var mtx;
+
+    mtx = multiply(scale, sh1);
+    mtx = multiply(sh2, mtx);
+    mtx = multiply(this.orig_mtx, mtx);
+    sys.puts([obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]]);
+    obj[0] = mtx[0];
+    obj[1] = mtx[1];
+    obj[2] = mtx[2];
+    obj[3] = mtx[3];
+    obj[4] = mtx[4];
+    obj[5] = mtx[5];
 
     this.app.refresh();
     var self = this;
     if (percent < 1) {
-	this.obj.timer=setTimeout(function() { self.draw();}, frame_interval);
+	obj.timer=setTimeout(function() { self.draw();}, frame_interval);
 	return;
     }
     this.app.refresh();
-    this.obj.animated_scale = null;
+    obj.animated_scale = null;
 }
 
 function scale(app,obj,targetx,targety, duration) {
@@ -168,10 +179,9 @@
     this.targetx = targetx;
     this.targety = targety;
     this.duration = duration*1000;
-    this.origin_offset_x = obj[2];
-    this.origin_offset_y = obj[5];
-    this.final_offset_x = this.origin_offset_x-(targetx-this.startsx)*obj.center.x;
-    this.final_offset_y = this.origin_offset_y-(targety-this.startsy)*obj.center.y;
+    this.center_x = obj.center.rel.x;
+    this.center_y = obj.center.rel.y;
+    this.orig_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]];
 }
 
 
--- a/nodejs/svg.js	Mon Sep 20 22:43:43 2010 +0800
+++ b/nodejs/svg.js	Mon Sep 20 22:43:44 2010 +0800
@@ -501,6 +501,16 @@
     /*! \brief Prevent user to modify value.
      */
     get y() { return this._y; },
+
+    get rel() {
+	var rev;
+	var xy;
+	
+	rev = this._get_ac_rev();
+	xy = _pnt_transform(this._svg_saved_x, this._svg_saved_y, rev);
+
+	return {x: xy[0], y: xy[1]};
+    },
 };
 
 loadSVG.prototype._set_bbox = function(node, tgt) {
--- a/nodejs/testdesktop.js	Mon Sep 20 22:43:43 2010 +0800
+++ b/nodejs/testdesktop.js	Mon Sep 20 22:43:44 2010 +0800
@@ -36,7 +36,7 @@
 	return;
     }
     var target = items[item];
-    var an = new animate.scale(app,old,1,1,0.1);
+    var an = new animate.scale(app,old,1,1/1.5,0.1);
     an.start();
     an = new animate.scale(app,target,1,1.5,0.3);
     an.start();
@@ -50,7 +50,7 @@
 	return;
     }
     var target = items[item];
-    var an = new animate.scale(app,old,1,1,0.1);
+    var an = new animate.scale(app,old,1,1/1.5,0.1);
     an.start();
     an = new animate.scale(app,target,1,1.5,0.3);
     an.start();