add copy() and point() methods to Kinetic.Transform; most methods return "this" for chaining

This commit is contained in:
Atis
2014-02-20 22:34:13 +02:00
parent f8ccd574cb
commit 57402f0ca8

View File

@@ -105,23 +105,49 @@
/** /**
* Transform constructor * Transform constructor
* @constructor * @constructor
* @param {Array} Optional six-element matrix
* @memberof Kinetic * @memberof Kinetic
*/ */
Kinetic.Transform = function() { Kinetic.Transform = function(m) {
this.m = [1, 0, 0, 1, 0, 0]; this.m = (m && m.slice()) || [1, 0, 0, 1, 0, 0];
}; };
Kinetic.Transform.prototype = { Kinetic.Transform.prototype = {
/**
* Copy Kinetic.Transform object
* @method
* @memberof Kinetic.Transform.prototype
* @returns {Kinetic.Transform}
*/
copy: function() {
return new Kinetic.Transform(this.m);
},
/**
* Transform point
* @method
* @memberof Kinetic.Transform.prototype
* @param {Object} 2D point(x, y)
* @returns {Object} 2D point(x, y)
*/
point: function(p) {
var m = this.m;
return {
x: m[0] * p.x + m[2] * p.y + m[4],
y: m[1] * p.x + m[3] * p.y + m[5]
};
},
/** /**
* Apply translation * Apply translation
* @method * @method
* @memberof Kinetic.Transform.prototype * @memberof Kinetic.Transform.prototype
* @param {Number} x * @param {Number} x
* @param {Number} y * @param {Number} y
* @returns {Kinetic.Transform}
*/ */
translate: function(x, y) { translate: function(x, y) {
this.m[4] += this.m[0] * x + this.m[2] * y; this.m[4] += this.m[0] * x + this.m[2] * y;
this.m[5] += this.m[1] * x + this.m[3] * y; this.m[5] += this.m[1] * x + this.m[3] * y;
return this;
}, },
/** /**
* Apply scale * Apply scale
@@ -129,18 +155,21 @@
* @memberof Kinetic.Transform.prototype * @memberof Kinetic.Transform.prototype
* @param {Number} sx * @param {Number} sx
* @param {Number} sy * @param {Number} sy
* @returns {Kinetic.Transform}
*/ */
scale: function(sx, sy) { scale: function(sx, sy) {
this.m[0] *= sx; this.m[0] *= sx;
this.m[1] *= sx; this.m[1] *= sx;
this.m[2] *= sy; this.m[2] *= sy;
this.m[3] *= sy; this.m[3] *= sy;
return this;
}, },
/** /**
* Apply rotation * Apply rotation
* @method * @method
* @memberof Kinetic.Transform.prototype * @memberof Kinetic.Transform.prototype
* @param {Number} rad Angle in radians * @param {Number} rad Angle in radians
* @returns {Kinetic.Transform}
*/ */
rotate: function(rad) { rotate: function(rad) {
var c = Math.cos(rad); var c = Math.cos(rad);
@@ -153,6 +182,7 @@
this.m[1] = m12; this.m[1] = m12;
this.m[2] = m21; this.m[2] = m21;
this.m[3] = m22; this.m[3] = m22;
return this;
}, },
/** /**
* Returns the translation * Returns the translation
@@ -172,6 +202,7 @@
* @memberof Kinetic.Transform.prototype * @memberof Kinetic.Transform.prototype
* @param {Number} sx * @param {Number} sx
* @param {Number} sy * @param {Number} sy
* @returns {Kinetic.Transform}
*/ */
skew: function(sx, sy) { skew: function(sx, sy) {
var m11 = this.m[0] + this.m[2] * sy; var m11 = this.m[0] + this.m[2] * sy;
@@ -182,12 +213,14 @@
this.m[1] = m12; this.m[1] = m12;
this.m[2] = m21; this.m[2] = m21;
this.m[3] = m22; this.m[3] = m22;
return this;
}, },
/** /**
* Transform multiplication * Transform multiplication
* @method * @method
* @memberof Kinetic.Transform.prototype * @memberof Kinetic.Transform.prototype
* @param {Kinetic.Transform} matrix * @param {Kinetic.Transform} matrix
* @returns {Kinetic.Transform}
*/ */
multiply: function(matrix) { multiply: function(matrix) {
var m11 = this.m[0] * matrix.m[0] + this.m[2] * matrix.m[1]; var m11 = this.m[0] * matrix.m[0] + this.m[2] * matrix.m[1];
@@ -205,11 +238,13 @@
this.m[3] = m22; this.m[3] = m22;
this.m[4] = dx; this.m[4] = dx;
this.m[5] = dy; this.m[5] = dy;
return this;
}, },
/** /**
* Invert the matrix * Invert the matrix
* @method * @method
* @memberof Kinetic.Transform.prototype * @memberof Kinetic.Transform.prototype
* @returns {Kinetic.Transform}
*/ */
invert: function() { invert: function() {
var d = 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]); var d = 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
@@ -225,6 +260,7 @@
this.m[3] = m3; this.m[3] = m3;
this.m[4] = m4; this.m[4] = m4;
this.m[5] = m5; this.m[5] = m5;
return this;
}, },
/** /**
* return matrix * return matrix
@@ -238,6 +274,7 @@
* set to absolute position via translation * set to absolute position via translation
* @method * @method
* @memberof Kinetic.Transform.prototype * @memberof Kinetic.Transform.prototype
* @returns {Kinetic.Transform}
* @author ericdrowell * @author ericdrowell
*/ */
setAbsolutePosition: function(x, y) { setAbsolutePosition: function(x, y) {
@@ -250,7 +287,7 @@
yt = ((m0 * (y - m5)) - (m1 * (x - m4))) / ((m0 * m3) - (m1 * m2)), yt = ((m0 * (y - m5)) - (m1 * (x - m4))) / ((m0 * m3) - (m1 * m2)),
xt = (x - m4 - (m2 * yt)) / m0; xt = (x - m4 - (m2 * yt)) / m0;
this.translate(xt, yt); return this.translate(xt, yt);
} }
}; };