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

275
Visualizações
How to export a Cytoscape.js graph to image?

I am learning Cytoscape.js. I successfully make a HTML file containing this basic graph. I try to export the graph to image using:

var png64 = cy.png();

// put the png data in an img tag
document.querySelector('#png-eg').setAttribute('src', png64);) 

and get the error

Uncaught TypeError: document.querySelector(...) is null
    <anonymous> debugger eval code:1
debugger eval code:1:10

There is this post Uncaught TypeError: Cannot read property 'value' of null but I don't understand it. I suspect it's simply that there is no declared element with id #png-eg. But when I replace that with other declared elements with id – which are cy (to display the graph), a, b, ab (the nodes and edge) – the only element that doesn't yield the error is cy. But even then I don't know where the image locates.

I tried add this element <img id="png-eg" src=""> in the body, but it still doesn't work. The null error still happens. (But if I don't include the cy.png() in the script but type in the console, the error is Uncaught DOMException: String contains an invalid character).

In the example there is a line window.cy, so I try to run that in the console. I get Object { _private: {…} }

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

0

If you have the following (make sure the DOM is loaded before trying to query for elements):

HTML:

<div id="cy"></div>

<button>Render PNG</button>

CSS:

#cy {
  width: 320px;
  height: 320px;
}

JS:

const cy = cytoscape({
  container: document.getElementById('cy'),
  // and the rest of the example you already have
})

const btn = document.querySelector('button');

btn.addEventListener('click', () => {
  const base64URI = cy.png();
  const img = document.createElement('img');
  img.src = base64URI;

  document.body.appendChild(img);
});

This will create a new image element and make its source equal to the base64 PNG representation exported by Cytoscape. Then the element will be added to the DOM and you can see the snapshot of the graph rendered inside the image tag.

You can see the whole example on StackBlitz.

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