/** * @author Mat Groves http://matgroves.com/ */ /** * A tiling sprite is a fast way of rendering a tiling image * * @class TilingSprite * @extends Sprite * @constructor * @param texture {Texture} the texture of the tiling sprite * @param width {Number} the width of the tiling sprite * @param height {Number} the height of the tiling sprite */ PIXI.TilingSprite = function(texture, width, height) { PIXI.Sprite.call( this, texture); /** * The with of the tiling sprite * * @property width * @type Number */ this.width = width || 100; /** * The height of the tiling sprite * * @property height * @type Number */ this.height = height || 100; /** * The scaling of the image that is being tiled * * @property tileScale * @type Point */ this.tileScale = new PIXI.Point(1,1); /** * A point that represents the scale of the texture object * * @property tileScaleOffset * @type Point */ this.tileScaleOffset = new PIXI.Point(1,1); /** * The offset position of the image that is being tiled * * @property tilePosition * @type Point */ this.tilePosition = new PIXI.Point(0,0); /** * Whether this sprite is renderable or not * * @property renderable * @type Boolean * @default true */ this.renderable = true; /** * The tint applied to the sprite. This is a hex value * * @property tint * @type Number * @default 0xFFFFFF */ this.tint = 0xFFFFFF; /** * The blend mode to be applied to the sprite * * @property blendMode * @type Number * @default PIXI.blendModes.NORMAL; */ this.blendMode = PIXI.blendModes.NORMAL; }; // constructor PIXI.TilingSprite.prototype = Object.create(PIXI.Sprite.prototype); PIXI.TilingSprite.prototype.constructor = PIXI.TilingSprite; /** * The width of the sprite, setting this will actually modify the scale to achieve the value set * * @property width * @type Number */ Object.defineProperty(PIXI.TilingSprite.prototype, 'width', { get: function() { return this._width; }, set: function(value) { this._width = value; } }); /** * The height of the TilingSprite, setting this will actually modify the scale to achieve the value set * * @property height * @type Number */ Object.defineProperty(PIXI.TilingSprite.prototype, 'height', { get: function() { return this._height; }, set: function(value) { this._height = value; } }); /** * When the texture is updated, this event will be fired to update the scale and frame * * @method onTextureUpdate * @param event * @private */ PIXI.TilingSprite.prototype.onTextureUpdate = function() { this.updateFrame = true; }; PIXI.TilingSprite.prototype.setTexture = function(texture) { if(this.texture === texture)return; this.texture = texture; this.refreshTexture = true; /* if(this.tilingTexture) { this.generateTilingTexture(true); } */ /* // stop current texture; if(this.texture.baseTexture !== texture.baseTexture) { this.textureChange = true; this.texture = texture; } else { this.texture = texture; } this.updateFrame = true;*/ this.cachedTint = 0xFFFFFF; }; /** * Renders the object using the WebGL renderer * * @method _renderWebGL * @param renderSession {RenderSession} * @private */ PIXI.TilingSprite.prototype._renderWebGL = function(renderSession) { if(this.visible === false || this.alpha === 0)return; var i,j; if(this.mask) { renderSession.spriteBatch.stop(); renderSession.maskManager.pushMask(this.mask, renderSession); renderSession.spriteBatch.start(); } if(this.filters) { renderSession.spriteBatch.flush(); renderSession.filterManager.pushFilter(this._filterBlock); } if(!this.tilingTexture || this.refreshTexture) { this.generateTilingTexture(true); if(this.tilingTexture && this.tilingTexture.needsUpdate) { //TODO - tweaking PIXI.updateWebGLTexture(this.tilingTexture.baseTexture, renderSession.gl); this.tilingTexture.needsUpdate = false; // this.tilingTexture._uvs = null; } } else renderSession.spriteBatch.renderTilingSprite(this); // simple render children! for(i=0,j=this.children.length; i maxX ? x1 : maxX; maxX = x2 > maxX ? x2 : maxX; maxX = x3 > maxX ? x3 : maxX; maxX = x4 > maxX ? x4 : maxX; maxY = y1 > maxY ? y1 : maxY; maxY = y2 > maxY ? y2 : maxY; maxY = y3 > maxY ? y3 : maxY; maxY = y4 > maxY ? y4 : maxY; var bounds = this._bounds; bounds.x = minX; bounds.width = maxX - minX; bounds.y = minY; bounds.height = maxY - minY; // store a reference so that if this function gets called again in the render cycle we do not have to recalculate this._currentBounds = bounds; return bounds; }; /** * * @method generateTilingTexture * * @param forcePowerOfTwo {Boolean} Whether we want to force the texture to be a power of two */ PIXI.TilingSprite.prototype.generateTilingTexture = function(forcePowerOfTwo) { var texture = this.texture; if(!texture.baseTexture.hasLoaded)return; var baseTexture = texture.baseTexture; var frame = texture.frame; var targetWidth, targetHeight; // check that the frame is the same size as the base texture. var isFrame = frame.width !== baseTexture.width || frame.height !== baseTexture.height; var newTextureRequired = false; if(!forcePowerOfTwo) { if(isFrame) { targetWidth = frame.width; targetHeight = frame.height; newTextureRequired = true; } } else { targetWidth = PIXI.getNextPowerOfTwo(frame.width); targetHeight = PIXI.getNextPowerOfTwo(frame.height); if(frame.width !== targetWidth && frame.height !== targetHeight)newTextureRequired = true; } if(newTextureRequired) { var canvasBuffer; if(this.tilingTexture && this.tilingTexture.isTiling) { canvasBuffer = this.tilingTexture.canvasBuffer; canvasBuffer.resize(targetWidth, targetHeight); this.tilingTexture.baseTexture.width = targetWidth; this.tilingTexture.baseTexture.height = targetHeight; this.tilingTexture.needsUpdate = true; } else { canvasBuffer = new PIXI.CanvasBuffer(targetWidth, targetHeight); this.tilingTexture = PIXI.Texture.fromCanvas(canvasBuffer.canvas); this.tilingTexture.canvasBuffer = canvasBuffer; this.tilingTexture.isTiling = true; } canvasBuffer.context.drawImage(texture.baseTexture.source, frame.x, frame.y, frame.width, frame.height, 0, 0, targetWidth, targetHeight); this.tileScaleOffset.x = frame.width / targetWidth; this.tileScaleOffset.y = frame.height / targetHeight; } else { //TODO - switching? if(this.tilingTexture && this.tilingTexture.isTiling) { // destroy the tiling texture! // TODO could store this somewhere? this.tilingTexture.destroy(true); } this.tileScaleOffset.x = 1; this.tileScaleOffset.y = 1; this.tilingTexture = texture; } this.refreshTexture = false; this.tilingTexture.baseTexture._powerOf2 = true; };