UNPKG

@awayjs/stage

Version:
151 lines (150 loc) 4.61 kB
import { __extends } from "tslib"; import { Rectangle } from '@awayjs/core'; import { ImageUtils } from '../utils/ImageUtils'; import { ImageBase } from './ImageBase'; var Image2D = /** @class */ (function (_super) { __extends(Image2D, _super); /** * */ function Image2D(width, height, powerOfTwo) { if (powerOfTwo === void 0) { powerOfTwo = true; } var _this = _super.call(this) || this; _this._powerOfTwo = true; _this._rect = new Rectangle(0, 0, Math.round(width), Math.round(height)); _this._powerOfTwo = powerOfTwo; _this._testDimensions(); return _this; } Object.defineProperty(Image2D.prototype, "assetType", { /** * * @returns {string} */ get: function () { return Image2D.assetType; }, enumerable: false, configurable: true }); Image2D.prototype.getImageType = function () { return '2d'; }; Object.defineProperty(Image2D.prototype, "height", { /** * The height of the image in pixels. */ get: function () { return this._rect.height; }, set: function (value) { this._setSize(this._rect.width, value); }, enumerable: false, configurable: true }); Object.defineProperty(Image2D.prototype, "alphaChannel", { get: function () { return this._alphaChannel; }, set: function (value) { this._alphaChannel = value; }, enumerable: false, configurable: true }); Object.defineProperty(Image2D.prototype, "rect", { /** * The rectangle that defines the size and location of the bitmap image. The * top and left of the rectangle are 0; the width and height are equal to the * width and height in pixels of the BitmapData object. */ get: function () { return this._rect; }, enumerable: false, configurable: true }); Object.defineProperty(Image2D.prototype, "width", { /** * The width of the bitmap image in pixels. */ get: function () { return this._rect.width; }, set: function (value) { this._setSize(value, this._rect.height); }, enumerable: false, configurable: true }); /** * * @param width * @param height * @private */ Image2D.prototype._setSize = function (width, height) { width = Math.round(width); height = Math.round(height); if (this._rect.width == width && this._rect.height == height) return; this.clear(); this._rect.width = width; this._rect.height = height; this._testDimensions(); }; /** * * @private */ Image2D.prototype._testDimensions = function () { if (this._powerOfTwo && (!ImageUtils.isDimensionValid(this._rect.width) || !ImageUtils.isDimensionValid(this._rect.height))) throw new Error('Invalid dimension: Width and height must be power of 2 and cannot exceed 2048'); }; Object.defineProperty(Image2D.prototype, "powerOfTwo", { /** * Enable POT texture size validation * @returns {boolean} */ get: function () { return this._powerOfTwo; }, set: function (value) { if (this._powerOfTwo == value) return; this._powerOfTwo = value; this._testDimensions(); }, enumerable: false, configurable: true }); Image2D.assetType = '[image Image2D]'; return Image2D; }(ImageBase)); export { Image2D }; import { ContextGLTextureFormat } from '../base/ContextGLTextureFormat'; import { _Stage_ImageBase } from './ImageBase'; /** * * @class away.pool.ImageStateBase */ var _Stage_Image2D = /** @class */ (function (_super) { __extends(_Stage_Image2D, _super); function _Stage_Image2D() { return _super !== null && _super.apply(this, arguments) || this; } /** * * @param context * @returns {ITexture} */ _Stage_Image2D.prototype._createTexture = function () { var image = this.image; this._texture = this._pool.context.createTexture(image.width, image.height, ContextGLTextureFormat.BGRA, true); }; return _Stage_Image2D; }(_Stage_ImageBase)); export { _Stage_Image2D }; // MOVED TO LIB INDEX // Stage.registerAbstraction(_Stage_Image2D, Image2D);