Ciao,
ho provato il tuo codice, a me da errore sulla riga

const ctx = canvas.getContext('2d');

Uncaught TypeError: Cannot read properties of null (reading 'getContext')

sia con edge che con firefox