From 724ed7ec17c6e149e29950374302035771b448f5 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 20 Feb 2024 23:53:26 +0000 Subject: [PATCH] `Texture#getFrameBounds` is a new method that will return the bounds that all of the frames of a given Texture Source encompass. This is useful for things like calculating the bounds of a Sprite Sheet embedded within a Texture Atlas. --- src/textures/Texture.js | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/textures/Texture.js b/src/textures/Texture.js index 52b5b2793..f9ef78081 100644 --- a/src/textures/Texture.js +++ b/src/textures/Texture.js @@ -316,6 +316,59 @@ var Texture = new Class({ return out; }, + /** + * Based on the given Texture Source Index, this method will get all of the Frames using + * that source and then work out the bounds that they encompass, returning them in an object. + * + * This is useful if this Texture is, for example, a sprite sheet within an Atlas, and you + * need to know the total bounds of the sprite sheet. + * + * @method Phaser.Textures.Texture#getFrameBounds + * @since 3.80.0 + * + * @param {number} sourceIndex - The index of the TextureSource to get the Frame bounds from. + * + * @return {Phaser.Types.Math.RectangleLike} An object containing the bounds of the Frames using the given Texture Source Index. + */ + getFrameBounds: function (sourceIndex) + { + if (sourceIndex === undefined) { sourceIndex = 0; } + + var frames = this.getFramesFromTextureSource(sourceIndex); + + var minX = Infinity; + var minY = Infinity; + var maxX = 0; + var maxY = 0; + + for (var i = 0; i < frames.length; i++) + { + var frame = frames[i]; + + if (frame.cutX < minX) + { + minX = frame.cutX; + } + + if (frame.cutY < minY) + { + minY = frame.cutY; + } + + if (frame.cutX + frame.cutWidth > maxX) + { + maxX = frame.cutX + frame.cutWidth; + } + + if (frame.cutY + frame.cutHeight > maxY) + { + maxY = frame.cutY + frame.cutHeight; + } + } + + return { x: minX, y: minY, width: maxX - minX, height: maxY - minY }; + }, + /** * Returns an array with all of the names of the Frames in this Texture. *