topical media & game development
mobile-graphic-enchant-dev-src-Matrix.js / js
enchant.Matrix = enchant.Class.create({
initialize: function() {
this.reset();
},
reset: function() {
this.stack = [];
this.stack.push([ 1, 0, 0, 1, 0, 0 ]);
},
makeTransformMatrix: function(node, dest) {
var x = node._x;
var y = node._y;
var width = node.width || 0;
var height = node.height || 0;
var rotation = node._rotation || 0;
var scaleX = (typeof node._scaleX === 'number') ? node._scaleX : 1;
var scaleY = (typeof node._scaleY === 'number') ? node._scaleY : 1;
var theta = rotation * Math.PI / 180;
var tmpcos = Math.cos(theta);
var tmpsin = Math.sin(theta);
var w = (typeof node._originX === 'number') ? node._originX : width / 2;
var h = (typeof node._originY === 'number') ? node._originY : height / 2;
var a = scaleX * tmpcos;
var b = scaleX * tmpsin;
var c = scaleY * tmpsin;
var d = scaleY * tmpcos;
dest[0] = a;
dest[1] = b;
dest[2] = -c;
dest[3] = d;
dest[4] = (-a * w + c * h + x + w);
dest[5] = (-b * w - d * h + y + h);
},
multiply: function(m1, m2, dest) {
var a11 = m1[0], a21 = m1[2], adx = m1[4],
a12 = m1[1], a22 = m1[3], ady = m1[5];
var b11 = m2[0], b21 = m2[2], bdx = m2[4],
b12 = m2[1], b22 = m2[3], bdy = m2[5];
dest[0] = a11 * b11 + a21 * b12;
dest[1] = a12 * b11 + a22 * b12;
dest[2] = a11 * b21 + a21 * b22;
dest[3] = a12 * b21 + a22 * b22;
dest[4] = a11 * bdx + a21 * bdy + adx;
dest[5] = a12 * bdx + a22 * bdy + ady;
},
multiplyVec: function(mat, vec, dest) {
var x = vec[0], y = vec[1];
var m11 = mat[0], m21 = mat[2], mdx = mat[4],
m12 = mat[1], m22 = mat[3], mdy = mat[5];
dest[0] = m11 * x + m21 * y + mdx;
dest[1] = m12 * x + m22 * y + mdy;
}
});
enchant.Matrix.instance = new enchant.Matrix();
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.