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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.