So, I have the following structure:
<div class="conf">
<canvas id="world"></canvas>
</div>
and I have the following line of code:
context = $('#world').get(0).getContext('2d');
which returns:
Cannot read property 'getContext' of undefined"
and i don't know why.
I already tried various variations like $('#world')[0]
and $('canvas')[0]
and $('canvas').get(0)
but I was not able to fix it :(
So I am asking here where is my fault?
EDIT: This is the code I am trying to run in my environment. Interestingly it is working here^^
Environment: VSCode
$(function() {
function trigger() {
console.log('triggered');
let context;
context = $('#world').get(0).getContext('2d');
context.beginPath();
context.fillStyle = "white";
context.arc(200, 100, 30, 0, Math.PI * 2, true);
context.fill();
}
$(document).on("click", "#test", function(e) {
trigger();
});
});
html,
body, .conf {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #111;
}
.button {
position: absolute;
top: 0;
left: 10px;
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<button id="test">
Button
</button>
<div class="conf">
<canvas id="world"></canvas>
</div>
</body>
</html>