Files

1 line
23 KiB
Plaintext
Raw Permalink Normal View History

2025-01-04 00:34:03 +01:00
{"version":3,"file":"HTMLText.mjs","sources":["../src/HTMLText.ts"],"sourcesContent":["import { Rectangle, settings, Texture, utils } from '@pixi/core';\nimport { Sprite } from '@pixi/sprite';\nimport { TextStyle } from '@pixi/text';\nimport { HTMLTextStyle } from './HTMLTextStyle';\n\nimport type { ImageResource, IRenderer, ISize, Renderer } from '@pixi/core';\nimport type { IDestroyOptions } from '@pixi/display';\nimport type { ITextStyle } from '@pixi/text';\n\n/**\n * Alternative to {@link PIXI.Text|Text} but supports multi-style HTML text. There are\n * few key differences between this and {@link PIXI.Text|Text}:\n * <br>&bull; HTMLText not support {@link https://caniuse.com/mdn-svg_elements_foreignobject|Internet Explorer}.\n * <br>&bull; Rendering is text asynchronous. If statically rendering, listen to `update` event on BaseTexture.\n * <br>&bull; Does not support all style options (e.g., `lineJoin`, `leading`, `textBaseline`, `trim`, `miterLimit`,\n * `fillGradientStops`, `fillGradientType`)\n * @example\n * import { HTMLText } from 'pixi.js';\n *\n * const text = new HTMLText(\"Hello <b>World</b>\", { fontSize: 20 });\n *\n * text.texture.baseTexture.on('update', () => {\n * console.log('Text is redrawn!');\n * });\n * @class\n * @memberof PIXI\n * @extends PIXI.Sprite\n * @since 7.2.0\n */\nexport class HTMLText extends Sprite\n{\n /**\n * Default opens when destroying.\n * @type {PIXI.IDestroyOptions}\n * @property {boolean} [texture=true] - Whether to destroy the texture.\n * @property {boolean} [children=false] - Whether to destroy the children.\n * @property {boolean} [baseTexture=true] - Whether to destroy the base texture.\n */\n public static defaultDestroyOptions: IDestroyOptions = {\n texture: true,\n children: false,\n baseTexture: true,\n };\n\n /** Default maxWidth, set at construction */\n public static defaultMaxWidth = 2024;\n\n /** Default maxHeight, set at construction */\n public static defaultMaxHeight = 2024;\n\n /** Default resolution, make sure autoResolution or defaultAutoResolution is `false`. */\n public static defaultResolution: number | undefined;\n\n /** Default autoResolution for all HTMLText objects */\n public static defaultAutoResolution = true;\n\n /** The maximum width in rendered pixels that the content can be, any larger will be hidden */\n public maxWidth: number;\n\n /** The maximum height in rendered pixels that the content can be, any larger will be hidden */\n public maxHeight: number;\n\n private _domElement: HTMLElement;\n private _styleElement: HTMLElement;\n private _svgRoot: SVGSVGElement;\n private _foreignObject: SVGForeignObjectElement;\n private _image: HTMLImageElement;\n private _loadImage: HTMLImageElement;\n private _resolution: number;\n private _text: string | null = null;\n private _style: HTMLTextStyle | null = null;\n private _autoResolution = true;\n private localStyleID = -1;\n private dirty = false;\n private _updateID = 0;\n\n /** The HTMLTextStyle object is owned by this instance */\n private ownsStyle = false;\n\n /**\n * @param {string} [text] - Text contents\n * @param {PIXI.HTMLTextStyle|PIXI.TextStyle|PIXI.ITextStyle} [style] - Style setting to use.\n * Strongly recommend using an HTMLTextStyle object. Providing a PIXI.TextStyle\n * will convert the TextStyle to an HTMLTextStyle and will no longer be linked.\n */\n constructor(text = '', style: HTMLTextStyle | TextStyle | Partial<ITextStyle> = {})\n {\n super(Texture.EMPTY);\n\n const image = new Image();\n const texture = Texture.from<ImageResource>(image, {\n scaleMode: settings.SCALE_MODE,\n resourceOptions: {\n autoLoad: false,\n },\n });\n\n texture.orig = new Rectangle();\n texture.trim = new Rectangle();\n\n this.texture = texture;\n\n const nssvg = 'http://www.w3.org/2000/svg';\n const nsxhtml =