+ 2

Canvas.getContext("2d")

I feel dumb but when I tried to access the getcontext 2d for canvas I get a return of null and I have no idea why????

6th Feb 2018, 4:50 PM
Demetrius Lewis
Demetrius Lewis - avatar
6 Respuestas
+ 2
you wrote id"Canvas" instead of id="Canvas" and also you get the element with id canvas instead of Canvas
6th Feb 2018, 5:50 PM
Sylver
Sylver - avatar
+ 7
Can you show code?
6th Feb 2018, 4:59 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
I think js is loading before HTML...if its try this window.onload = function(){ //all codes over here }
6th Feb 2018, 5:05 PM
Rohit
Rohit - avatar
+ 1
<!DOCTYPE html> <html> <head> <title>brick breaker</title> </head> <body> <canvas id"Canvas" width="300" height="300" style="border:1px solid black"></canvas> </body> </html>
6th Feb 2018, 5:31 PM
Demetrius Lewis
Demetrius Lewis - avatar
+ 1
function canvas() { var ctx = document.getElementById("canvas").getContext("2d");
6th Feb 2018, 5:31 PM
Demetrius Lewis
Demetrius Lewis - avatar
+ 1
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); also tried this
6th Feb 2018, 5:39 PM
Demetrius Lewis
Demetrius Lewis - avatar