gl2d
Version:
2D graphics package for WebGL
130 lines • 5.54 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var point_1 = require("../struct/point");
var rect_1 = require("../struct/rect");
var vec2_1 = require("../struct/vec2");
var graphic_1 = require("./graphic");
/**
* Shape defined by matrix transformation of a mesh.
*/
var Shape = (function (_super) {
__extends(Shape, _super);
/**
* Creates a shape with the specified mesh data and initial transformation matrix.
* @param mesh the static vertex and index data data for this shape.
* @param matrix the initial transformation matrix. Defaults to identiy.
*/
function Shape(mesh, matrix) {
var _this = _super.call(this, matrix) || this;
_this.mesh = mesh;
return _this;
}
/**
* Measures the boundaries of this shape in world space.
* @returns the boundaries of this shape, or null if the shape has no vertices.
*/
Shape.prototype.measureBoundaries = function () {
return Shape.measureBoundaries(this.matrix, this.mesh.vertices);
};
/**
* Measures the boundaries of a shape in world space.
* @param matrix the matrix that maps the shape to world space.
* @param vertices the vertices of the shape in model space.
* @returns the boundaries of the shape, or null if the shape has no vertices.
*/
Shape.measureBoundaries = function (matrix, vertices) {
var bounds = null;
if (vertices.moveToFirst()) {
// Enclose the first mapped vertex
var vertex = new point_1.Point();
matrix.map(vertices, vertex);
bounds = rect_1.Rect.unionOfPoints([vertex]);
// Enclose the remaining vertices
while (vertices.moveToNext()) {
matrix.map(vertices, vertex);
bounds.unionPoint(vertex);
}
}
return bounds;
};
/**
* Measures the position of this shape's control point in world space.
* @returns the position of the control point in world space.
*/
Shape.prototype.measureControl = function () {
return this.convertPointToWorldSpace(this.mesh.bounds.centerBottom());
};
/**
* Measures the position of this shape's fixed point in world space.
* @returns the position of the fixed point in world space.
*/
Shape.prototype.measurePivot = function () {
return this.convertPointToWorldSpace(this.mesh.bounds.centerTop());
};
/**
* Measures the position of the specified vertex in world space.
* @param index the index of the vertex in the buffer associated with this shape's mesh.
* @returns the position of the vertex in world space, or null if no vertex exists at the specified position.
*/
Shape.prototype.measureVertex = function (index) {
var vertices = this.mesh.vertices;
if (vertices.moveToPosition(index)) {
return this.convertPointToWorldSpace(vertices);
}
return null;
};
/**
* Checks if this shape contains the specified point.
* @param point the point to check.
* @param inverse the inverse of this shape's model matrix. If undefined, the inverse matrix will be calculated on the fly.
*/
Shape.prototype.contains = function (pt, inverse) {
// This shape contains the point if its mesh contains the model point
var modelPt = this.convertPointToModelSpace(pt, inverse);
return this.mesh.contains(modelPt);
};
/**
* Maps this shape to the destination Rect using the specified scale to fit option.
* @param dst the destination rectangle.
* @param stf the scale to fit option. Defaults to Fill.
*/
Shape.prototype.mapToRect = function (dst, stf) {
if (stf === void 0) { stf = 2 /* Fill */; }
this.matrix.setRectToRect(this.mesh.bounds, dst, stf);
};
/**
* Stretch-rotates this shape across the line from p1 to p2.
*/
Shape.prototype.stretchAcrossLine = function (p1, p2) {
return Shape.stretchAcrossLine(this.matrix, this.mesh, p1, p2);
};
/**
* Sets the specified matrix to stretch rotate the specified mesh across the line from p1 to p2.
*/
Shape.stretchAcrossLine = function (matrix, mesh, p1, p2) {
var bounds = mesh.bounds;
var meshPivot = bounds.centerTop();
var meshControl = bounds.centerBottom();
// *Translate from mesh pivot to shape pivot
var pivot = p1;
var vec = vec2_1.Vec2.fromPointToPoint(meshPivot, pivot);
var start = vec2_1.Vec2.create(meshControl);
start.add(vec);
// Stretch rotate from translated control point to p2, with pivot at p1
matrix.setStretchRotateToPoint(start, p2, pivot);
matrix.preTranslate(vec); //*
};
return Shape;
}(graphic_1.Graphic));
exports.Shape = Shape;
//# sourceMappingURL=shape.js.map