-
Notifications
You must be signed in to change notification settings - Fork 395
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
Render function called twice while exporting #25
Comments
Thanks for the issue report! This is actually intentional, and not a bug. I'll explain why: In many sketches the visible size on screen (within your browser) will not match the export size. For example, if you are using Another situation is with Just like with other declarative frameworks (React, Vue, etc) you should try not to assume too much about when the I'd also be happy to explore other solutions, if you let me know a bit more about why you needed to use a generator function. Maybe there is some syntax or documentation I could improve in canvas-sketch. Cheers! |
Hi! Thanks very much for the detailed response. This makes a lot of sense and I guess is just a drawback of rendering in the browser. (I’m currently working on something inspired by I imagine this approach also causes issues if you want to repeatedly draw over the previous frame? (Like generative code that overlays on itself constantly.) Might it be possible to instantiate a second invisible Thanks! |
Oh, and regarding documentation: the documentation for this project is absolutely excellent, but yes, I suppose mentioning the mechanics behind frame exporting somewhere would be nice — I hadn’t considered the sizing issues at all. |
Cool that you are remixing it with a node-only version. 😄 Not sure if you saw, but I have also designed Yes, if you want to repeatedly draw over previous frames this might create issues. I'd like to find a nice solution for those scenarios, though. The secondary In general I'd like to clean up some of the logic around the render/frame loop, it generally feels a bit messy/unpolished right now. Maybe there is room for another setting like |
Actually, I'm just testing and you can almost achieve a generative result like this, using the const canvasSketch = require('canvas-sketch');
const settings = {
dimensions: [ 2048, 2048 ],
totalFrames: 100,
animate: true
};
const sketch = ({ context, width, height }) => {
return {
begin () {
// Initially fill/clear the canvas
context.fillStyle = 'white';
context.fillRect(0, 0, width, height);
},
tick ({ frame }) {
// Render each subsequent frame
console.log('Rendered', frame);
for (let i = 0; i < 10; i++) {
context.fillRect(Math.random() * width, Math.random() * height, 10, 10);
}
}
};
};
canvasSketch(sketch, settings); However, it's still a bit buggy, and this goes back to having to fix the frame/render loop. I've noticed occasionally the last frame doesn't render, and it seems to be due to timing differences. |
Oh, nice, I didn’t notice the section on using Does I don’t know exactly what the performance implications would be of a second canvas. I guess the number of calculations would stay the same because they are already duplicated in the current implementation, but there might be a memory hit. Depending on how fast it is to instantiate or dispose of a |
The second canvas is a cool idea but doing it automatically under the hood is just too magical for my taste, and could introduce a lot of new problems. I think its best that |
I noticed while experimenting with
canvas-sketch
that when you export an image using cmd + s or an animation using cmd + shift + s, the render function is called twice for each frame.To reproduce:
Then launch:
Open the browser console and then try saving with cmd + s and you should see “render frame 0” logged twice.
Here are the relevant lines from my console (Firefox 62.0.3):
“render frame 0” is logged once on page load, then twice more on export/save. When exporting an animation the duplication happens for each frame.
I imagine this has performance implications, but I also tripped up on it because I was using a generator function to get the next value to display for each frame. Each frame was being rendered twice (and saved once), which meant every other value returned from the generator was lost.
P.S. Generally the experience getting started with this has been really great. Thanks for the thoughtful library and clear documentation!
The text was updated successfully, but these errors were encountered: