I created a canvas and called its getContext() method. But it returns null for the context.
Here is the code that I used:
<script>
window.onload = init;
function init(){
var canvas, context;
canvas = document.getElementById('canvas');
context = canvas.getContext('2D');
console.log(canvas);
console.log(context);
}
</script>
I got this in my console:
<canvas id="canvas" width='500' height='250'>This is a canvas</canvas> null
I think it's 2d with a lowercase "d". Try it this way:
context = canvas.getContext('2d');
And if it's not that, then according to the specs:
(
getContext) Returns null if the given context ID is not supported, if the canvas has already been initialized with the other context type (e.g. trying to get a "2d" context after getting a "webgl" context).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With