Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

367
Visualizações
How to upload image of profile in phaser js?

How to upload Image from local storage to phaser using rest apis ?

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It depends what you mean exactly, do you want to upload the image to the server, where the phaser application is running, or should everything be kept locally?

  • if you only need a local version, you could do this:
    • load the image
    • save it in the localStorage of the browser (doesn't work on stackoverflow, code snipplets) : localStorage.setItem('profile', <image-data> )
    • display the image (or from the localStorage, if it was saved localStorage.getItem('profile', <image-data> ))
  • if you need it to be uploaded to the server, the answer is a bit more complex and depends on the programming language on the server. But here is a pointer, for the client side: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#uploading_a_file

Here a short demo/proof-of-concept:
(without localStorage, since it doesn't work here)

document.body.style = 'margin:0;';

var currentScene;
var profileImage;

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 173,
    scene: { create },
    banner: false
}; 

function create () {
    let message = this.add.text(10,10, 'Select a Image');
    currentScene = this;
}

new Phaser.Game(config);

let imgElement = document.querySelector('#localImage');
imgElement.addEventListener('change', _ => {
    const [file] = imgElement.files
    if (file) {
      var reader = new FileReader();
      reader.onload = function(e) {
           var img = new Image();
           img.onload = _ => { 
               let txt = currentScene
                   .textures
                   .createCanvas('profile',  img.width, img.height);

               txt.draw(0, 0, img);               
               profileImage = currentScene.add
                   .image(20, 40, 'profile')
                   .setOrigin(0);
               profileImage.setScale(180 / img.height);
           }               
           img.src = reader.result;
       }
       reader.readAsDataURL(file);
    }
})
<script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>
<input accept="image/*" type='file' id="localImage" />

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda