Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2017 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d01963a44c523dc04b21bfd7037081b6 to your computer and use it in GitHub Desktop.
Save anonymous/d01963a44c523dc04b21bfd7037081b6 to your computer and use it in GitHub Desktop.
BaseTexture.prototype.update = function update(dontemit) {
// Svg size is handled during load
if (this.imageType !== 'svg') {
this.realWidth = this.source.naturalWidth || this.source.videoWidth || this.source.width;
this.realHeight = this.source.naturalHeight || this.source.videoHeight || this.source.height;
this._updateDimensions();
}
if (!dontemit) this.emit('update', this);
};
BaseTexture.prototype.loadSource = function loadSource(source) {
var wasLoading = this.isLoading;
this.hasLoaded = false;
this.isLoading = false;
if (wasLoading && this.source) {
this.source.onload = null;
this.source.onerror = null;
}
var firstSourceLoaded = !this.source;
this.source = source;
// Apply source if loaded. Otherwise setup appropriate loading monitors.
if ((source.src && source.complete || source.getContext) && source.width && source.height) {
this._updateImageType();
if (this.imageType === 'svg') {
this._loadSvgSource();
} else {
}
var scope = this;
if (firstSourceLoaded) {
// send loaded event if previous source was null and we have been passed a pre-loaded IMG element
if (window.createImageBitmap) {
scope.hasLoaded = true;
scope.update(true);
window.createImageBitmap(source).then(function(imageBitmap) {
scope.source = imageBitmap;
scope.emit('loaded', scope);
})
} else {
this._sourceLoaded();
scope.emit('loaded', scope);
}
}
} else if (!source.getContext) {
// Image fail / not ready
this.isLoading = true;
var scope = this;
source.onload = function () {
scope._updateImageType();
source.onload = null;
source.onerror = null;
if (!scope.isLoading) {
return;
}
scope.isLoading = false;
if (scope.imageType === 'svg') {
scope._loadSvgSource();
return;
}
if (window.createImageBitmap) {
scope.hasLoaded = true;
scope.update(true);
window.createImageBitmap(source).then(function(imageBitmap) {
scope.source = imageBitmap;
scope.emit('loaded', scope);
})
} else {
scope._sourceLoaded();
scope.emit('loaded', scope);
}
};
source.onerror = function () {
source.onload = null;
source.onerror = null;
if (!scope.isLoading) {
return;
}
scope.isLoading = false;
scope.emit('error', scope);
};
// Per http://www.w3.org/TR/html5/embedded-content-0.html#the-img-element
// "The value of `complete` can thus change while a script is executing."
// So complete needs to be re-checked after the callbacks have been added..
// NOTE: complete will be true if the image has no src so best to check if the src is set.
if (source.complete && source.src) {
// ..and if we're complete now, no need for callbacks
source.onload = null;
source.onerror = null;
if (scope.imageType === 'svg') {
scope._loadSvgSource();
return;
}
this.isLoading = false;
if (source.width && source.height) {
// If any previous subscribers possible
if (wasLoading) {
if (window.createImageBitmap) {
scope.hasLoaded = true;
scope.update(true);
window.createImageBitmap(source).then(function(imageBitmap) {
scope.source = imageBitmap;
scope.emit('loaded', scope);
})
} else {
this._sourceLoaded();
scope.emit('loaded', scope);
}
}
}
// If any previous subscribers possible
else if (wasLoading) {
this.emit('error', this);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment