1

My question is simple (probably). How do I terminate a p5 sketch? Basically what I mean is a way to press the stop (circle button with square) but with code. I've looked around and found stuff about noLoop(), but that doesn't seem to be what I need. I've also seen things about exit(), but I'm that doesn't work for me. What do I do?

  • 1
    What exactly do you want to happen when you "terminate" your sketch? Do you want to show a particular screen / message? Do you want the canvas to be removed from the page? Something else? – Kevin Workman Jul 30 '20 at 22:52
  • @KevinWorkman What I mean is a way to press the stop (circle button with square) but with code. – Patrick Martin Jul 31 '20 at 17:43
  • @PatrickMartin Can you illustrate what you mean by the circle button with square to terminate the sketch ? As opposed to a Processing Java sketch with opens a window via the OS, p5.js sketches are rendered in `` elements on a web page. Do you mean programatically closing the browser window which run the p5.js sketch you mention ? – George Profenza Aug 01 '20 at 01:03

1 Answers1

0

1.If you want to close the active tab: Insert this in the head:

<script>
    function closeWindow() {
        window.open('','_parent','');
        window.close();
    }
</script> 
<a href="javascript:closeWindow();">Close Window</a>

Also see this link: How to close current tab in a browser window?

2.If you want to remove the canvas/sketch only: Insert this in your sketch.js file:

function mousePressed() {
  remove(); // remove whole sketch on mouse press
}

It will remove the canvas created by p5 and every element of p5.