@daign/2d-pipeline
Version:
Two dimensional graphics pipeline.
83 lines (82 loc) • 3.29 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransformCollection = void 0;
var math_1 = require("@daign/math");
/**
* Collection of transformations combined into a single transformation.
* The transformations are applied in order from highest to lowest index.
*/
var TransformCollection = /** @class */ (function (_super) {
__extends(TransformCollection, _super);
/**
* Constructor.
*/
function TransformCollection() {
var _this = _super.call(this) || this;
/**
* The transformation matrix.
*/
_this.transformMatrix = new math_1.Matrix3().setIdentity();
/**
* The matrix of the inverse transformation.
*/
_this.inverseTransformMatrix = new math_1.Matrix3().setIdentity();
/**
* The transformation matrix, not including native transforms.
*/
_this.transformMatrixNonNative = new math_1.Matrix3().setIdentity();
/**
* The native SVG transform attribute string.
*/
_this.nativeSvgTransform = null;
_this.subscribeToChanges(function () {
_this.combineTransformations();
});
return _this;
}
/**
* Combine all transformations in the collection into a single transformation matrix.
*/
TransformCollection.prototype.combineTransformations = function () {
var _this = this;
this.transformMatrix.setIdentity();
this.transformMatrixNonNative.setIdentity();
this.nativeSvgTransform = null;
var nativeTransformsArray = [];
this.iterate(function (item) {
_this.transformMatrix.multiply(item.matrix);
_this.transformMatrixNonNative.multiply(item.matrixNonNative);
// Put the native transforms in an array.
if (item.nativeSvgTransform) {
nativeTransformsArray.push(item.nativeSvgTransform);
}
});
// Create the joined native SVG transform command if the array is not empty.
if (nativeTransformsArray.length > 0) {
this.nativeSvgTransform = nativeTransformsArray.join(', ');
}
try {
this.inverseTransformMatrix.setToInverse(this.transformMatrix);
}
catch (e) {
// Keep the previous inverse matrix if the current can not be inverted.
}
};
return TransformCollection;
}(math_1.GenericArray));
exports.TransformCollection = TransformCollection;