7 Kasım 2014 Cuma

Uncaught TypeError: Cannot call method 'getContext' of null

HTML5 ile gelen canvas elemanı sayfa ilk yüklendiğinde oluşturulmalı veya oluşturulan canvas fonksiyonu çağırılmalıdır. Şayet js canvas nesnesinden önce çalışırsa canvas oluşturulamayabilir. Mozilla mdc sitesinde yer alan çözüm metodu aşağıdadır :

<html>

<head>

<script type="application/javascript">

function draw() {

var canvas = document.getElementById("canvas");

var ctx = canvas.getContext("2d");

ctx.fillStyle = "rgb(200,0,0)";

ctx.fillRect (10, 10, 55, 50);

ctx.fillStyle = "rgba(0, 0, 200, 0.5)";

ctx.fillRect (30, 30, 55, 50);

}

</script>

</head>

<body onload="draw()">

<canvas id="canvas" width="300" height="300"></canvas>

</body>

</html>


 

Hiç yorum yok:

Yorum Gönder