|
@@ -499,32 +499,56 @@
|
|
|
return url;
|
|
|
}
|
|
|
|
|
|
- public static LoadImage(url: any, onLoad: (img: HTMLImageElement) => void, onError: (message?: string, exception?: any) => void, database: Nullable<Database>): HTMLImageElement {
|
|
|
- if (url instanceof ArrayBuffer) {
|
|
|
- url = Tools.EncodeArrayBufferTobase64(url);
|
|
|
- }
|
|
|
-
|
|
|
- url = Tools.CleanUrl(url);
|
|
|
+ /**
|
|
|
+ * Loads an image as an HTMLImageElement.
|
|
|
+ * @param input url string, ArrayBuffer, or Blob to load
|
|
|
+ * @param onLoad callback called when the image successfully loads
|
|
|
+ * @param onError callback called when the image fails to load
|
|
|
+ * @param database database for caching
|
|
|
+ * @returns the HTMLImageElement of the loaded image
|
|
|
+ */
|
|
|
+ public static LoadImage(input: string | ArrayBuffer | Blob, onLoad: (img: HTMLImageElement) => void, onError: (message?: string, exception?: any) => void, database: Nullable<Database>): HTMLImageElement {
|
|
|
+ let url: string;
|
|
|
+ let usingObjectURL = false;
|
|
|
|
|
|
- url = Tools.PreprocessUrl(url);
|
|
|
+ if (input instanceof ArrayBuffer) {
|
|
|
+ url = URL.createObjectURL(new Blob([input]));
|
|
|
+ usingObjectURL = true;
|
|
|
+ }
|
|
|
+ else if (input instanceof Blob) {
|
|
|
+ url = URL.createObjectURL(input);
|
|
|
+ usingObjectURL = true;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ url = Tools.CleanUrl(input);
|
|
|
+ url = Tools.PreprocessUrl(input);
|
|
|
+ }
|
|
|
|
|
|
var img = new Image();
|
|
|
Tools.SetCorsBehavior(url, img);
|
|
|
|
|
|
const loadHandler = () => {
|
|
|
+ if (usingObjectURL && img.src) {
|
|
|
+ URL.revokeObjectURL(img.src);
|
|
|
+ }
|
|
|
+
|
|
|
img.removeEventListener("load", loadHandler);
|
|
|
img.removeEventListener("error", errorHandler);
|
|
|
onLoad(img);
|
|
|
};
|
|
|
|
|
|
const errorHandler = (err: any) => {
|
|
|
+ if (usingObjectURL && img.src) {
|
|
|
+ URL.revokeObjectURL(img.src);
|
|
|
+ }
|
|
|
+
|
|
|
img.removeEventListener("load", loadHandler);
|
|
|
img.removeEventListener("error", errorHandler);
|
|
|
|
|
|
- Tools.Error("Error while trying to load image: " + url);
|
|
|
+ Tools.Error("Error while trying to load image: " + input);
|
|
|
|
|
|
if (onError) {
|
|
|
- onError("Error while trying to load image: " + url, err);
|
|
|
+ onError("Error while trying to load image: " + input, err);
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -541,7 +565,6 @@
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-
|
|
|
//ANY database to do!
|
|
|
if (url.substr(0, 5) !== "data:" && database && database.enableTexturesOffline && Database.IsUASupportingBlobStorage) {
|
|
|
database.openAsync(loadFromIndexedDB, noIndexedDB);
|
|
@@ -560,6 +583,7 @@
|
|
|
blobURL = URL.createObjectURL(FilesInput.FilesToLoad[textureName]);
|
|
|
}
|
|
|
img.src = blobURL;
|
|
|
+ usingObjectURL = true;
|
|
|
}
|
|
|
catch (e) {
|
|
|
img.src = "";
|