Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

canvas.getContext('2D') returns null value

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
like image 733
Sajith Avatar asked Dec 04 '25 10:12

Sajith


1 Answers

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).

like image 60
federico-t Avatar answered Dec 06 '25 23:12

federico-t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!