- 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
import { TilingSprite } from '@pixi/sprite-tiling';
import { canvasUtils } from '@pixi/canvas-renderer';
import { CanvasRenderTarget } from '@pixi/utils';
import { Matrix, Point } from '@pixi/math';
import type { CanvasRenderer } from '@pixi/canvas-renderer';
const worldMatrix = new Matrix();
const patternMatrix = new Matrix();
const patternRect = [new Point(), new Point(), new Point(), new Point()];
/**
* Renders the object using the Canvas renderer
* @protected
* @function _renderCanvas
* @memberof PIXI.TilingSprite#
* @param {PIXI.CanvasRenderer} renderer - a reference to the canvas renderer
*/
TilingSprite.prototype._renderCanvas = function _renderCanvas(renderer: CanvasRenderer): void
{
const texture = this._texture;
if (!texture.baseTexture.valid)
{
return;
}
const context = renderer.context;
const transform = this.worldTransform;
const baseTexture = texture.baseTexture;
const source = baseTexture.getDrawableSource();
const baseTextureResolution = baseTexture.resolution;
// create a nice shiny pattern!
if (this._textureID !== this._texture._updateID || this._cachedTint !== this.tint)
{
this._textureID = this._texture._updateID;
// cut an object from a spritesheet..
const tempCanvas = new CanvasRenderTarget(texture._frame.width,
texture._frame.height,
baseTextureResolution);
// Tint the tiling sprite
if (this.tint !== 0xFFFFFF)
{
this._tintedCanvas = canvasUtils.getTintedCanvas(this, this.tint) as HTMLCanvasElement;
tempCanvas.context.drawImage(this._tintedCanvas, 0, 0);
}
else
{
tempCanvas.context.drawImage(source,
-texture._frame.x * baseTextureResolution, -texture._frame.y * baseTextureResolution);
}
this._cachedTint = this.tint;
this._canvasPattern = tempCanvas.context.createPattern(tempCanvas.canvas, 'repeat');
}
// set context state..
context.globalAlpha = this.worldAlpha;
renderer.setBlendMode(this.blendMode);
this.tileTransform.updateLocalTransform();
const lt = this.tileTransform.localTransform;
const W = this._width;
const H = this._height;
/*
* # Implementation Notes
*
* The tiling transform is not simply a transform on the tiling sprite's local space. If that
* were, the bounds of the tiling sprite would change. Rather the tile transform is a transform
* on the "pattern" coordinates each vertex is assigned.
*
* To implement the `tileTransform`, we issue drawing commands in the pattern's own space, which
* is defined as:
*
* Pattern_Space = Local_Space x inverse(tileTransform)
*
* In other words,
* Local_Space = Pattern_Space x tileTransform
*
* We draw the pattern in pattern space, because the space we draw in defines the pattern's coordinates.
* In other words, the pattern will always "originate" from (0, 0) in the space we draw in.
*
* This technique is equivalent to drawing a pattern texture, and then finding a quadrilateral that becomes
* the tiling sprite's local bounds under the tileTransform and mapping that onto the screen.
*
* ## uvRespectAnchor
*
* The preceding paragraph discusses the case without considering `uvRespectAnchor`. The `uvRespectAnchor` flags
* where the origin of the pattern space is. Assuming the tileTransform includes no translation, without
* loss of generality: If uvRespectAnchor = true, then
*
* Local Space (0, 0) <--> Pattern Space (0, 0) (where <--> means "maps to")
*
* Here the mapping is provided by trivially by the tileTransform (note tileTransform includes no translation. That
* means the invariant under all other transforms are the origins)
*
* Otherwise,
*
* Local Space (-localBounds.x, -localBounds.y) <--> Pattern Space (0, 0)
*
* Here the mapping is provided by the tileTransfrom PLUS some "shift". This shift is done POST-tileTransform. The shift
* is equal to the position of the top-left corner of the tiling sprite in its local space.
*
* Hence,
*
* Local_Space = Pattern_Space x tileTransform x shiftTransform
*/
// worldMatrix is used to convert from pattern space to world space.
//
// worldMatrix = tileTransform x shiftTransform x worldTransfrom
// = patternMatrix x worldTransform
worldMatrix.identity();
// patternMatrix is used to convert from pattern space to local space. The drawing commands are issued in pattern space
// and this matrix is used to inverse-map the local space vertices into it.
//
// patternMatrix = tileTransfrom x shiftTransform
patternMatrix.copyFrom(lt);
// Apply shiftTransform into patternMatrix. See $1.1
if (!this.uvRespectAnchor)
{
patternMatrix.translate(-this.anchor.x * W, -this.anchor.y * H);
}
patternMatrix.scale(this.tileScale.x / baseTextureResolution, this.tileScale.y / baseTextureResolution);
worldMatrix.prepend(patternMatrix);
worldMatrix.prepend(transform);
renderer.setContextTransform(worldMatrix);
// Fill the pattern!
context.fillStyle = this._canvasPattern;
// The position in local space we are drawing the rectangle: (lx, ly, lx + W, ly + H)
const lx = this.anchor.x * -W;
const ly = this.anchor.y * -H;
// Set pattern rect in local space first.
patternRect[0].set(lx, ly);
patternRect[1].set(lx + W, ly);
patternRect[2].set(lx + W, ly + H);
patternRect[3].set(lx, ly + H);
// Map patternRect into pattern space.
for (let i = 0; i < 4; i++)
{
patternMatrix.applyInverse(patternRect[i], patternRect[i]);
}
/*
* # Note about verification of theory
*
* As discussed in the implementation notes, you can verify that `patternRect[0]` will always be (0, 0) in case of
* `uvRespectAnchor` false and tileTransform having no translation. Indeed, because the pattern origin should map
* to the top-left corner of the tiling sprite in its local space.
*/
context.beginPath();
context.moveTo(patternRect[0].x, patternRect[0].y);
for (let i = 1; i < 4; i++)
{
context.lineTo(patternRect[i].x, patternRect[i].y);
}
context.closePath();
context.fill();
};