I am trying to find why my html canvas element is not being recognized in javascript.
I'm not sure why my canvas element is null. I defined it in html, and only run javascript once the DOMContentLoaded event is fired.
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Whiteboard</title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" />
</head>
<body>
<canvas id="canvas">Your browser doesn't support "canvas".</canvas>
<!--<script src="/node_modules/socket.io/socket.io.js"></script>-->
<script type="text/javascript" src="client1.js"></script>
</body>
</html>
client1.js:
document.addEventListener("DOMContentLoaded", function() {
var mouse = {
click: false,
move: false,
pos: {x:0, y:0},
pos_prev: false
};
// get canvas element and create context
document.write("Hetoo ");
var canvas = document.getElementById("canvas");
if(canvas)
{
document.write("got her.");
}
else
{
document.write(canvas);
}
document.write(" Hepoo ");
var context = canvas.getContext('2d');
document.write("Heyoo ");
}
output on webpage:
Hetoo null Hepoo
I am expecting the canvas to not be null, but the javascript does not recognize this for some reason.