var CanvasPool = require('../display/canvas/CanvasPool'); var Class = require('../utils/Class'); var IsSizePowerOfTwo = require('../math/pow2/IsSizePowerOfTwo'); var ScaleModes = require('../renderer/ScaleModes'); var TextureSource = new Class({ initialize: function TextureSource(texture, source, width, height, flipY){ if (flipY === undefined) { flipY = false ; } var game = texture.manager.game; this.renderer = game.renderer; this.texture = texture; this.source = source; this.image = source; this.compressionAlgorithm = null ; this.resolution = 1; this.width = width || source.naturalWidth || source.videoWidth || source.width || 0; this.height = height || source.naturalHeight || source.videoHeight || source.height || 0; this.scaleMode = ScaleModes.DEFAULT; this.isCanvas = (source instanceof HTMLCanvasElement); this.isVideo = (window.hasOwnProperty('HTMLVideoElement') && source instanceof HTMLVideoElement); this.isRenderTexture = (source.type === 'RenderTexture'); this.isGLTexture = (window.hasOwnProperty('WebGLTexture') && source instanceof WebGLTexture); this.isPowerOf2 = IsSizePowerOfTwo(this.width, this.height); this.glTexture = null ; this.glIndex = 0; this.glIndexCounter = -1; this.flipY = flipY; _AN_Call_init('init', this, game); } , init: function (game){ var renderer = this.renderer; if (renderer) { if (renderer.gl) { if (this.isCanvas) { this.glTexture = renderer.createCanvasTexture(this.image, false , this.flipY); } else if (this.isVideo) { this.glTexture = renderer.createVideoTexture(this.image, false , this.flipY); } else if (this.isRenderTexture) { this.image = this.source.canvas; this.glTexture = renderer.createTextureFromSource(null , this.width, this.height, this.scaleMode); } else if (this.isGLTexture) { this.glTexture = this.source; } else { this.glTexture = renderer.createTextureFromSource(this.image, this.width, this.height, this.scaleMode); } } else if (this.isRenderTexture) { this.image = this.source.canvas; } } if (!game.config.antialias) { this.setFilter(1); } } , setFilter: function (filterMode){ if (this.renderer.gl) { this.renderer.setTextureFilter(this.glTexture, filterMode); } this.scaleMode = filterMode; } , setFlipY: function (value){ if (value === undefined) { value = true ; } this.flipY = value; return this; } , update: function (){ var gl = this.renderer.gl; if (gl && this.isCanvas) { this.glTexture = this.renderer.updateCanvasTexture(this.image, this.glTexture, this.flipY); } else if (gl && this.isVideo) { this.glTexture = this.renderer.updateVideoTexture(this.image, this.glTexture, this.flipY); } } , destroy: function (){ if (this.glTexture) { this.renderer.deleteTexture(this.glTexture, false ); } if (this.isCanvas) { CanvasPool.remove(this.image); } this.renderer = null ; this.texture = null ; this.source = null ; this.image = null ; this.glTexture = null ; } } ); module.exports = TextureSource;