Is there an easy way to bind an existing html canvas to the Graphics object you create with createGraphics()?
Example: I have an existing painted 2D canvas that I would like to draw via image() into my existing WEBGL canvas. I just can’t find a method how I can do that other than redrawing everything within p5js. Since createGraphics generates a canvas element. There must be an easy way to just change the reference.
I don’t know if there’s a fancy way of doing it, but you could always fall back on the underlying HTML5 Canvas API. One approach:
// Select the existing canvas (underlying HTML element via `elt`).
const existingCanvas = select('#existingCanvas').elt;
// Use native API to draw existing canvas to the p5.js canvas.
drawingContext.drawImage(existingCanvas, 0, 0);
The p5.js API provides a lot of functionality for creating graphics, but there is some native HTML5 Canvas functionality that is not exposed by p5. You can still call it directly using the variable drawingContext[.]
Ahh this is soo good and helpful! needed to render p5 to an existing canvas for a project and this is the only solution that’s worked…! would be great if it could make it into the main source – once I share example of usage, i’ll (re)open an issue about how/why it’s needed.