Sajith
Sajith

Reputation: 2852

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

Upvotes: 3

Views: 6864

Answers (1)

federicot
federicot

Reputation: 12341

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

Upvotes: 9

Related Questions