@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.
143 lines • 6.79 kB
JavaScript
import { EventObject } from "../EventObject";
import { EqualsOfFloatNumbers, ConvertDegreeToRadian } from "./Common";
import { Matrix } from "./Matrix";
var Transform = /** @class */ (function () {
function Transform(scaleX, scaleY, translateX, translateY, angle) {
this._angle = 0;
this._scaleX = 0;
this._scaleY = 0;
this._translateX = 0;
this._translateY = 0;
this._transformChangedEvent = new EventObject();
this._angle = (angle) ? angle : 0;
this._scaleX = (scaleX || scaleX === 0) ? scaleX : 1;
this._scaleY = (scaleY || scaleX === 0) ? scaleY : 1;
this._translateX = (translateX) ? translateX : 0;
this._translateY = (translateY) ? translateY : 0;
}
Transform.prototype.addTransformChanged = function (handler) {
this._transformChangedEvent.add(handler);
};
Transform.prototype.removeTransformChanged = function (handler) {
this._transformChangedEvent.remove(handler);
};
Transform.prototype._setProperty = function (propName, value, supressOnChanged) {
if (supressOnChanged === void 0) { supressOnChanged = false; }
var fieldName = "_" + propName;
if (this[fieldName] !== value) {
this[fieldName] = value;
if (!supressOnChanged)
this._transformChangedEvent.notify();
}
};
Object.defineProperty(Transform.prototype, "angle", {
get: function () { return this._angle; },
enumerable: true,
configurable: true
});
Transform.prototype.setAngle = function (value, supressOnChanged) { this._setProperty("angle", value, supressOnChanged); };
Object.defineProperty(Transform.prototype, "scaleX", {
get: function () { return this._scaleX; },
enumerable: true,
configurable: true
});
Transform.prototype.setScaleX = function (value, supressOnChanged) { this._setProperty("scaleX", value, supressOnChanged); };
Object.defineProperty(Transform.prototype, "scaleY", {
get: function () { return this._scaleY; },
enumerable: true,
configurable: true
});
Transform.prototype.setScaleY = function (value, supressOnChanged) { this._setProperty("scaleY", value, supressOnChanged); };
Object.defineProperty(Transform.prototype, "translateX", {
get: function () { return this._translateX; },
enumerable: true,
configurable: true
});
Transform.prototype.setTranslateX = function (value, supressOnChanged) { this._setProperty("translateX", value, supressOnChanged); };
Object.defineProperty(Transform.prototype, "translateY", {
get: function () { return this._translateY; },
enumerable: true,
configurable: true
});
Transform.prototype.setTranslateY = function (value, supressOnChanged) { this._setProperty("translateY", value, supressOnChanged); };
Transform.prototype.move = function (x, y) {
var t = this.clone();
if (x != null)
this.setTranslateX(this._translateX + x, true);
if (y != null)
this.setTranslateY(this._translateY + y, true);
if (!t.equals(this))
this._transformChangedEvent.notify();
};
Transform.prototype.rotate = function (angle) {
if (angle == null || EqualsOfFloatNumbers(angle, 0))
return;
this._angle += angle;
this._transformChangedEvent.notify();
};
Transform.prototype.clone = function () {
return new Transform(this._scaleX, this._scaleY, this._translateX, this._translateY, this._angle);
};
Transform.prototype.equals = function (transform, tolerance) {
return transform != null &&
EqualsOfFloatNumbers(this._scaleX, transform._scaleX, tolerance) &&
EqualsOfFloatNumbers(this._scaleY, transform._scaleY, tolerance) &&
EqualsOfFloatNumbers(this._translateX, transform._translateX, tolerance) &&
EqualsOfFloatNumbers(this._translateY, transform._translateY, tolerance) &&
EqualsOfFloatNumbers(this._angle, transform._angle, tolerance);
};
Transform.isEqual = function (a, b, tolerance) {
if (tolerance === void 0) { tolerance = 0.0001; }
if (a == null && b == null)
return true;
if (a == null || b == null)
return false;
return a.equals(b, tolerance);
};
Transform.prototype.toMatrix = function () {
var angle = this._angle;
var sin = Math.sin(ConvertDegreeToRadian(angle));
var cos = Math.cos(ConvertDegreeToRadian(angle));
return new Matrix(cos * this._scaleX, sin * this._scaleX, -sin * this._scaleY, cos * this._scaleY, this._translateX, this._translateY);
};
Transform.prototype.clear = function (_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.keepAngle, keepAngle = _c === void 0 ? false : _c, _d = _b.supressOnChanged, supressOnChanged = _d === void 0 ? false : _d;
this._scaleX = 1;
this._scaleY = 1;
this._translateX = 0;
this._translateY = 0;
if (!keepAngle)
this._angle = 0;
if (!supressOnChanged)
this._transformChangedEvent.notify();
};
Transform.prototype.applyMatrix = function (matrix, supressOnChanged) {
if (supressOnChanged === void 0) { supressOnChanged = false; }
if (matrix.isIdentity)
return;
var targetMatrix = this.toMatrix().concatenate(matrix);
this._angle = targetMatrix.angle;
this._scaleX = targetMatrix.scaleX;
this._scaleY = targetMatrix.scaleY;
this._translateX = targetMatrix.translateX;
this._translateY = targetMatrix.translateY;
if (!supressOnChanged)
this._transformChangedEvent.notify();
};
Object.defineProperty(Transform.prototype, "isEmpty", {
get: function () {
return EqualsOfFloatNumbers(this._scaleX, 1) && EqualsOfFloatNumbers(this._scaleY, 1) &&
EqualsOfFloatNumbers(this._translateX, 0) && EqualsOfFloatNumbers(this._translateY, 0) &&
EqualsOfFloatNumbers(this._angle, 0);
},
enumerable: true,
configurable: true
});
Transform.prototype.toString = function () {
var r = [this.translateX, this.translateY, this.scaleX, this.scaleY, this.angle].map(function (x) { return x.toFixed(2); });
return "{ tx: " + r[0] + ", ty: " + r[1] + ", sx: " + r[2] + ", sy: " + r[3] + ", a: " + r[4] + "}";
};
return Transform;
}());
export { Transform };
//# sourceMappingURL=Transform.js.map