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

Added feature to saveCanvas() to support framebuffer #6510

Merged
merged 7 commits into from Nov 17, 2023

Conversation

capGoblin
Copy link
Contributor

Resolves #6404

Introduces an enhancement to the saveCanvas function, allowing it to accept a framebuffer as an input.

Changes:

  • Added support for framebuffer as a valid argument for the saveCanvas function.
  • updated comment docs to include p5.Graphics as a param.

Screenshots of the change:

PR Checklist

@capGoblin
Copy link
Contributor Author

capGoblin commented Oct 29, 2023

Is there anything else left that needs to be addressed in this PR?

@davepagurek
Copy link
Contributor

Thanks for taking on this task! I think we don't need to do anything to support p5.Graphics in the if statement since a p5.Graphics is just a more specific case of a p5.Element:

p5.Graphics = class extends p5.Element {

What we would need to do to resolve the issue is to add support for the p5.Framebuffer class. This would involve a different if statement branch because it isn't an element directly. One way to add this in might be:

  • make a new 2d createGraphics() (it doesn't matter for saving if it's 2D or 3D but the browser can support more 2d canvases at once than 3D)
  • loadPixels() on the framebuffer
  • update the pixels on the graphic to match the pixels of the framebuffer
  • call updatePixels() on the graphic
  • save the graphic
  • remove the graphic

src/image/image.js Outdated Show resolved Hide resolved

if (arguments[0] instanceof HTMLCanvasElement) {
htmlCanvas = arguments[0];
args.shift();
} else if (arguments[0] instanceof p5.Element) {
htmlCanvas = arguments[0].elt;
args.shift();
} else if (arguments[0] instanceof p5.Framebuffer) {
const framebuffer = arguments[0];
temporaryGraphics = createGraphics(framebuffer.width, framebuffer.height);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think this all works in global mode! The last thing to do is to also make it work in instance mode, where createGraphics isn't globally defined. I think in that case using this.createGraphics will work though? Maybe try that and try using this in an instance mode sketch to verify

@capGoblin
Copy link
Contributor Author

Works in both global and instance mode now.👍

@davepagurek davepagurek merged commit 007f550 into processing:main Nov 17, 2023
2 checks passed
@capGoblin capGoblin deleted the feature/savecanvas-framebuffer branch November 17, 2023 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support saveCanvas with framebuffer inputs
2 participants