0

I have a canvas in HTML and I am trying to put a transparent background so I can layer other elements under it. This is the advised code to enable a transparent background on a canvas.

const canvas = document.getElementsByTagName('canvas');
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");

<canvas style="width: 1500px; height: 800px;"></canvas>

Error message: Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

1 Answers1

0

I believe that you are getting the error/exception due to the fact that getElementsByTagName returns a list instead of a single element.

I would propose/recommend using getElementById

A sample would be something like this:

HTML:

<canvas id='canvas' width='600' height='300'>
</canvas>

JS:

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext("2d");
heyitsmarcucu
  • 612
  • 5
  • 9