Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6097409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:02:24+00:00 2026-05-23T13:02:24+00:00

Here is my html: <canvas id=test width=400 height=200></canvas> here is my javascript: var canvas_one

  • 0

Here is my html:

<canvas id="test" width="400" height="200"></canvas>

here is my javascript:

var canvas_one = document.getElementById("test");
var canvas_context = canvas_one.getContext("2d");
canvas_context.fillRect(50, 25, 150, 100);

I am just experimenting with this and none of the tutorials seem to be working. I am using chrome as my browser fyi.

The original typo was only on here; not in my editor. Sorry about that.

I am getting the error: Uncaught TypeError: Cannot call method ‘getContext’ of null even with the jsfiddle example.

Ok I figured out what was wrong. Because my javascript was in the head of the html file, it was being loaded before the DOM was completely loaded, So when it tried to do var canvas_one = document.getElementById("test"); it set canvas_one to null. So what I did is just wrap it in jquery like this:

$(document).ready(function(){
var canvas_one = document.getElementById("test");
var canvas_context = canvas_one.getContext("2d");

canvas_context.fillStyle = "rgb(255,0,0)";
canvas_context.fillRect(10, 25, 150, 100);

});

Now it waits for the DOM to load before it reads the javascript and everything works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:02:25+00:00Added an answer on May 23, 2026 at 1:02 pm

    Make sure any code which references the DOM executes only after the DOM is ready. This can be achieved by wrapping everything in a function which is called using window.onload, or by using jQuery or a similar library to do this for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's the code: HTML: <body onload=initializeMap()> <div id=map_canvas style=width:100%; height:100%; z-index:1></div> <canvas id=control style=width:100%;
In my HTML I have: <div id=cowcloud> <canvas id=cows width=1024 height=253> <p>Browser doesn't support
Here is my HTML: <tr> <td colspan=2 class=borderBottomCell> <div class=benefitInfo style=WIDTH: 99%! important;> <asp:DropDownList
i want to enable facebox jquery using javascript. here in normal html <a href=stairs.jpg
I am making a game using html canvas. Here is my progress: http://db.tt/ei3LlR (use
I'm creating html canvas illustrations using a an adobe illustrator plugin. Here is link
I have html like so: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div>
So I found this: http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html Here is the example: <definition name=myapp.homepage template=/layouts/classic.jsp> <put-attribute name=title
I am trying to follow Trees tutorial at: http://cslibrary.stanford.edu/110/BinaryTrees.html Here is the code I
Here is my HTML: <div id=leftMenuWrapper> <div id=ramps class=leftMenuHeaderButton></div> <div id=carServiceRamps class=leftMenuSubButton></div> <div class=clear></div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.