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 13.3 kB
{"version":3,"file":"NineSliceGeometry.mjs","sources":["../../../src/scene/sprite-nine-slice/NineSliceGeometry.ts"],"sourcesContent":["import { type PointData } from '../../maths/point/PointData';\nimport { PlaneGeometry } from '../mesh-plane/PlaneGeometry';\n\n/**\n * Options for the NineSliceGeometry.\n * @category scene\n * @advanced\n */\nexport interface NineSliceGeometryOptions\n{\n\n /** The width of the NineSlicePlane, setting this will actually modify the vertices and UV's of this plane. */\n width?: number\n /** The height of the NineSlicePlane, setting this will actually modify the vertices and UV's of this plane. */\n height?: number\n /** The original width of the texture */\n originalWidth?: number\n /** The original height of the texture */\n originalHeight?: number\n /** The width of the left column. */\n leftWidth?: number\n /** The height of the top row. */\n topHeight?: number\n /** The width of the right column. */\n rightWidth?: number\n /** The height of the bottom row. */\n bottomHeight?: number\n\n /** The anchor point of the NineSliceSprite. */\n anchor?: PointData\n\n /**\n * The trim rectangle of the texture, describing the offset and size of the visible\n * pixel area within the original (unpadded) frame. When provided, UV coordinates are\n * clamped to the trimmed region so that transparent padding in the atlas does not\n * bleed into the rendered corners/edges.\n * @default null\n */\n trim?: { x: number; y: number; width: number; height: number } | null\n}\n\n/**\n * The NineSliceGeometry class allows you to create a NineSlicePlane object.\n * @category scene\n * @advanced\n */\nexport class NineSliceGeometry extends PlaneGeometry\n{\n /** The default options for the NineSliceGeometry. */\n public static defaultOptions: NineSliceGeometryOptions = {\n /** The width of the NineSlicePlane, setting this will actually modify the vertices and UV's of this plane. */\n width: 100,\n /** The height of the NineSlicePlane, setting this will actually modify the vertices and UV's of this plane. */\n height: 100,\n /** The width of the left column. */\n leftWidth: 10,\n /** The height of the top row. */\n topHeight: 10,\n /** The width of the right column. */\n rightWidth: 10,\n /** The height of the bottom row. */\n bottomHeight: 10,\n\n /** The original width of the texture */\n originalWidth: 100,\n /** The original height of the texture */\n originalHeight: 100,\n };\n\n /** @internal */\n public _leftWidth: number;\n /** @internal */\n public _rightWidth: number;\n /** @internal */\n public _topHeight: number;\n /** @internal */\n public _bottomHeight: number;\n\n private _originalWidth: number;\n private _originalHeight: number;\n private _trimX: number;\n private _trimY: number;\n private _trimWidth: number;\n private _trimHeight: number;\n private _anchorX: any;\n private _anchorY: number;\n\n constructor(options: NineSliceGeometryOptions = {})\n {\n options = { ...NineSliceGeometry.defaultOptions, ...options };\n\n super({\n width: options.width,\n height: options.height,\n verticesX: 4,\n verticesY: 4,\n });\n\n // Initialise trim fields before update() so updateUvs() has valid values\n this._trimX = 0;\n this._trimY = 0;\n this._trimWidth = options.originalWidth ?? NineSliceGeometry.defaultOptions.originalWidth;\n this._trimHeight = options.originalHeight ?? NineSliceGeometry.defaultOptions.originalHeight;\n\n this.update(options);\n }\n\n /**\n * Updates the NineSliceGeometry with the options.\n * @param options - The options of the NineSliceGeometry.\n */\n public update(options: NineSliceGeometryOptions)\n {\n this.width = options.width ?? this.width;\n this.height = options.height ?? this.height;\n this._originalWidth = options.originalWidth ?? this._originalWidth;\n this._originalHeight = options.originalHeight ?? this._originalHeight;\n this._leftWidth = options.leftWidth ?? this._leftWidth;\n this._rightWidth = options.rightWidth ?? this._rightWidth;\n this._topHeight = options.topHeight ?? this._topHeight;\n this._bottomHeight = options.bottomHeight ?? this._bottomHeight;\n\n this._anchorX = options.anchor?.x;\n this._anchorY = options.anchor?.y;\n\n // Update trim values whenever they are explicitly provided (including null to reset)\n if (options.trim !== undefined)\n {\n this._trimX = options.trim?.x ?? 0;\n this._trimY = options.trim?.y ?? 0;\n this._trimWidth = options.trim?.width ?? this._originalWidth;\n this._trimHeight = options.trim?.height ?? this._originalHeight;\n }\n else\n {\n this._trimWidth = this._originalWidth;\n this._trimHeight = this._originalHeight;\n }\n\n this.updateUvs();\n this.updatePositions();\n }\n\n /** Updates the positions of the vertices. */\n public updatePositions()\n {\n const p = this.positions;\n const {\n width,\n height,\n _leftWidth,\n _rightWidth,\n _topHeight,\n _bottomHeight,\n _anchorX,\n _anchorY,\n } = this;\n\n const w = _leftWidth + _rightWidth;\n const scaleW = width > w ? 1.0 : width / w;\n\n const h = _topHeight + _bottomHeight;\n const scaleH = height > h ? 1.0 : height / h;\n\n const scale = Math.min(scaleW, scaleH);\n\n const anchorOffsetX = _anchorX * width;\n const anchorOffsetY = _anchorY * height;\n\n p[0] = p[8] = p[16] = p[24] = -anchorOffsetX;\n p[2] = p[10] = p[18] = p[26] = (_leftWidth * scale) - anchorOffsetX;\n p[4] = p[12] = p[20] = p[28] = width - (_rightWidth * scale) - anchorOffsetX;\n p[6] = p[14] = p[22] = p[30] = width - anchorOffsetX;\n\n p[1] = p[3] = p[5] = p[7] = -anchorOffsetY;\n p[9] = p[11] = p[13] = p[15] = (_topHeight * scale) - anchorOffsetY;\n p[17] = p[19] = p[21] = p[23] = height - (_bottomHeight * scale) - anchorOffsetY;\n p[25] = p[27] = p[29] = p[31] = height - anchorOffsetY;\n\n this.getBuffer('aPosition').update();\n }\n\n /** Updates the UVs of the vertices. */\n public updateUvs()\n {\n const uvs = this.uvs;\n\n const origW = this._originalWidth;\n const origH = this._originalHeight;\n\n // Compute the UV bounds for the trimmed region within the original texture space.\n // When the texture has no trim, these default to [0, 1] (the full orig area).\n // When the texture is trimmed, UV coordinates are offset so that only the visible\n // pixel area is sampled, preventing transparent padding from bleeding into corners.\n const u0 = this._trimX / origW;\n const v0 = this._trimY / origH;\n const u1 = (this._trimX + this._trimWidth) / origW;\n const v1 = (this._trimY + this._trimHeight) / origH;\n\n uvs[0] = uvs[8] = uvs[16] = uvs[24] = u0;\n uvs[1] = uvs[3] = uvs[5] = uvs[7] = v0;\n\n uvs[6] = uvs[14] = uvs[22] = uvs[30] = u1;\n uvs[25] = uvs[27] = uvs[29] = uvs[31] = v1;\n\n const _uvw = 1.0 / origW;\n const _uvh = 1.0 / origH;\n\n uvs[2] = uvs[10] = uvs[18] = uvs[26] = u0 + (_uvw * this._leftWidth);\n uvs[9] = uvs[11] = uvs[13] = uvs[15] = v0 + (_uvh * this._topHeight);\n\n uvs[4] = uvs[12] = uvs[20] = uvs[28] = u1 - (_uvw * this._rightWidth);\n uvs[17] = uvs[19] = uvs[21] = uvs[23] = v1 - (_uvh * this._bottomHeight);\n\n this.getBuffer('aUV').update();\n }\n}\n\n"],"names":[],"mappings":";;;AA8CO,MAAM,kBAAA,GAAN,MAAM,kBAAA,SAA0B,aAAA,CACvC;AAAA,EAwCI,WAAA,CAAY,OAAA,GAAoC,EAAC,EACjD;AACI,IAAA,OAAA,GAAU,EAAE,GAAG,kBAAA,CAAkB,cAAA,EAAgB,GAAG,OAAA,EAAQ;AAE5D,IAAA,KAAA,CAAM;AAAA,MACF,OAAO,OAAA,CAAQ,KAAA;AAAA,MACf,QAAQ,OAAA,CAAQ,MAAA;AAAA,MAChB,SAAA,EAAW,CAAA;AAAA,MACX,SAAA,EAAW;AAAA,KACd,CAAA;AAGD,IAAA,IAAA,CAAK,MAAA,GAAS,CAAA;AACd,IAAA,IAAA,CAAK,MAAA,GAAS,CAAA;AACd,IAAA,IAAA,CAAK,UAAA,GAAa,OAAA,CAAQ,aAAA,IAAiB,kBAAA,CAAkB,cAAA,CAAe,aAAA;AAC5E,IAAA,IAAA,CAAK,WAAA,GAAc,OAAA,CAAQ,cAAA,IAAkB,kBAAA,CAAkB,cAAA,CAAe,cAAA;AAE9E,IAAA,IAAA,CAAK,OAAO,OAAO,CAAA;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,OAAO,OAAA,EACd;AACI,IAAA,IAAA,CAAK,KAAA,GAAQ,OAAA,CAAQ,KAAA,IAAS,IAAA,CAAK,KAAA;AACnC,IAAA,IAAA,CAAK,MAAA,GAAS,OAAA,CAAQ,MAAA,IAAU,IAAA,CAAK,MAAA;AACrC,IAAA,IAAA,CAAK,cAAA,GAAiB,O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