Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
TypeError: drCtx is null

I'm learning JavaScript and tried to do a simple function, but get this error while attempting to run:

TypeError: drCtx is null

html:

<body onload="init()">
     <div style="min-width: 400px; display: inline-block;"></div>
     <canvas id="test" width="1000" height="800"></canvas>
</body>

js:

var width, height;
var drCtx;
var charsize =15;
var chars = ["0","1"]
var charArray;
var c,r;
//Initialze canvas
function init(){
    var canvasElement = document.getElementById("test");
    drCtx = canvasElement.getContext("2D");

    width = canvasElement.width;
    height = canvasElement.height;

    c = parseInt(Math.ceil(width / charsize));
    r = parseInt(Math.ceil(height / charsize));
    charArray = new Array(c);
    for(var i=0; i<c;i++)
    {
        charArray[i] = new Array(r);
        for(var j =0; j<r;j++)
        {
            var rIndex = parseInt(randomIntFromInterval(0, chars.length-1));
            charArray[i][j] = chars[rIndex];
        }
    }
    console.log(charArray);
    renderDR();
}

function renderDR(){
    drCtx.fillStyle = "#000000";
    drCtx.beginPath();
    drCtx.fillRect(0,0,width,height);
    drCtx.fill();

    drCtx.fillStyle = "#FFFFFF"

    for(var i=0; i<c;i++)
    {
        for(var j =0; j<r;j++)
        {
            var x = i*charsize;
            var y = j*charsize;
            drCtx.fillText(charArray[i][j],x,y);
        }
    }
}

function randomIntFromInterval(min, max) { // min and max included 
    return Math.floor(Math.random() * (max - min + 1) + min)
}

My init() fucntion doesn't have this problem and doesn't have return. I don't know what went wrong, any help is appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Change this line:

.getContext("2D")

To:

.getContext("2d")
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda