UNPKG

gl2d

Version:

2D graphics package for WebGL

108 lines 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var point_1 = require("../struct/point"); var mat2d_1 = require("../struct/mat2d"); /** * A graphic that can be transformed by altering its model matrix. */ var Graphic = (function () { /** * Creates a new graphic with the specified matrix transformation. * @param matrix the initial matrix transformation. Defaults to identity. */ function Graphic(matrix) { this.matrix = matrix || mat2d_1.Mat2dStruct.identity(); } /** * Measures the position of this graphic's center point in world space. * Assumes the model for the graphic is centered at the origin. * @returns the position of the center point in world space. */ Graphic.prototype.measureCenter = function () { return new point_1.Point(this.matrix.c3r1, this.matrix.c3r2); }; /** * Converts a point in this graphic's model space to a point in world space. * @param pointInModel the point in model space. * @param dst where to store the result. * @returns dst. */ Graphic.prototype.convertPointToWorldSpace = function (pointInModel, dst) { if (dst === void 0) { dst = new point_1.Point(); } this.matrix.map(pointInModel, dst); return dst; }; /** * Converts a point in world space to a point in this graphic's model space using the inverse model matrix. * @param pointInWorld the point in world space. * @param inverse the inverse of this graphic's model matrix. If undefined, the inverse matrix will be calculated on the fly, resulting in a potential performance hit. * @param dst where to write the result. Defaults to new point. * @returns dst. */ Graphic.prototype.convertPointToModelSpace = function (pointInWorld, inverse, dst) { if (inverse === void 0) { inverse = mat2d_1.Mat2d.inverse(this.matrix); } if (dst === void 0) { dst = new point_1.Point(); } inverse.map(pointInWorld, dst); return dst; }; /** * Offsets this graphic by the specified vector. */ Graphic.prototype.offset = function (vec) { this.offset$(vec.x, vec.y); }; /** * Offsets this graphic by the vector (dx,dy). */ Graphic.prototype.offset$ = function (dx, dy) { this.matrix.postTranslate$(dx, dy); }; /** * Offsets this graphic so that it is centered at the specified point. * Assumes the model for the graphic is centered at the origin. * @param center the new center point for this graphic. */ Graphic.prototype.offsetTo = function (center) { this.offsetTo$(center.x, center.y); }; /** * Offsets this graphic so that it is centered at the point (x,y). * Assumes the model for the graphic is centered at the origin. */ Graphic.prototype.offsetTo$ = function (x, y) { this.matrix.c3r1 = x; this.matrix.c3r2 = y; }; /** * Transforms this graphic by the specified matrix. * @param matrix the transformation matrix. */ Graphic.prototype.transform = function (matrix) { this.matrix.postConcat(matrix); }; /** * Scales this graphic by the specified scale factors, with a pivot point at the center of the shape. * @param sx the horizontal scale factor. * @param sy the vertical scale factor. */ Graphic.prototype.scale = function (sx, sy) { this.transform(mat2d_1.Mat2d.scale(sx, sy, this.measureCenter())); }; /** * Stretches this graphic by the specified ratio, with a pivot point at the center of the shape. * @param ratio the percentage by which to scale in all directions. */ Graphic.prototype.stretch = function (ratio) { this.scale(ratio, ratio); }; /** * Scales this graphic by the specified angle, with a pivot point at the center of the shape. * @param radians the angle in radians. */ Graphic.prototype.rotate = function (radians) { this.transform(mat2d_1.Mat2d.rotate(radians, this.measureCenter())); }; return Graphic; }()); exports.Graphic = Graphic; //# sourceMappingURL=graphic.js.map