Newer
Older
pixi.js / packages / canvas / canvas-sprite-tiling / src / TilingSprite.js
import { TilingSprite } from '@pixi/sprite-tiling';
import { CanvasTinter } from '@pixi/canvas-sprite';
import { CanvasRenderTarget } from '@pixi/utils';

/**
 * Renders the object using the Canvas renderer
 *
 * @private
 * @param {PIXI.CanvasRenderer} renderer - a reference to the canvas renderer
 */
TilingSprite.prototype._renderCanvas = function _renderCanvas(renderer)
{
    const texture = this._texture;

    if (!texture.baseTexture.hasLoaded)
    {
        return;
    }

    const context = renderer.context;
    const transform = this.worldTransform;
    const resolution = renderer.resolution;
    const baseTexture = texture.baseTexture;
    const baseTextureResolution = baseTexture.resolution;
    const modX = ((this.tilePosition.x / this.tileScale.x) % texture._frame.width) * baseTextureResolution;
    const modY = ((this.tilePosition.y / this.tileScale.y) % texture._frame.height) * baseTextureResolution;

    // 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.tintedTexture = CanvasTinter.getTintedTexture(this, this.tint);
            tempCanvas.context.drawImage(this.tintedTexture, 0, 0);
        }
        else
        {
            tempCanvas.context.drawImage(baseTexture.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;
    context.setTransform(transform.a * resolution,
        transform.b * resolution,
        transform.c * resolution,
        transform.d * resolution,
        transform.tx * resolution,
        transform.ty * resolution);

    renderer.setBlendMode(this.blendMode);

    // fill the pattern!
    context.fillStyle = this._canvasPattern;

    // TODO - this should be rolled into the setTransform above..
    context.scale(this.tileScale.x / baseTextureResolution, this.tileScale.y / baseTextureResolution);

    const anchorX = this.anchor.x * -this._width;
    const anchorY = this.anchor.y * -this._height;

    if (this.uvRespectAnchor)
    {
        context.translate(modX, modY);

        context.fillRect(-modX + anchorX, -modY + anchorY,
            this._width / this.tileScale.x * baseTextureResolution,
            this._height / this.tileScale.y * baseTextureResolution);
    }
    else
    {
        context.translate(modX + anchorX, modY + anchorY);

        context.fillRect(-modX, -modY,
            this._width / this.tileScale.x * baseTextureResolution,
            this._height / this.tileScale.y * baseTextureResolution);
    }
};