From 8a12eb47fac4f320f460b1aafbf8562fd68a0bb0 Mon Sep 17 00:00:00 2001 From: Rex Date: Fri, 29 Sep 2023 19:09:41 +0800 Subject: [PATCH] Twist logic --- .../loader/load-image-from-localforage.js | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/examples/loader/load-image-from-localforage.js b/examples/loader/load-image-from-localforage.js index b7b0d39929..70b4219f16 100644 --- a/examples/loader/load-image-from-localforage.js +++ b/examples/loader/load-image-from-localforage.js @@ -10,37 +10,11 @@ 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() { @@ -48,6 +22,33 @@ class Demo extends Phaser.Scene { } } +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',