You can request the layer canvas as an extra argument for your ngon() function:
function ngon(x, y, sides, sz, star = false, ins_ = sz/2, midPos = .5, pg = p5.instance) {
Now prefix all drawing-related methods w/ that pg parameter:
push();
→pg.push();
beginShape();
→pg.beginShape();
vertex(x, y);
→pg.vertex(x, y);