@aurigma/design-atoms-model
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
156 lines • 6.44 kB
JavaScript
import { PointF } from "./PointF";
import { ConvertDegreeToRadian, ConvertRadianToDegree, EqualsOfFloatNumbers } from "./Common";
import * as _ from "underscore";
import { ArgumentException } from "../Exception";
/**
* Creates a 2D transform matrix.
* [ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ]
* [ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ]
* [ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]
*/
var Matrix = /** @class */ (function () {
function Matrix(m00, m10, m01, m11, m02, m12) {
this._equalityTolerance = 0.000001;
if (arguments.length == 6) {
this.setTransform(m00, m10, m01, m11, m02, m12);
}
else if (arguments.length == 0) {
this.setTransform(1, 0, 0, 1, 0, 0);
}
else {
throw new Error('Arguments error');
}
}
Object.defineProperty(Matrix.prototype, "angle", {
get: function () {
return ConvertRadianToDegree(Math.atan(-this.m01 / this.m00));
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix.prototype, "scaleX", {
get: function () {
return Math.sqrt(this.m00 * this.m00 + this.m10 * this.m10);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix.prototype, "scaleY", {
get: function () {
return Math.sqrt(this.m01 * this.m01 + this.m11 * this.m11);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix.prototype, "translateX", {
get: function () {
return this.m02;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix.prototype, "translateY", {
get: function () {
return this.m12;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix.prototype, "isIdentity", {
get: function () {
return EqualsOfFloatNumbers(this.m00, 1, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m01, 0, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m02, 0, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m10, 0, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m11, 1, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m12, 0, this._equalityTolerance);
},
enumerable: true,
configurable: true
});
Matrix.prototype.setTransform = function (m00, m10, m01, m11, m02, m12) {
this.m00 = m00;
this.m10 = m10;
this.m01 = m01;
this.m11 = m11;
this.m02 = m02;
this.m12 = m12;
};
Matrix.prototype.transformPoint = function (p, clone) {
if (clone === void 0) { clone = false; }
var pp = clone ? new PointF() : p;
var x = p.x, y = p.y;
pp.x = x * this.m00 + y * this.m01 + this.m02;
pp.y = x * this.m10 + y * this.m11 + this.m12;
return pp;
};
Matrix.prototype.concatenate = function (m) {
var m00 = this.m00;
var m01 = this.m01;
this.m00 = m00 * m.m00 + m01 * m.m10;
this.m01 = m00 * m.m01 + m01 * m.m11;
this.m02 = m00 * m.m02 + m01 * m.m12 + this.m02;
var m10 = this.m10;
var m11 = this.m11;
this.m10 = m10 * m.m00 + m11 * m.m10;
this.m11 = m10 * m.m01 + m11 * m.m11;
this.m12 = m10 * m.m02 + m11 * m.m12 + this.m12;
return this;
};
Matrix.prototype.rotate = function (angle) {
angle = ConvertDegreeToRadian(angle);
var cos = Math.cos(angle);
var sin = Math.sin(angle);
return this.concatenate(new Matrix(cos, sin, -sin, cos, 0, 0));
};
Matrix.prototype.rotateAt = function (angle, x, y) {
this.translate(x, y);
this.rotate(angle);
this.translate(-x, -y);
return this;
};
Matrix.prototype.scale = function (sx, sy) {
return this.concatenate(new Matrix(sx, 0, 0, sy, 0, 0));
};
Matrix.prototype.scaleRelativeToPoint = function (sx, sy, origin) {
return this.concatenate(new Matrix(sx, 0, 0, sy, origin.x * (1 - sx), origin.y * (1 - sy)));
};
Matrix.prototype.translate = function (dx, dy) {
return this.concatenate(new Matrix(1, 0, 0, 1, dx, dy));
};
Matrix.prototype.clone = function () {
return new Matrix(this.m00, this.m10, this.m01, this.m11, this.m02, this.m12);
};
Matrix.prototype.updateObjectByMatrix = function (object, key1, key2) {
if (object == null)
throw new ArgumentException("object cannot be null");
if (!_.isNumber(object[key1]) || !_.isNumber(object[key2]))
throw new ArgumentException("object members by given keys nust be a numbers!");
var resultPoint = this.transformPoint(new PointF(object[key1], object[key2]));
object[key1] = resultPoint.x;
object[key2] = resultPoint.y;
return object;
};
Matrix.prototype.getInversed = function () {
var determinant = this._determinant;
return new Matrix(this.m11 / determinant, -this.m10 / determinant, -this.m01 / determinant, this.m00 / determinant, (this.m01 * this.m12 - this.m11 * this.m02) / determinant, (this.m10 * this.m02 - this.m00 * this.m12) / determinant);
};
Matrix.prototype.equals = function (other) {
return EqualsOfFloatNumbers(this.m00, other.m00, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m01, other.m01, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m02, other.m02, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m10, other.m10, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m11, other.m11, this._equalityTolerance)
&& EqualsOfFloatNumbers(this.m12, other.m12, this._equalityTolerance);
};
Object.defineProperty(Matrix.prototype, "_determinant", {
get: function () {
return this.m00 * this.m11 - this.m01 * this.m10;
},
enumerable: true,
configurable: true
});
return Matrix;
}());
export { Matrix };
//# sourceMappingURL=Matrix.js.map