@daign/2d-pipeline
Version:
Two dimensional graphics pipeline.
121 lines (120 loc) • 4.45 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.NativeRotateTransform = void 0;
var math_1 = require("@daign/math");
var transform_1 = require("./transform");
/**
* A rotation transformation that supports native SVG transform usage.
*/
var NativeRotateTransform = /** @class */ (function (_super) {
__extends(NativeRotateTransform, _super);
/**
* Constructor.
*/
function NativeRotateTransform() {
var _this = _super.call(this) || this;
/**
* The angle of the rotation.
*/
_this._angle = new math_1.Angle();
/**
* The rotation center of the rotation.
*/
_this._center = new math_1.Vector2();
// Notify observers when angle or center changes.
var callback = function () {
_this.notifyObservers();
};
_this._angle.subscribeToChanges(callback);
_this._center.subscribeToChanges(callback);
return _this;
}
Object.defineProperty(NativeRotateTransform.prototype, "angle", {
/**
* Getter for the angle.
* @returns The angle.
*/
get: function () {
return this._angle;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NativeRotateTransform.prototype, "center", {
/**
* Getter for the rotation center.
* @returns The rotation center.
*/
get: function () {
return this._center;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NativeRotateTransform.prototype, "matrix", {
/**
* Getter for the transformation matrix.
* @returns The transformation matrix.
*/
get: function () {
// Calculate the matrix from angle and center.
var matrix = new math_1.Matrix3().setRotation(this._angle, this._center);
return matrix;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NativeRotateTransform.prototype, "matrixNonNative", {
/**
* Getter for the transformation matrix, not including native transforms.
* @returns The transformation matrix.
*/
get: function () {
// Since this is a native transform only an identity transformation is returned here.
var matrix = new math_1.Matrix3().setIdentity();
return matrix;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NativeRotateTransform.prototype, "nativeSvgTransform", {
/**
* Getter for the native SVG transform attribute string.
* @returns The transform command string or null.
*/
get: function () {
var command = null;
var normalizedDegrees = this._angle.clone().normalize().degrees;
// If angle is 0, then don't output a command string.
if (normalizedDegrees !== 0) {
// Only include the rotation center if it's different than (0/0).
if (this._center.length() !== 0) {
command = "rotate(".concat(this._angle.degrees, ", ").concat(this._center.x, ", ").concat(this._center.y, ")");
}
else {
command = "rotate(".concat(this._angle.degrees, ")");
}
}
return command;
},
enumerable: false,
configurable: true
});
return NativeRotateTransform;
}(transform_1.Transform));
exports.NativeRotateTransform = NativeRotateTransform;