Skip to content

Commit

Permalink
Twist logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Sep 29, 2023
1 parent 826ac0f commit 8a12eb4
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions examples/loader/load-image-from-localforage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,45 @@ class Demo extends Phaser.Scene {
}

preload() {
var self = this;
var key = 'photo';
this.load.rexAwait(function (successCallback, failureCallback) {
localforage.getItem(key).then(function (image) {
if (!image) {
// failureCallback();

// Load from server
self.load.binary('photo', 'assets/images/mushroom.png', Uint8Array);

self.load.once(`filecomplete-binary-${key}`, function () {
var buffer = self.cache.binary.get(key);
localforage.setItem('photo', buffer);

console.log('Prepare local image file, see you next time');
failureCallback();
});
}

var blob = new Blob([image]);
var url = window.URL.createObjectURL(blob);
self.load.image(key, url);

console.log('Get local image file success');
successCallback();
})
});
LoadImageFromLocalForage(this, 'mushroom', 'assets/images/mushroom.png')
}

create() {
this.add.image(400, 300, 'photo');
this.add.image(400, 300, 'mushroom');
}

update() {

}
}

var LoadImageFromLocalForage = function (scene, key, sourceURL) {
scene.load.rexAwait(function (successCallback, failureCallback) {
localforage.getItem(key).then(function (data) {
if (!data) {
// Load from server
scene.load.binary(key, sourceURL, Uint8Array);

scene.load.once(`filecomplete-binary-${key}`, function () {
var buffer = scene.cache.binary.get(key);
LoadImageFromUint8Array(scene, key, buffer);
localforage.setItem(key, buffer).then(successCallback);
});

} else {
LoadImageFromUint8Array(scene, key, data);
successCallback();
}
})
});
}

var LoadImageFromUint8Array = function (scene, key, data) {
var blob = new Blob([data]);
var blobURL = window.URL.createObjectURL(blob);
scene.load.image(key, blobURL);
}

var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
Expand Down

0 comments on commit 8a12eb4

Please sign in to comment.