Shubham Seth

Phaser 3 Load BASE 64 Image

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');
}

Share this post


Link to post
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});
}	

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.