UNPKG

laya-coreui-es

Version:

laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改

111 lines (110 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheStyle = void 0; const Point_1 = require("../../maths/Point"); const Rectangle_1 = require("../../maths/Rectangle"); const HTMLCanvas_1 = require("../../resource/HTMLCanvas"); const Pool_1 = require("../../utils/Pool"); class CacheStyle { constructor() { this.reset(); } needBitmapCache() { return this.cacheForFilters || !!this.mask; } needEnableCanvasRender() { return this.userSetCache != "none" || this.cacheForFilters || !!this.mask; } releaseContext() { if (this.canvas && this.canvas.size) { Pool_1.Pool.recover("CacheCanvas", this.canvas); this.canvas.size(0, 0); try { this.canvas.width = 0; this.canvas.height = 0; } catch (e) { } } this.canvas = null; } createContext() { if (!this.canvas) { this.canvas = Pool_1.Pool.getItem("CacheCanvas") || new HTMLCanvas_1.HTMLCanvas(false); var tx = this.canvas.context; if (!tx) { tx = this.canvas.getContext('2d'); } } } releaseFilterCache() { var fc = this.filterCache; if (fc) { fc.destroy(); fc.recycle(); this.filterCache = null; } } recover() { if (this === CacheStyle.EMPTY) return; Pool_1.Pool.recover("SpriteCache", this.reset()); } reset() { this.releaseContext(); this.releaseFilterCache(); this.cacheAs = "none"; this.enableCanvasRender = false; this.userSetCache = "none"; this.cacheForFilters = false; this.staticCache = false; this.reCache = true; this.mask = null; this.maskParent = null; this.filterCache = null; this.filters = null; this.hasGlowFilter = false; if (this.cacheRect) this.cacheRect.recover(); this.cacheRect = null; return this; } static create() { return Pool_1.Pool.getItemByClass("SpriteCache", CacheStyle); } _calculateCacheRect(sprite, tCacheType, x, y) { var _cacheStyle = sprite._cacheStyle; if (!_cacheStyle.cacheRect) _cacheStyle.cacheRect = Rectangle_1.Rectangle.create(); var tRec; if (tCacheType === "bitmap") { tRec = sprite.getSelfBounds(); tRec.width = tRec.width + CacheStyle.CANVAS_EXTEND_EDGE * 2; tRec.height = tRec.height + CacheStyle.CANVAS_EXTEND_EDGE * 2; tRec.x = tRec.x - sprite.pivotX; tRec.y = tRec.y - sprite.pivotY; tRec.x = tRec.x - CacheStyle.CANVAS_EXTEND_EDGE; tRec.y = tRec.y - CacheStyle.CANVAS_EXTEND_EDGE; tRec.x = Math.floor(tRec.x + x) - x; tRec.y = Math.floor(tRec.y + y) - y; tRec.width = Math.floor(tRec.width); tRec.height = Math.floor(tRec.height); _cacheStyle.cacheRect.copyFrom(tRec); } else { _cacheStyle.cacheRect.setTo(-sprite._style.pivotX, -sprite._style.pivotY, 1, 1); } tRec = _cacheStyle.cacheRect; if (sprite._style.scrollRect) { var scrollRect = sprite._style.scrollRect; tRec.x -= scrollRect.x; tRec.y -= scrollRect.y; } CacheStyle._scaleInfo.setTo(1, 1); return CacheStyle._scaleInfo; } } exports.CacheStyle = CacheStyle; CacheStyle.EMPTY = new CacheStyle(); CacheStyle._scaleInfo = new Point_1.Point(); CacheStyle.CANVAS_EXTEND_EDGE = 16;