79247163

Date: 2024-12-03 10:24:51
Score: 1.5
Natty:
Report link
<script setup>
import { onBeforeUnmount, ref } from "vue";
let p5Instance = null;
const sketch = (p5) => {
  p5Instance = p5;

  p5.preload = () => {};

  p5.setup = () => {};

  p5.draw = () => {};

  p5.mousePressed = () => {};

  p5.mouseDragged = () => {};

  p5.mouseReleased = () => {};

  p5.mouseWheel = (event) => {};
  p5.keyPressed = (event) => {
    switch (event.keyCode) {
      default:
        break;
    }
  };
};

// p5 instance Cleanup on unmount component
onBeforeUnmount(() => {
  if (p5Instance) {
    p5Instance.remove();
  }
});
</script>

<template>
  <P5 style="overflow: hidden; height: 100dvh"/>
</template>

If anyone comes across this post with the same problem i had, this is the solution I found, save the p5 instance in a global variable and use the on unmount hook to remove the instance on navigation.

Sadly i dont remember where i found or who gave me this solution, otherwise i would give them some credit.

@emeik has a very similar solution

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @emeik
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Ivo Heberle