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

254
Visualizações
How to generate SVG dataURL with p5.js and p5.js-svg?

I am trying to generate an image using p5.js and p5.js-svg and then save the SVG xml as a dataURL for storage. Here is an example script that I've simplified for this question:

const canvasWidth = 600;
const canvasHeight = 600;
const bgColor = "#fffcf3";

let unencodedDataURL = "";

const sketch = (p) => {
  p.setup = () => {
    p.createCanvas(canvasWidth, canvasHeight, p.SVG);
    p.background(bgColor);
    p.noLoop();
  }

  p.draw = () => {
    p.noFill();

    p.ellipse(z
      canvasWidth / 2,
      canvasHeight / 2,
      150,
      150
    );
    unencodedDataURL = p.getDataURL();
    console.log(`inside: ${unencodedDataURL}`);
  }
}

const test = new p5(sketch, document.body);
console.log(`outside: ${unencodedDataURL}`);

The first log statement prints the right dataURL. But of course the second log statement executes before the one inside draw() and I cannot figure out how to capture the dataURL correctly. I'm sure I am missing something in the p5.js or p5.js-svg libraries and there is an easier way. But I am stuck. Anyone have an idea here? Thanks in advance!

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

0

Because p5js run asynchronous, You can add callback or write a Promise to get data after sketch draw, like this

var handleDataCallback = null;

//........
p.ellipse(
    canvasWidth / 2,
    canvasHeight / 2,
    150,
    150
);
unencodedDataURL = p.getDataURL();
if (handleDataCallback) handleDataCallback(unencodedDataURL);
//....

//for callback
handleDataCallback = dataURL => {
//todo something with dataURL
}

if you want to write as a function to generate image

function generateImage(param, callback) {
    return new Promise(resolve  => {

        //......
        // do something
        p.ellipse(
            canvasWidth / 2,
            canvasHeight / 2,
            150,
            150
        );
       var unencodedDataURL = p.getDataURL();
        if (callback) callback(unencodedDataURL);
        resolve(unencodedDataURL);
        //  ....


    })


}

//use Promise
generateImage(yourParam, null).then(unencodedDataURL=>{
    console.log(`outside: ${unencodedDataURL}`);
})
//or callback
generateImage(yourParam, unencodedDataURL=> {
    console.log(`outside: ${unencodedDataURL}`);
});

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