gl2d
Version:
2D graphics package for WebGL
841 lines • 28.6 kB
JavaScript
"use strict";
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 });
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
var point_1 = require("./point");
var struct_1 = require("gulp-structify/struct");
var buffer_1 = require("gulp-structify/buffer");
var mixin_1 = require("gulp-structify/mixin");
/**
* A rectangle with (left, top, right, bottom) boundaries.
*/
var Rect = (function () {
/**
* A rectangle with (left, top, right, bottom) boundaries.
*/
function Rect(left, top, right, bottom) {
if (left === void 0) { left = 0; }
if (top === void 0) { top = 0; }
if (right === void 0) { right = 0; }
if (bottom === void 0) { bottom = 0; }
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
Rect.ltrb = function (left, top, right, bottom) {
var rect = new Rect();
rect.setLtrb(left, top, right, bottom);
return rect;
};
Rect.lbrt = function (left, bottom, right, top) {
var rect = new Rect();
rect.setLbrt(left, bottom, right, top);
return rect;
};
Rect.lrbt = function (left, right, bottom, top) {
var rect = new Rect();
rect.setLrbt(left, right, bottom, top);
return rect;
};
Rect.ltwh = function (left, top, width, height) {
var rect = new Rect();
rect.setLtwh(left, top, width, height);
return rect;
};
Rect.lbwh = function (left, bottom, width, height) {
var rect = new Rect();
rect.setLbwh(left, bottom, width, height);
return rect;
};
Rect.unionOfPoints = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length; }
var rect = new Rect();
rect.setUnionOfPoints(points, offset, count);
return rect;
};
Rect.unionOfPoints$ = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length >> 1; }
var rect = new Rect();
rect.setUnionOfPoints$(points, offset, count);
return rect;
};
Rect.create = function (other) {
var rect = new Rect();
rect.set(other);
return rect;
};
/**
* Sets the boundaries of this Rect in left-top-right-bottom order.
*/
Rect.prototype.setLtrb = function (left, top, right, bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
};
/**
* Sets the boundaries of this Rect in left-bottom-right-top order.
*/
Rect.prototype.setLbrt = function (left, bottom, right, top) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
};
/**
* Sets the boundaries of this Rect in left-right-bottom-top order.
*/
Rect.prototype.setLrbt = function (left, right, bottom, top) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
};
/**
* Sets the dimensions of this rect in left-top-width-height order.
*/
Rect.prototype.setLtwh = function (left, top, width, height) {
this.left = left;
this.top = top;
this.right = left + width;
this.bottom = top - height;
};
/**
* Sets the dimensions of this rect in left-bottom-width-height order.
*/
Rect.prototype.setLbwh = function (left, bottom, width, height) {
this.left = left;
this.top = bottom + height;
this.right = left + width;
this.bottom = bottom;
};
/**
* Checks if this Rect is empty. True if left >= right or bottom >= top.
*/
Rect.prototype.isEmpty = function () {
return this.left >= this.right || this.bottom >= this.top;
};
/**
* Checks if the boundaries of this Rect represent a valid rectangle. True if right >= left and top >= bottom.
*/
Rect.prototype.isValid = function () {
return this.right >= this.left && this.top >= this.bottom;
};
/**
* Computes the width of this Rect.
*/
Rect.prototype.width = function () {
return this.right - this.left;
};
/**
* Computes the height of this Rect.
*/
Rect.prototype.height = function () {
return this.top - this.bottom;
};
/**
* Computes the area of this Rect.
*/
Rect.prototype.area = function () {
return this.width() * this.height();
};
/**
* Finds the x-coordinate of the point at the center of this Rect.
*/
Rect.prototype.centerX = function () {
return 0.5 * (this.left + this.right);
};
/**
* Finds the y-coordinate of the point at the center of this Rect.
*/
Rect.prototype.centerY = function () {
return 0.5 * (this.bottom + this.top);
};
/**
* Gets the point at the center of this Rect
*/
Rect.prototype.center = function () {
return new point_1.Point(this.centerX(), this.centerY());
};
/**
* Gets the point between the top left and top right corners of this Rect.
*/
Rect.prototype.centerTop = function () {
return new point_1.Point(this.centerX(), this.top);
};
/**
* Gets the point between the bottom left and bottom right corners of this Rect.
*/
Rect.prototype.centerBottom = function () {
return new point_1.Point(this.centerX(), this.bottom);
};
/**
* Gets the point between the top and bottom left corners of this Rect.
*/
Rect.prototype.centerLeft = function () {
return new point_1.Point(this.left, this.centerY());
};
/**
* Gets the point between the top and bottom right corners of this Rect.
*/
Rect.prototype.centerRight = function () {
return new point_1.Point(this.right, this.centerY());
};
/**
* Gets the point at the bottom left corner of this Rect.
*/
Rect.prototype.bottomLeft = function () {
return new point_1.Point(this.left, this.bottom);
};
/**
* Gets the point at the bottom right corner of this Rect.
*/
Rect.prototype.bottomRight = function () {
return new point_1.Point(this.right, this.bottom);
};
/**
* Gets the point at the top left corner of this Rect.
*/
Rect.prototype.topLeft = function () {
return new point_1.Point(this.left, this.top);
};
/**
* Gets the point at the top right corner of this Rect.
*/
Rect.prototype.topRight = function () {
return new point_1.Point(this.right, this.top);
};
/**
* Gets the four corners of this Rect entered in CCW order beginning with the top left vertex.
*/
Rect.prototype.corners = function () {
return [this.topLeft(), this.bottomLeft(), this.bottomRight(), this.topRight()];
};
/**
* Sets this Rect to the empty rect (0,0,0,0).
*/
Rect.prototype.empty = function () {
this.left = 0;
this.top = 0;
this.right = 0;
this.bottom = 0;
};
/**
* Sets this Rect to the smallest rectangle containing the two specified points.
*/
Rect.prototype.setUnionOfPoints = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length; }
this.left = this.right = points[offset].x;
this.top = this.bottom = points[offset].y;
this.unionPoints(points, offset + 1, count - 1);
};
/**
* Sets this Rect to the smallest rectangle containing a subset of points in the specified array.
* @param points array of points entered as a series of (x,y) coordinates.
* @param offset offset of the first point in the subset.
* @param count number of points in the subset.
*/
Rect.prototype.setUnionOfPoints$ = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length >> 1; }
this.left = this.right = points[offset++];
this.top = this.bottom = points[offset++];
this.unionPoints$(points, offset, count - 1);
};
/**
* Checks if this Rect contains the other Rect.
*/
Rect.prototype.containsRect = function (other) {
return this.left <= other.left && other.right <= this.right &&
this.bottom <= other.bottom && other.top <= this.top;
};
/**
* Checks if this Rect contains the specified point.
*/
Rect.prototype.contains = function (p) {
return this.contains$(p.x, p.y);
};
/**
* Checks if this Rect contains the point (x,y).
*/
Rect.prototype.contains$ = function (x, y) {
return this.left <= x && x <= this.right && this.bottom <= y && y <= this.top;
};
/**
* Checks if this Rect intersects the other Rect.
*/
Rect.prototype.intersects = function (other) {
return this.right >= other.left && other.right >= this.left
&& this.top >= other.bottom && other.top >= this.bottom;
};
/**
* Sets this Rect to the intersection of itself with the other Rect.
*/
Rect.prototype.intersect = function (other) {
this.left = Math.max(this.left, other.left);
this.right = Math.min(this.right, other.right);
this.bottom = Math.max(this.bottom, other.bottom);
this.top = Math.min(this.top, other.top);
};
/**
* Expands this Rect to enclose the other Rect.
*/
Rect.prototype.union = function (other) {
this.left = Math.min(this.left, other.left);
this.right = Math.max(this.right, other.right);
this.bottom = Math.min(this.bottom, other.bottom);
this.top = Math.max(this.top, other.top);
};
/**
* Expands this Rect to enclose the specified point.
*/
Rect.prototype.unionPoint = function (p) {
this.unionPoint$(p.x, p.y);
};
/**
* Expands this Rect to enclose the point (x,y).
*/
Rect.prototype.unionPoint$ = function (x, y) {
this.left = Math.min(x, this.left);
this.top = Math.max(y, this.top);
this.right = Math.max(x, this.right);
this.bottom = Math.min(y, this.bottom);
};
/**
* Expands this Rect to enclose the specified points
* @param points array of points.
* @param offset offset of the first point in the subset.
* @param count number of points in the subset.
*/
Rect.prototype.unionPoints = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length; }
while (count-- > 0) {
//Expand this Rect to enclose the point
this.unionPoint(points[offset++]);
}
};
/**
* Expands this Rect to enclose the specified points
* @param points array of points entered as a series of (x,y) coordinates.
* @param offset offset of the first point in the subset.
* @param count number of points in the subset.
*/
Rect.prototype.unionPoints$ = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length >> 1; }
while (count-- > 0) {
//Expand this Rect to enclose the point
this.unionPoint$(points[offset++], points[offset++]);
}
};
/**
* Insets the boundaries of this Rect by the specified vector.
*/
Rect.prototype.inset = function (vec) {
this.inset$(vec.x, vec.y);
};
/**
* Insets the boundaries of this Rect by the vector (dx,dy).
*/
Rect.prototype.inset$ = function (dx, dy) {
this.left += dx;
this.top -= dy;
this.right -= dx;
this.bottom += dy;
};
/**
* Offsets the boundaries of this Rect by the specified vector.
*/
Rect.prototype.offset = function (vec) {
this.offset$(vec.x, vec.y);
};
/**
* Offsets the boundaries of this Rect by the vector (dx,dy).
*/
Rect.prototype.offset$ = function (dx, dy) {
this.left += dx;
this.top += dy;
this.right += dx;
this.bottom += dy;
};
/**
* Scales this Rect out from it's center by the specified (sx,sy) percentages.
* @param r the Rect to scale.
* @param sx the percentage by which to scale in the horizontal direction.
* @param sy the percentage by which to scale in the vertical direction.
*/
Rect.prototype.scale = function (sx, sy) {
var cx = this.centerX();
var cy = this.centerY();
this.offset$(-cx, -cy);
this.left *= sx;
this.right *= sx;
this.bottom *= sy;
this.top *= sy;
this.offset$(cx, cy);
};
/**
* Stretches this Rect out from it's center by the specified ratio, maintaining aspect.
* @param ratio the percentage by which to stretch in all directions.
*/
Rect.prototype.stretch = function (ratio) {
this.scale(ratio, ratio);
};
/**
* Shrinks this Rect to a square with the same center point.
*/
Rect.prototype.shrinkToSquare = function () {
var w = this.width(), h = this.height();
if (h > w) {
// Cut off top and bottom edges by scaling
this.scale(1, w / h);
}
else if (w > h) {
// Cut off left and right edges by scaling
this.scale(h / w, 1);
}
};
/**
* Expands this Rect to a square with the same center point.
*/
Rect.prototype.expandToSquare = function () {
var w = this.width(), h = this.height();
if (h > w) {
// Scale left and right edges so width=height
this.scale(h / w, 1);
}
else if (w > h) {
// Scale top and bottom edges so width=height
this.scale(1, w / h);
}
};
/**
* Swaps the top/bottom or left/right boundaries of this Rect if they are flipped, meaning left > right and/or top > bottom.
*/
Rect.prototype.sort = function () {
if (this.bottom > this.top) {
//Swap top and bottom
var topCopy = this.top;
this.top = this.bottom;
this.bottom = topCopy;
}
if (this.left > this.right) {
//Swap left and right
var rightCopy = this.right;
this.right = this.left;
this.left = rightCopy;
}
};
/**
* Sets each component of this Rect to that of the other Rect.
*/
Rect.prototype.set = function (other) {
this.left = other.left;
this.top = other.top;
this.right = other.right;
this.bottom = other.bottom;
};
/**
* Sets each component of this Rect.
*/
Rect.prototype.set$ = function (left, top, right, bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
};
/**
* Sets each component of this Rect to the specified scalar.
*/
Rect.prototype.setScalar = function (k) {
this.left = k;
this.top = k;
this.right = k;
this.bottom = k;
};
/**
* Adds the other Rect to this Rect componentwise.
*/
Rect.prototype.add = function (other) {
this.left += other.left;
this.top += other.top;
this.right += other.right;
this.bottom += other.bottom;
};
/**
* Adds the specified values to this Rect componentwise.
*/
Rect.prototype.add$ = function (left, top, right, bottom) {
this.left += left;
this.top += top;
this.right += right;
this.bottom += bottom;
};
/**
* Subtracts the other Rect from this Rect componentwise.
*/
Rect.prototype.subtract = function (other) {
this.left -= other.left;
this.top -= other.top;
this.right -= other.right;
this.bottom -= other.bottom;
};
/**
* Subtracts the specified values from this Rect componentwise.
*/
Rect.prototype.subtract$ = function (left, top, right, bottom) {
this.left -= left;
this.top -= top;
this.right -= right;
this.bottom -= bottom;
};
/**
* Multiplies each component of this Rect by the specified scalar.
*/
Rect.prototype.mulScalar = function (k) {
this.left *= k;
this.top *= k;
this.right *= k;
this.bottom *= k;
};
/**
* Divides each component of this Rect by the specified scalar.
*/
Rect.prototype.divScalar = function (k) {
this.left /= k;
this.top /= k;
this.right /= k;
this.bottom /= k;
};
/**
* Checks if each component of this Rect is exactly equal to that of the other Rect.
*/
Rect.prototype.equals = function (other) {
return this.left === other.left && this.top === other.top && this.right === other.right && this.bottom === other.bottom;
};
/**
* Checks if each component of this Rect is exactly equal to the specified scalar.
*/
Rect.prototype.equalsScalar = function (k) {
return this.left === k && this.top === k && this.right === k && this.bottom === k;
};
/**
* Checks if each component of this Rect is approximately equal to that of the other Rect.
*/
Rect.prototype.epsilonEquals = function (other, e) {
return Math.abs(this.left - other.left) <= e && Math.abs(this.top - other.top) <= e && Math.abs(this.right - other.right) <= e && Math.abs(this.bottom - other.bottom) <= e;
};
/**
* Checks if each component of this Rect is approximately equal to the specified scalar.
*/
Rect.prototype.epsilonEqualsScalar = function (k, e) {
return Math.abs(this.left - k) <= e && Math.abs(this.top - k) <= e && Math.abs(this.right - k) <= e && Math.abs(this.bottom - k) <= e;
};
/**
* Returns a string representation of this Rect.
*/
Rect.prototype.toString = function () {
return "{ left: " + this.left + ", top: " + this.top + ", right: " + this.right + ", bottom: " + this.bottom + " }";
};
return Rect;
}());
exports.Rect = Rect;
/**
* A Rect backed by a Float32Array.
*/
var RectStruct = (function (_super) {
__extends(RectStruct, _super);
/**
* Creates a Rect struct backed by the specified data.
*/
function RectStruct(data) {
if (data === void 0) { data = new Float32Array(4); }
return _super.call(this, data) || this;
}
RectStruct.ltrb = function (left, top, right, bottom) {
var rect = new RectStruct();
rect.setLtrb(left, top, right, bottom);
return rect;
};
RectStruct.lbrt = function (left, bottom, right, top) {
var rect = new RectStruct();
rect.setLbrt(left, bottom, right, top);
return rect;
};
RectStruct.lrbt = function (left, right, bottom, top) {
var rect = new RectStruct();
rect.setLrbt(left, right, bottom, top);
return rect;
};
RectStruct.ltwh = function (left, top, width, height) {
var rect = new RectStruct();
rect.setLtwh(left, top, width, height);
return rect;
};
RectStruct.lbwh = function (left, bottom, width, height) {
var rect = new RectStruct();
rect.setLbwh(left, bottom, width, height);
return rect;
};
RectStruct.unionOfPoints = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length; }
var rect = new RectStruct();
rect.setUnionOfPoints(points, offset, count);
return rect;
};
RectStruct.unionOfPoints$ = function (points, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = points.length >> 1; }
var rect = new RectStruct();
rect.setUnionOfPoints$(points, offset, count);
return rect;
};
RectStruct.create = function (other) {
var rect = new RectStruct();
rect.set(other);
return rect;
};
RectStruct.create$ = function (left, top, right, bottom) {
var rect = new RectStruct();
rect.set$(left, top, right, bottom);
return rect;
};
Object.defineProperty(RectStruct.prototype, "left", {
/**
* The left boundary of this Rect.
*/
get: function () {
return this.data[0];
},
/**
* The left boundary of this Rect.
*/
set: function (value) {
this.data[0] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(RectStruct.prototype, "top", {
/**
* The top boundary of this Rect.
*/
get: function () {
return this.data[1];
},
/**
* The top boundary of this Rect.
*/
set: function (value) {
this.data[1] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(RectStruct.prototype, "right", {
/**
* The right boundary of this Rect.
*/
get: function () {
return this.data[2];
},
/**
* The right boundary of this Rect.
*/
set: function (value) {
this.data[2] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(RectStruct.prototype, "bottom", {
/**
* The bottom boundary of this Rect.
*/
get: function () {
return this.data[3];
},
/**
* The bottom boundary of this Rect.
*/
set: function (value) {
this.data[3] = value;
},
enumerable: true,
configurable: true
});
return RectStruct;
}(struct_1.Struct));
exports.RectStruct = RectStruct;
mixin_1.applyMixins(RectStruct, Rect);
/**
* A Rect buffer backed by a Float32Array.
*/
var RectBuffer = (function (_super) {
__extends(RectBuffer, _super);
function RectBuffer() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Creates an empty Rect buffer with the specified Rect capacity.
*/
RectBuffer.create = function (capacity) {
return new RectBuffer(new Float32Array(capacity * 4));
};
Object.defineProperty(RectBuffer.prototype, "left", {
/**
* The left boundary of the current Rect.
*/
get: function () {
return this.data[this.dataPosition + 0];
},
/**
* The left boundary of the current Rect.
*/
set: function (value) {
this.data[this.dataPosition + 0] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(RectBuffer.prototype, "top", {
/**
* The top boundary of the current Rect.
*/
get: function () {
return this.data[this.dataPosition + 1];
},
/**
* The top boundary of the current Rect.
*/
set: function (value) {
this.data[this.dataPosition + 1] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(RectBuffer.prototype, "right", {
/**
* The right boundary of the current Rect.
*/
get: function () {
return this.data[this.dataPosition + 2];
},
/**
* The right boundary of the current Rect.
*/
set: function (value) {
this.data[this.dataPosition + 2] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(RectBuffer.prototype, "bottom", {
/**
* The bottom boundary of the current Rect.
*/
get: function () {
return this.data[this.dataPosition + 3];
},
/**
* The bottom boundary of the current Rect.
*/
set: function (value) {
this.data[this.dataPosition + 3] = value;
},
enumerable: true,
configurable: true
});
/**
* Gets the number of properties in a Rect, namely 4.
*/
RectBuffer.prototype.structLength = function () {
return 4;
};
/**
* Gets the components of the Rect at the specified position of this buffer.
*/
RectBuffer.prototype.aget = function (position, dst) {
if (dst === void 0) {
dst = new Rect();
}
;
var dataPos = position * this.structLength();
dst.left = this.data[dataPos++];
dst.top = this.data[dataPos++];
dst.right = this.data[dataPos++];
dst.bottom = this.data[dataPos++];
return dst;
};
/**
* Gets the components of the current Rect, then moves to the next position of this buffer.
*/
RectBuffer.prototype.rget = function (dst) {
if (dst === void 0) {
dst = new Rect();
}
;
dst.left = this.data[this.dataPosition++];
dst.top = this.data[this.dataPosition++];
dst.right = this.data[this.dataPosition++];
dst.bottom = this.data[this.dataPosition++];
return dst;
};
/**
* Sets each component of the Rect at the specified position to that of the src Rect.
*/
RectBuffer.prototype.aset = function (position, src) {
var dataPos = position * this.structLength();
this.data[dataPos++] = src.left;
this.data[dataPos++] = src.top;
this.data[dataPos++] = src.right;
this.data[dataPos++] = src.bottom;
};
/**
* Sets each component of the Rect at the specified position.
*/
RectBuffer.prototype.aset$ = function (position, left, top, right, bottom) {
var dataPos = position * this.structLength();
this.data[dataPos++] = left;
this.data[dataPos++] = top;
this.data[dataPos++] = right;
this.data[dataPos++] = bottom;
};
/**
* Sets each component of the current Rect to that of the src Rect, then moves to the next position of this buffer.
*/
RectBuffer.prototype.rset = function (src) {
this.data[this.dataPosition++] = src.left;
this.data[this.dataPosition++] = src.top;
this.data[this.dataPosition++] = src.right;
this.data[this.dataPosition++] = src.bottom;
};
/**
* Sets each component of the current Rect, then moves to the next position of this buffer.
*/
RectBuffer.prototype.rset$ = function (left, top, right, bottom) {
this.data[this.dataPosition++] = left;
this.data[this.dataPosition++] = top;
this.data[this.dataPosition++] = right;
this.data[this.dataPosition++] = bottom;
};
return RectBuffer;
}(buffer_1.StructBuffer));
exports.RectBuffer = RectBuffer;
mixin_1.applyMixins(RectBuffer, Rect);
//# sourceMappingURL=rect.js.map