Saving and Loading BitmapData

Hi to all,
I save a BitmapData as:
let dataUri = this.bmdDraw.canvas.toDataURL();
localStorage.setItem(this.prev_image_key, dataUri);

Then i try to load data this way.
My code:
let localDataUri = localStorage.getItem(this.image_key);
let data = document.getElementById(“temp_img”);
data[‘src’] = localDataUri;
this.game.cache.addImage(randomKey, localDataUri, data);
this.bmdDraw.load(randomKey);

And I get a next error when adding a picture to the cache:
Phaser.Cache.AddImage: Image “randomKey” hasn’t been retrived yet

But the image is uploaded to the temp_img web element.
What am I doing wrong? How can I save and load BitmapData correctly?

Thanks!

Hi,

Is it correct until this point ? http://jsfiddle.net/nazimboudeffa/9sf0dy1L/ So we can dig more

I realized my mistake, I had to use the event ‘onload’, and not immediately try to use the loaded data.
here is the working code:
let img: any = document.getElementById(“temp_img”); // or new Image()
img.onload = () => {
this.game.cache.addImage(key_img, localDataUrl, img);
this.bmdDraw.load(key_img);
};
img[‘src’] = localDataUrl;