- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
import { Texture, TextureMatrix } from '@pixi/core';
import { Point, Rectangle, Transform } from '@pixi/math';
import { Sprite } from '@pixi/sprite';
import type { Renderer, IBaseTextureOptions, TextureSource } from '@pixi/core';
import type { IDestroyOptions } from '@pixi/display';
import type { IPoint, IPointData, ISize, ObservablePoint } from '@pixi/math';
const tempPoint = new Point();
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TilingSprite extends GlobalMixins.TilingSprite {}
/**
* A tiling sprite is a fast way of rendering a tiling image.
* @memberof PIXI
*/
export class TilingSprite extends Sprite
{
/** Tile transform */
public tileTransform: Transform;
/** Matrix that is applied to UV to get the coords in Texture normalized space to coords in BaseTexture space. */
public uvMatrix: TextureMatrix;
/**
* Flags whether the tiling pattern should originate from the origin instead of the top-left corner in
* local space.
*
* This will make the texture coordinates assigned to each vertex dependent on the value of the anchor. Without
* this, the top-left corner always gets the (0, 0) texture coordinate.
* @default false
*/
public uvRespectAnchor: boolean;
/**
* @param texture - The texture of the tiling sprite.
* @param width - The width of the tiling sprite.
* @param height - The height of the tiling sprite.
*/
constructor(texture: Texture, width = 100, height = 100)
{
super(texture);
this.tileTransform = new Transform();
// The width of the tiling sprite
this._width = width;
// The height of the tiling sprite
this._height = height;
this.uvMatrix = this.texture.uvMatrix || new TextureMatrix(texture);
/**
* Plugin that is responsible for rendering this element.
* Allows to customize the rendering process without overriding '_render' method.
* @default 'tilingSprite'
*/
this.pluginName = 'tilingSprite';
this.uvRespectAnchor = false;
}
/**
* Changes frame clamping in corresponding textureTransform, shortcut
* Change to -0.5 to add a pixel to the edge, recommended for transparent trimmed textures in atlas
* @default 0.5
* @member {number}
*/
get clampMargin(): number
{
return this.uvMatrix.clampMargin;
}
set clampMargin(value: number)
{
this.uvMatrix.clampMargin = value;
this.uvMatrix.update(true);
}
/** The scaling of the image that is being tiled. */
get tileScale(): ObservablePoint
{
return this.tileTransform.scale;
}
set tileScale(value: IPointData)
{
this.tileTransform.scale.copyFrom(value as IPoint);
}
/** The offset of the image that is being tiled. */
get tilePosition(): ObservablePoint
{
return this.tileTransform.position;
}
set tilePosition(value: ObservablePoint)
{
this.tileTransform.position.copyFrom(value as IPoint);
}
/**
* @protected
*/
protected _onTextureUpdate(): void
{
if (this.uvMatrix)
{
this.uvMatrix.texture = this._texture;
}
this._cachedTint = 0xFFFFFF;
}
/**
* Renders the object using the WebGL renderer
* @param renderer - The renderer
*/
protected _render(renderer: Renderer): void
{
// tweak our texture temporarily..
const texture = this._texture;
if (!texture || !texture.valid)
{
return;
}
this.tileTransform.updateLocalTransform();
this.uvMatrix.update();
renderer.batch.setObjectRenderer(renderer.plugins[this.pluginName]);
renderer.plugins[this.pluginName].render(this);
}
/** Updates the bounds of the tiling sprite. */
protected _calculateBounds(): void
{
const minX = this._width * -this._anchor._x;
const minY = this._height * -this._anchor._y;
const maxX = this._width * (1 - this._anchor._x);
const maxY = this._height * (1 - this._anchor._y);
this._bounds.addFrame(this.transform, minX, minY, maxX, maxY);
}
/**
* Gets the local bounds of the sprite object.
* @param rect - Optional output rectangle.
* @returns The bounds.
*/
public getLocalBounds(rect?: Rectangle): Rectangle
{
// we can do a fast local bounds if the sprite has no children!
if (this.children.length === 0)
{
this._bounds.minX = this._width * -this._anchor._x;
this._bounds.minY = this._height * -this._anchor._y;
this._bounds.maxX = this._width * (1 - this._anchor._x);
this._bounds.maxY = this._height * (1 - this._anchor._y);
if (!rect)
{
if (!this._localBoundsRect)
{
this._localBoundsRect = new Rectangle();
}
rect = this._localBoundsRect;
}
return this._bounds.getRectangle(rect);
}
return super.getLocalBounds.call(this, rect);
}
/**
* Checks if a point is inside this tiling sprite.
* @param point - The point to check.
* @returns Whether or not the sprite contains the point.
*/
public containsPoint(point: IPointData): boolean
{
this.worldTransform.applyInverse(point, tempPoint);
const width = this._width;
const height = this._height;
const x1 = -width * this.anchor._x;
if (tempPoint.x >= x1 && tempPoint.x < x1 + width)
{
const y1 = -height * this.anchor._y;
if (tempPoint.y >= y1 && tempPoint.y < y1 + height)
{
return true;
}
}
return false;
}
/**
* Destroys this sprite and optionally its texture and children
* @param {object|boolean} [options] - Options parameter. A boolean will act as if all options
* have been set to that value
* @param {boolean} [options.children=false] - if set to true, all the children will have their destroy
* method called as well. 'options' will be passed on to those calls.
* @param {boolean} [options.texture=false] - Should it destroy the current texture of the sprite as well
* @param {boolean} [options.baseTexture=false] - Should it destroy the base texture of the sprite as well
*/
public destroy(options?: IDestroyOptions | boolean): void
{
super.destroy(options);
this.tileTransform = null;
this.uvMatrix = null;
}
/**
* Helper function that creates a new tiling sprite based on the source you provide.
* The source can be - frame id, image url, video url, canvas element, video element, base texture
* @static
* @param {string|PIXI.Texture|HTMLCanvasElement|HTMLVideoElement} source - Source to create texture from
* @param {object} options - See {@link PIXI.BaseTexture}'s constructor for options.
* @param {number} options.width - required width of the tiling sprite
* @param {number} options.height - required height of the tiling sprite
* @returns {PIXI.TilingSprite} The newly created texture
*/
static from(source: TextureSource | Texture, options: ISize & IBaseTextureOptions): TilingSprite
{
const texture = (source instanceof Texture)
? source
: Texture.from(source, options);
return new TilingSprite(
texture,
options.width,
options.height
);
}
/** The width of the sprite, setting this will actually modify the scale to achieve the value set. */
get width(): number
{
return this._width;
}
set width(value: number)
{
this._width = value;
}
/** The height of the TilingSprite, setting this will actually modify the scale to achieve the value set. */
get height(): number
{
return this._height;
}
set height(value: number)
{
this._height = value;
}
}