Jump to content

Phaser 3 Load BASE 64 Image


Shubham Seth
 Share

Recommended Posts

how to load a BASE 64  image in phaser 3 ?

I am doing on this way 

function preload() {
 

  var dataURI = '''data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQY............"

var data = new Image();
  data.src = dataURI;
  game.cache.addImage('image-data', dataURI, data);
}

function create() {

  game.add.image(0, 426, 'image-data');
}

Link to comment
Share on other sites

I have re-write your code a little bit. Hope it help.

var platform;
var counter = 0;

function preload() {
	platform = this;
	
	/*var dataURI = '''data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQY............"

	var data = new Image();
	data.src = dataURI;
	game.cache.addImage('image-data', dataURI, data);*/
}

function create() {	
	// Handle Base 64 Image From Here Not From PreLoad
	
	var dataURI = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQY............"
	
	platform.textures.addBase64('imgBase64', dataURI);
	
	// Loader to wait all base64 Image loaded
	platform.textures.on('onload', function() { 
		counter++;			
	});
	
	// Timer check when all based64 assets have been loaded
	platform.customTimer = platform.time.addEvent({ delay: 500, callback: function callback() {
		
		// Adjust counter maximum as you ecpect. For now just one based64 assets need to be loaded only
		if (counter === 1) {
		
			// Destroy timer to save memory
			platform.customTimer.remove(false);
		
			// Add base64 image and position it		
			platform.imgBase64Sprite = platform.add.sprite(game.config.width / 2, game.config.height / 2, 'imgBase64');
		
		}
	
	}, callbackScope: platform, loop: true});
}	

 

Link to comment
Share on other sites

  • 2 months later...
  • 1 year later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...