{"size": 4001, "ext": "js", "max_stars_repo_path": "src/pixi/textures/BaseTexture.js", "max_stars_repo_name": "drkibitz/node-pixi", "max_stars_repo_stars_event_min_datetime": "2015-03-07T00:14:54.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-29T21:46:00.000Z", "max_issues_repo_path": "src/pixi/textures/BaseTexture.js", "max_issues_repo_name": "drkibitz/node-pixi", "max_issues_repo_issues_event_min_datetime": "2015-05-20T05:46:23.000Z", "max_issues_repo_issues_event_max_datetime": "2015-05-23T03:39:14.000Z", "max_forks_repo_path": "src/pixi/textures/BaseTexture.js", "max_forks_repo_name": "drkibitz/node-pixi", "max_forks_repo_forks_event_min_datetime": "2017-09-17T21:24:33.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-29T21:46:02.000Z"}
{"max_stars_count": 7, "max_issues_count": 4, "max_forks_count": 2, "avg_line_length": 22.8628571429, "max_line_length": 94, "alphanum_fraction": 0.5993501625}
/**
 * @author Mat Groves http://matgroves.com/ @Doormat23
 */
'use strict';

var platform = require('../platform');
var globals = require('../core/globals');
var EventTarget = require('../events/EventTarget');
var baseTextureCache = {};

/**
 * 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)
 */
function BaseTexture(source, scaleMode)
{
    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.BaseTexture.SCALE_MODE
     * @default PIXI.BaseTexture.SCALE_MODE.LINEAR
     */
    this.scaleMode = scaleMode || BaseTexture.SCALE_MODE.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('complete' in this.source)
    {
        if(this.source.complete)
        {
            this.hasLoaded = true;
            this.width = this.source.width;
            this.height = this.source.height;

            globals.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...
                globals.texturesToUpdate.push(scope);
                scope.dispatchEvent( { type: 'loaded', content: scope } );
            };
            //this.image.src = imageUrl;
        }
    }
    else
    {
        this.hasLoaded = true;
        this.width = this.source.width;
        this.height = this.source.height;

        globals.texturesToUpdate.push(this);
    }

    this.imageUrl = null;
    this._powerOf2 = false;
}

var proto = BaseTexture.prototype;

/**
 * Destroys this base texture
 *
 * @method destroy
 */
proto.destroy = function destroy()
{
    if(this.source.src)
    {
        if (this.imageUrl in baseTextureCache)
            delete baseTextureCache[this.imageUrl];
        this.imageUrl = null;
        this.source.src = null;
    }
    this.source = null;
    globals.texturesToDestroy.push(this);
};

/**
 *
 *
 * @method destroy
 */
proto.updateSourceImage = function updateSourceImage(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
 * @return BaseTexture
 */
BaseTexture.fromImage = function fromImage(imageUrl, crossorigin, scaleMode)
{
    var baseTexture = baseTextureCache[imageUrl];
    if(!baseTexture)
    {
        var image = new platform.createImage();
        if (crossorigin)
        {
            image.crossOrigin = '';
        }
        image.src = imageUrl;
        baseTexture = new BaseTexture(image, scaleMode);
        baseTexture.imageUrl = imageUrl;
        baseTextureCache[imageUrl] = baseTexture;
    }

    return baseTexture;
};

BaseTexture.SCALE_MODE = {
    DEFAULT: 0, //default to LINEAR
    LINEAR: 0,
    NEAREST: 1
};

module.exports = BaseTexture;
