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">
114 lines (111 loc) • 4.08 kB
JavaScript
import { ExtensionType } from '../../../extensions/Extensions.mjs';
import { TexturePool } from '../../../rendering/renderers/shared/texture/TexturePool.mjs';
import { TextureStyle } from '../../../rendering/renderers/shared/texture/TextureStyle.mjs';
import { deprecation } from '../../../utils/logging/deprecation.mjs';
import { TextStyle } from '../TextStyle.mjs';
import { getPo2TextureFromSource } from '../utils/getPo2TextureFromSource.mjs';
import { CanvasTextGenerator } from './CanvasTextGenerator.mjs';
;
class CanvasTextSystem {
constructor(_renderer) {
this._renderer = _renderer;
}
getTexture(options, _resolution, _style, _textKey) {
if (typeof options === "string") {
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)) {
options.style = new TextStyle(options.style);
}
if (!(options.textureStyle instanceof TextureStyle)) {
options.textureStyle = new 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.getCanvasAndContext({
text,
style,
resolution
});
const texture = 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.returnCanvasAndContext(canvasAndContext);
return filteredTexture;
}
this._renderer.texture.initSource(texture._source);
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.returnTexture(texture, true);
}
/**
* Renders text to its canvas, and updates its texture.
* @deprecated since 8.10.0
*/
renderTextToCanvas() {
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: [
ExtensionType.WebGLSystem,
ExtensionType.WebGPUSystem,
ExtensionType.CanvasSystem
],
name: "canvasText"
};
export { CanvasTextSystem };
//# sourceMappingURL=CanvasTextSystem.mjs.map