UNPKG

pixi.js

Version:

<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">

116 lines (112 loc) 4.3 kB
'use strict'; var Extensions = require('../../../extensions/Extensions.js'); var TexturePool = require('../../../rendering/renderers/shared/texture/TexturePool.js'); var TextureStyle = require('../../../rendering/renderers/shared/texture/TextureStyle.js'); var deprecation = require('../../../utils/logging/deprecation.js'); var TextStyle = require('../TextStyle.js'); var getPo2TextureFromSource = require('../utils/getPo2TextureFromSource.js'); var CanvasTextGenerator = require('./CanvasTextGenerator.js'); "use strict"; class CanvasTextSystem { constructor(_renderer) { this._renderer = _renderer; } getTexture(options, _resolution, _style, _textKey) { if (typeof options === "string") { deprecation.deprecation("8.0.0", "CanvasTextSystem.getTexture: Use object TextOptions instead of separate arguments"); options = { text: options, style: _style, resolution: _resolution }; } if (!(options.style instanceof TextStyle.TextStyle)) { options.style = new TextStyle.TextStyle(options.style); } if (!(options.textureStyle instanceof TextureStyle.TextureStyle)) { options.textureStyle = new TextureStyle.TextureStyle(options.textureStyle); } if (typeof options.text !== "string") { options.text = options.text.toString(); } const { text, style, textureStyle } = options; const resolution = options.resolution ?? this._renderer.resolution; const { frame, canvasAndContext } = CanvasTextGenerator.CanvasTextGenerator.getCanvasAndContext({ text, style, resolution }); const texture = getPo2TextureFromSource.getPo2TextureFromSource(canvasAndContext.canvas, frame.width, frame.height, resolution); if (textureStyle) texture.source.style = textureStyle; if (style.trim) { frame.pad(style.padding); texture.frame.copyFrom(frame); texture.updateUvs(); } if (style.filters) { const filteredTexture = this._applyFilters(texture, style.filters); this.returnTexture(texture); CanvasTextGenerator.CanvasTextGenerator.returnCanvasAndContext(canvasAndContext); return filteredTexture; } this._renderer.texture.initSource(texture._source); CanvasTextGenerator.CanvasTextGenerator.returnCanvasAndContext(canvasAndContext); return texture; } /** * Returns a texture that was created wit the above `getTexture` function. * Handy if you are done with a texture and want to return it to the pool. * @param texture - The texture to be returned. */ returnTexture(texture) { const source = texture.source; source.resource = null; source.uploadMethodId = "unknown"; source.alphaMode = "no-premultiply-alpha"; TexturePool.TexturePool.returnTexture(texture, true); } /** * Renders text to its canvas, and updates its texture. * @deprecated since 8.10.0 */ renderTextToCanvas() { deprecation.deprecation( "8.10.0", "CanvasTextSystem.renderTextToCanvas: no longer supported, use CanvasTextSystem.getTexture instead" ); } /** * Applies the specified filters to the given texture. * * This method takes a texture and a list of filters, applies the filters to the texture, * and returns the resulting texture. It also ensures that the alpha mode of the resulting * texture is set to 'premultiplied-alpha'. * @param {Texture} texture - The texture to which the filters will be applied. * @param {Filter[]} filters - The filters to apply to the texture. * @returns {Texture} The resulting texture after all filters have been applied. */ _applyFilters(texture, filters) { const currentRenderTarget = this._renderer.renderTarget.renderTarget; const resultTexture = this._renderer.filter.generateFilteredTexture({ texture, filters }); this._renderer.renderTarget.bind(currentRenderTarget, false); return resultTexture; } destroy() { this._renderer = null; } } /** @ignore */ CanvasTextSystem.extension = { type: [ Extensions.ExtensionType.WebGLSystem, Extensions.ExtensionType.WebGPUSystem, Extensions.ExtensionType.CanvasSystem ], name: "canvasText" }; exports.CanvasTextSystem = CanvasTextSystem; //# sourceMappingURL=CanvasTextSystem.js.map