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

239
Visualizações
<image> element with data-url href does not show in svg

I'm working on a super simplified avatar editor.

I want the user to be able to drag and drop a memoji on top of a background and display the result (then I'll make it downloadable)

I use an svg as the main container, and use an element to display the background png. On drop of an image, I get a data-url representation of the dropped image and set it as href of a new <image> element. Unfortunately this element never shows up on screen, even though it is there when I inspect my svg element with developer tools. How to get the dynamically created element to display in my svg?

<!DOCTYPE html>
<html lang="en">
  <body>
    <svg
      id="canvas"
      width="800px"
      height="800px"
    >
      <image href="https://cdn.glitch.com/f408a86f-811f-4c06-8c4b-86f559d391b1%2Fbackground.png?v=1631718789960" height="800px" width="800px"/>
    </svg>

    <script>
      const canvas = document.getElementById("canvas");

      // Image file reader
      const reader = new FileReader();
      reader.onload = function() {
        const memojiImage = document.createElement("image");
        memojiImage.setAttribute("href", reader.result);
        memojiImage.setAttribute("height", "844px");
        memojiImage.setAttribute("width", "844px");
        canvas.appendChild(memojiImage);
      };

      // Drag and drop
      const preventDefault = e => {
        e.preventDefault();
        e.stopPropagation();
      };
      canvas.addEventListener("dragenter", preventDefault, false);
      canvas.addEventListener("dragleave", preventDefault, false);
      canvas.addEventListener("dragover", preventDefault, false);
      canvas.addEventListener("drop", preventDefault, false);
      canvas.addEventListener("drop", e => {
        const file = e.dataTransfer.files[0];
        reader.readAsDataURL(file);
      });
    </script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
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