I am trying to make P5 work with Framework inputs slider, however, whenever the slider changes value, the p5 sketch is drawn multiple times as demoed in the images below.
The idea is that the draw method checks whether the sketchâs node is still connected, and if itâs not, it calls p.remove to terminate the animation loop. This way when you trigger the sketch to be re-created, as when the input value changes, youâre getting a new sketch and throwing away the old one.
Also note that you can write this in a way that doesnât re-recreate the sketch when the input changes, and instead refers to the latest input value within the draw loop. That looks like this:
with the helper function, it saved so much of the mess I have to write to get the code running:
no longer need to create an instance of P5 each time I create a sketch;
no longer need to append the sketch to a div to avoid the sketch is thrown to the bottom of the page;
no long need to worry about duplicate sketches when using Inputs
More importantly, because of your helper function, the strange feeling of working in instance mode is almost gone, as now it feels almost exactly like working in the familiar global mode of P5.js! Thank you so much!
I have new use case in which the wonderful helper function wonât remove the duplicate of canvas for me. when you click on any of the sliders, the sketch directly above it will keep duplicating. Here is the link to the demo. Nature of Code Original Demos | Code in Steps
I tried to add some code to the helper function but didnât work. Could you help to make the helper func work on this case too? you can click âsource codeâ details to see the code
update:
As the problem caused by this new use case is the setup function get triggered or the redraw function get triggered under the scene, and your helper function seems not deal with this function at the moment.
Of course, I can avoid this problem by adding p.draw function. However, I would love to see the helper func can automatically handle all use cases.
another update:
Even without the new use case, I still experience some sort of flickering (I can notice the sketch is duplicated and then quickly being removed) when click or drag the slider (50% times I can see the flickering effect, 50% of times are just perfect). Can such flickering effect be eliminated? (maybe flickering is the wrong word, apologize for my English)
The problem is youâre inserting the created canvas into another part of the page when you say e.g. .parent("randomDemo1"). That prevents it from being cleaned up automatically, which is this line here in the code I posted:
I would avoid trying to âinsert content elsewhereâ and using global identifiers in the DOM, and stick to the pattern I suggested where the code is local to where it is displayed. Or, you could use the invalidation promise to clean up the canvases you are inserting elsewhere, but thatâs probably more work.
Thank you so much @mbostock , you are absolutely right about the problem I am running into. After implementing the code as you suggested, the problem solved!