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">

1 lines 6.01 kB
{"version":3,"file":"FillPattern.mjs","sources":["../../../../../src/scene/graphics/shared/fill/FillPattern.ts"],"sourcesContent":["import { Matrix } from '../../../../maths/matrix/Matrix';\nimport { uid } from '../../../../utils/data/uid';\n\nimport type { WRAP_MODE } from '../../../../rendering/renderers/shared/texture/const';\nimport type { Texture } from '../../../../rendering/renderers/shared/texture/Texture';\n\n/**\n * Defines the repetition modes for fill patterns.\n *\n * - `repeat`: The pattern repeats in both directions.\n * - `repeat-x`: The pattern repeats horizontally only.\n * - `repeat-y`: The pattern repeats vertically only.\n * - `no-repeat`: The pattern does not repeat.\n * @category scene\n * @standard\n */\nexport type PatternRepetition = 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';\n\nconst repetitionMap = {\n repeat: {\n addressModeU: 'repeat',\n addressModeV: 'repeat',\n },\n 'repeat-x': {\n addressModeU: 'repeat',\n addressModeV: 'clamp-to-edge',\n },\n 'repeat-y': {\n addressModeU: 'clamp-to-edge',\n addressModeV: 'repeat',\n },\n 'no-repeat': {\n addressModeU: 'clamp-to-edge',\n addressModeV: 'clamp-to-edge',\n },\n};\n\n/**\n * A class that represents a fill pattern for use in Text and Graphics fills.\n * It allows for textures to be used as patterns, with optional repetition modes.\n * @category scene\n * @standard\n * @example\n * const txt = await Assets.load('https://pixijs.com/assets/bg_scene_rotate.jpg');\n * const pat = new FillPattern(txt, 'repeat');\n *\n * const textPattern = new Text({\n * text: 'PixiJS',\n * style: {\n * fontSize: 36,\n * fill: 0xffffff,\n * stroke: { fill: pat, width: 10 },\n * },\n * });\n *\n * textPattern.y = (textGradient.height);\n */\nexport class FillPattern implements CanvasPattern\n{\n /**\n * unique id for this fill pattern\n * @internal\n */\n public readonly uid: number = uid('fillPattern');\n /**\n * Internal tick counter to track changes in the pattern.\n * This is used to invalidate the pattern when the texture or transform changes.\n * @internal\n */\n public _tick: number = 0;\n /** @internal */\n public _texture: Texture;\n /** The transform matrix applied to the pattern */\n public transform = new Matrix();\n\n constructor(texture: Texture, repetition?: PatternRepetition)\n {\n this.texture = texture;\n\n this.transform.scale(\n 1 / texture.frame.width,\n 1 / texture.frame.height\n );\n\n if (repetition)\n {\n texture.source.style.addressModeU = repetitionMap[repetition].addressModeU as WRAP_MODE;\n texture.source.style.addressModeV = repetitionMap[repetition].addressModeV as WRAP_MODE;\n }\n }\n\n /**\n * Sets the transform for the pattern\n * @param transform - The transform matrix to apply to the pattern.\n * If not provided, the pattern will use the default transform.\n */\n public setTransform(transform?: Matrix): void\n {\n const texture = this.texture;\n\n this.transform.copyFrom(transform);\n this.transform.invert();\n // transform.scale\n this.transform.scale(\n 1 / texture.frame.width,\n 1 / texture.frame.height\n );\n\n this._tick++;\n }\n\n /** Internal texture used to render the gradient */\n public get texture()\n {\n return this._texture;\n }\n public set texture(value: Texture)\n {\n if (this._texture === value) return;\n\n this._texture = value;\n this._tick++;\n }\n\n /**\n * Returns a unique key for this instance.\n * This key is used for caching.\n * @returns {string} Unique key for the instance\n */\n public get styleKey(): string\n {\n return `fill-pattern-${this.uid}-${this._tick}`;\n }\n\n /** Destroys the fill pattern, releasing resources. This will also destroy the internal texture. */\n public destroy(): void\n {\n this.texture.destroy(true);\n this.texture = null;\n }\n}\n"],"names":[],"mappings":";;;;AAkBA,MAAM,aAAA,GAAgB;AAAA,EAClB,MAAA,EAAQ;AAAA,IACJ,YAAA,EAAc,QAAA;AAAA,IACd,YAAA,EAAc;AAAA,GAClB;AAAA,EACA,UAAA,EAAY;AAAA,IACR,YAAA,EAAc,QAAA;AAAA,IACd,YAAA,EAAc;AAAA,GAClB;AAAA,EACA,UAAA,EAAY;AAAA,IACR,YAAA,EAAc,eAAA;AAAA,IACd,YAAA,EAAc;AAAA,GAClB;AAAA,EACA,WAA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