I am trying to make the background of the canvas but I can't use getContext. I get the error:
Uncaught TypeError: Cannot read property 'getContext' of null at game.js:4
Html
<!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8">
<title>Game</title>
</head>
<body>
<script src="game.js"></script>
<canvas id="canvas" width="100" height="320"></canvas>
</body>
</html>
Javascript
const canvas = document.getElementById('canvas');
if (canvas != null) {
ctx = canvas.getContext("2d");
} else {
console.log('wtf happen');
}
function draw(){
ctx.background(255,0,0);
}
I have already tried using the post: Cannot read property 'getContext' of null
but it did not help me.
tag so it runs after all the elements have rendered.
– pizzarob Jul 11 '19 at 21:23