最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'getContext' of null - Stack Overflow

programmeradmin0浏览0评论

In my console I get the error: "Uncaught TypeError: Cannot read property 'getContext' of null" and I just can't find the error I've made... or what I've done wrong. So maybe you can help me find it? Please help :)

enter code here

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

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

var cW = canvas.width = 1000; 
var cH = canvas.height = 500; 

var particleAmount = 10; 
var particles = []; 

for(var i=0;i<particleAmount;i++) { 
particles.push(new particle());

}

function particle() { 
this.x = (Math.random() * (cW-(40*2))) + 40; 
this.y = (Math.random() * (cH-(40*2))) + 40; 
this.xv = Math.random()*20-10; 
this.yv = Math.random()*20-10; 

}

function draw () { 
ctx.fillStyle = "black";
ctx.fillRect(0,0,cW,cH);

for(var ii=0;ii<particles.length;ii++){
    var p = particles[ii]; 
    ctx.fillStyle = "red";
    ctx.beginPath(); 
    ctx.arc(p.x,p.y,40,Math.PI*2,false); 
    ctx.fill();
}


}

setInterval(draw,30);

This is my html code:

<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Vector_5</title>
        <script src="Test_Particle.js"></script>  
    </head>

    <body>
        <canvas id="myCanvas" ></canvas>
    </body>
    
</html>

In my console I get the error: "Uncaught TypeError: Cannot read property 'getContext' of null" and I just can't find the error I've made... or what I've done wrong. So maybe you can help me find it? Please help :)

enter code here

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

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

var cW = canvas.width = 1000; 
var cH = canvas.height = 500; 

var particleAmount = 10; 
var particles = []; 

for(var i=0;i<particleAmount;i++) { 
particles.push(new particle());

}

function particle() { 
this.x = (Math.random() * (cW-(40*2))) + 40; 
this.y = (Math.random() * (cH-(40*2))) + 40; 
this.xv = Math.random()*20-10; 
this.yv = Math.random()*20-10; 

}

function draw () { 
ctx.fillStyle = "black";
ctx.fillRect(0,0,cW,cH);

for(var ii=0;ii<particles.length;ii++){
    var p = particles[ii]; 
    ctx.fillStyle = "red";
    ctx.beginPath(); 
    ctx.arc(p.x,p.y,40,Math.PI*2,false); 
    ctx.fill();
}


}

setInterval(draw,30);

This is my html code:

<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Vector_5</title>
        <script src="Test_Particle.js"></script>  
    </head>

    <body>
        <canvas id="myCanvas" ></canvas>
    </body>
    
</html>
Share Improve this question edited Mar 18, 2024 at 10:00 Stephen Ostermiller 25.5k16 gold badges95 silver badges115 bronze badges asked Sep 13, 2014 at 22:40 Pumpkin_MPumpkin_M 1611 gold badge2 silver badges12 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 6

The error basically means that the HTML and the JavaScript code aren't cooperating properly, or simply that you haven't loaded the script correctly.
Try this:

function init() {
  // Run your javascript code here
}

// Run the 'init' function when the DOM content is loaded
document.addEventListener("DOMContentLoaded", init, false);

Hope this helps.

The canvas doesn't exist yet. Load the script after the canvas element.

Source: elclanr's comment Sep 13 '14 at 22:43

I think you put the script tag above of the canvas tag please put it after the canvas tag.

Put the scripts file after the canvas. That means put down your script before the body tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML5 Canvas</title>

    <link rel="shortcut icon" href="assets/1.png">
    <link rel="stylesheet" href="assets/canvas.css">
</head>
<body>
    <div class="container">
        <h2>Canvas</h2>
        <canvas id="myCanvas" width="400" height="300">

        </canvas> <!-- End Canvas -->
    </div> <!-- End Container -->
    <script src="canvas.js"></script>
</body>
</html> 

Try to declare the canvas element before the script tag. It worked fine for me.

发布评论

评论列表(0)

  1. 暂无评论