Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to use p5.js-svg with createGraphics()? #193

Open
atagger opened this issue Jul 7, 2021 · 2 comments
Open

Is there a way to use p5.js-svg with createGraphics()? #193

atagger opened this issue Jul 7, 2021 · 2 comments
Assignees
Labels

Comments

@atagger
Copy link

atagger commented Jul 7, 2021

I'm creating a music visualizer which is processor intensive, so I can't call the SVG renderer from canvas without serious performance implications. Is there a way to call p5-svg once at a set interval? For example - the program finishes running and then it renders the SVG? I was trying to do this by using a creategraphics()object, so it renders one frame, and not putting all the canvas data into the buffer every single frame. When I do this the SVG is empty.

createCanvas(windowWidth, windowHeight); pg = createGraphics(400, 400, SVG);

at some set interval:

save("mySVG.svg"); // give file name

Using clear() in draw() won't work for me either, as it would erase all the visuals.

Thanks for making this add-on, very awesome work!!!

@zenozeng
Copy link
Owner

zenozeng commented Jul 9, 2021

@atagger Hi, you can use pg.xxx() to draw into an off-screen graphics buffer.

Example:

let pg;
function setup() {
  createCanvas(100, 100);
  pg = createGraphics(100, 100, SVG);
}

function draw() {
  background(200);
  pg.background(100);
  pg.noStroke();
  pg.ellipse(pg.width / 2, pg.height / 2, 50, 50);
  image(pg, 50, 50);
  image(pg, 0, 0, 50, 50);
}

@zenozeng zenozeng self-assigned this Jul 9, 2021
@atagger
Copy link
Author

atagger commented Jul 9, 2021

Thank you so much for this response, this behavior works great.

What I'm actually trying to do is figure out if there is a way to clone the entire canvas to the pGraphics, so I don't have to draw each SVG element into the createGraphics buffer one element at a time every frame.

The idea is to try and get a snapshot of the canvas at set intervals of time and then use your library to output the entire canvas as a SVG. I could just set the canvas to SVG as the renderer, but this takes up too much processing power, which I why I'm trying to side step this. Maybe this is possible by cloning the canvas somehow? I will ask around on the p5 forums as well. This would be an amazingly useful way of using your library if it's possible (sort of like processing's PDF renderer).

I stumbled upon this article:
https://discourse.processing.org/t/creategraphics-referencing-existing-canvas-context/21067/3

Will explore further. THANK YOU!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants