/** * @author Mat Groves http://matgroves.com/ @Doormat23 */ PIXI.BaseTextureCache = {}; PIXI.texturesToUpdate = []; PIXI.texturesToDestroy = []; PIXI.BaseTextureCacheIdGenerator = 0; /** * A texture stores the information that represents an image. All textures have a base texture * * @class BaseTexture * @uses EventTarget * @constructor * @param source {String} the source object (image or canvas) * @param scaleMode {Number} Should be one of the PIXI.scaleMode consts */ PIXI.BaseTexture = function(source, scaleMode) { PIXI.EventTarget.call( this ); /** * [read-only] The width of the base texture set when the image has loaded * * @property width * @type Number * @readOnly */ this.width = 100; /** * [read-only] The height of the base texture set when the image has loaded * * @property height * @type Number * @readOnly */ this.height = 100; /** * The scale mode to apply when scaling this texture * @property scaleMode * @type PIXI.scaleModes * @default PIXI.scaleModes.LINEAR */ this.scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; /** * [read-only] Describes if the base texture has loaded or not * * @property hasLoaded * @type Boolean * @readOnly */ this.hasLoaded = false; /** * The source that is loaded to create the texture * * @property source * @type Image */ this.source = source; if(!source)return; if(this.source.complete || this.source.getContext) { this.hasLoaded = true; this.width = this.source.width; this.height = this.source.height; PIXI.texturesToUpdate.push(this); } else { var scope = this; this.source.onload = function() { scope.hasLoaded = true; scope.width = scope.source.width; scope.height = scope.source.height; // add it to somewhere... PIXI.texturesToUpdate.push(scope); scope.dispatchEvent( { type: 'loaded', content: scope } ); }; } this.imageUrl = null; this._powerOf2 = false; //TODO will be used for futer pixi 1.5... this.id = PIXI.BaseTextureCacheIdGenerator++; // used for webGL this._glTextures = []; }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; /** * Destroys this base texture * * @method destroy */ PIXI.BaseTexture.prototype.destroy = function() { if(this.imageUrl) { delete PIXI.BaseTextureCache[this.imageUrl]; this.imageUrl = null; this.source.src = null; } this.source = null; PIXI.texturesToDestroy.push(this); }; /** * Changes the source image of the texture * * @method updateSourceImage * @param newSrc {String} the path of the image */ PIXI.BaseTexture.prototype.updateSourceImage = function(newSrc) { this.hasLoaded = false; this.source.src = null; this.source.src = newSrc; }; /** * Helper function that returns a base texture based on an image url * If the image is not in the base texture cache it will be created and loaded * * @static * @method fromImage * @param imageUrl {String} The image url of the texture * @param crossorigin {Boolean} * @param scaleMode {Number} Should be one of the PIXI.scaleMode consts * @return BaseTexture */ PIXI.BaseTexture.fromImage = function(imageUrl, crossorigin, scaleMode) { var baseTexture = PIXI.BaseTextureCache[imageUrl]; crossorigin = !crossorigin; if(!baseTexture) { // new Image() breaks tex loading in some versions of Chrome. // See https://code.google.com/p/chromium/issues/detail?id=238071 var image = new Image();//document.createElement('img'); if (crossorigin) { image.crossOrigin = ''; } image.src = imageUrl; baseTexture = new PIXI.BaseTexture(image, scaleMode); baseTexture.imageUrl = imageUrl; PIXI.BaseTextureCache[imageUrl] = baseTexture; } return baseTexture; }; PIXI.BaseTexture.fromCanvas = function(canvas, scaleMode) { if(!canvas._pixiId) { canvas._pixiId = 'canvas_' + PIXI.TextureCacheIdGenerator++; } var baseTexture = PIXI.BaseTextureCache[canvas._pixiId]; if(!baseTexture) { baseTexture = new PIXI.BaseTexture(canvas, scaleMode); PIXI.BaseTextureCache[canvas._pixiId] = baseTexture; } return baseTexture; };