@GoToLoop thankyouverymuch!
That properly named p5RemoveHack() did the trick to remove all traces of the P5 instance and seeing your technique for re-loading the files, specifically re-linking p5.min + p5.dom = what I was missing (as I was only replacing the actual sketch script).
Here’s my hacky snippet which is working perfect for live-switching between a sketch with WEBGL and one without (testing by using text(), which unfortunately still doesn’t work in WEBGL mode):
function updateP5(){
if(validCode){
p5RemoveHack();
document.getElementById("holdscript").innerHTML = "";
var ps = document.createElement("script");
ps.type = "text/javascript";
ps.src = "includes/p5/p5.min.js";
document.getElementById("holdscript").appendChild(ps);
var ps2 = document.createElement("script");
ps.type = "text/javascript";
ps.src = "includes/p5/addons/p5.dom.min.js";
document.getElementById("holdscript").appendChild(ps2);
var s = document.createElement("script");
s.type = "text/javascript";
s.innerHTML = editor.getValue();
document.getElementById("holdscript").appendChild(s);
}
}
// GoToLoop » https://codepen.io/GoSubRoutine/pen/ZBPjdP
function p5RemoveHack() {
if (window.p5 && p5.instance) {
p5.instance.remove();
p5.instance = window.setup = window.draw = null;
} else {
const canv = document.querySelector('canvas');
canv && canv.remove();
}
}