diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bd767e6..56213c2 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -1,6 +1,7 @@ var utils = require('../utils'), CONST = require('../const'), EventEmitter = require('eventemitter3'), + determineCrossOrigin = require('../utils/determineCrossOrigin'), bitTwiddle = require('bit-twiddle'); /** @@ -391,19 +392,16 @@ { var baseTexture = utils.BaseTextureCache[imageUrl]; - if (crossorigin === undefined && imageUrl.indexOf('data:') !== 0) - { - crossorigin = true; - } - 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) + + + if (crossorigin === undefined && imageUrl.indexOf('data:') !== 0) { - image.crossOrigin = ''; + image.crossOrigin = determineCrossOrigin(imageUrl); } baseTexture = new BaseTexture(image, scaleMode); diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bd767e6..56213c2 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -1,6 +1,7 @@ var utils = require('../utils'), CONST = require('../const'), EventEmitter = require('eventemitter3'), + determineCrossOrigin = require('../utils/determineCrossOrigin'), bitTwiddle = require('bit-twiddle'); /** @@ -391,19 +392,16 @@ { var baseTexture = utils.BaseTextureCache[imageUrl]; - if (crossorigin === undefined && imageUrl.indexOf('data:') !== 0) - { - crossorigin = true; - } - 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) + + + if (crossorigin === undefined && imageUrl.indexOf('data:') !== 0) { - image.crossOrigin = ''; + image.crossOrigin = determineCrossOrigin(imageUrl); } baseTexture = new BaseTexture(image, scaleMode); diff --git a/src/core/utils/determineCrossOrigin.js b/src/core/utils/determineCrossOrigin.js new file mode 100644 index 0000000..e80c9bd --- /dev/null +++ b/src/core/utils/determineCrossOrigin.js @@ -0,0 +1,43 @@ +var tempAnchor; +var _url = require('url'); + +/** + * Sets the `crossOrigin` property for this resource based on if the url + * for this resource is cross-origin. If crossOrigin was manually set, this + * function does nothing. + * Nipped from the resource loader! + * @private + * @param url {string} The url to test. + * @param [location=window.location] {object} The location object to test against. + * @return {string} The crossOrigin value to use (or empty string for none). + */ +var determineCrossOrigin = function (url, loc) { + // data: and javascript: urls are considered same-origin + if (url.indexOf('data:') === 0) { + return ''; + } + + // default is window.location + loc = loc || window.location; + + if (!tempAnchor) { + tempAnchor = document.createElement('a'); + } + + // let the browser determine the full href for the url of this resource and then + // parse with the node url lib, we can't use the properties of the anchor element + // because they don't work in IE9 :( + tempAnchor.href = url; + url = _url.parse(tempAnchor.href); + + var samePort = (!url.port && loc.port === '') || (url.port === loc.port); + + // if cross origin + if (url.hostname !== loc.hostname || !samePort || url.protocol !== loc.protocol) { + return 'anonymous'; + } + + return ''; +}; + +module.exports = determineCrossOrigin; \ No newline at end of file