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 8213713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:12:46+00:00 2026-06-07T11:12:46+00:00

just starting in JavaScript and I’m getting the error canvas is null function draw()

  • 0

just starting in JavaScript and I’m getting the error “canvas is null”

function draw() {  

    var canvas = document.getElementById("canvas");  
    var ctx = canvas.getContext("2d"); 
    var width,height;

    width = canvas.width;
    height = canvas.height;

    ctx.fillStyle = "rgb(0,0,0)";  
    ctx.fillRect (10, 10, width, height); 
    ctx.fillStyle = "rgb(200,0,0)";  
    ctx.fillRect (posx, posy, 30, 30); 

}
var posx;
var posy;
function getMouse(e){
    posx=0;posy=0;
    var ev=(!e)?window.event:e;//Moz:IE
    if (ev.pageX){
        posx=ev.pageX;posy=ev.pageY//Mozilla or compatible
    }
    else if(ev.clientX){
        posx=ev.clientX;posy=ev.clientY//IE or compatible
    }
    else{
        return false//old browsers
    }
        document.getElementById('mydiv').firstChild.data='X='+posx+' Y='+posy;
}
setInterval(draw(),1000);

The code basically draws a box at the mouse position ( at least that’s what it was suppose to do…)

Btw “canvas” is the id of the canvas.
THANKS!

  • 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-06-07T11:12:48+00:00Added an answer on June 7, 2026 at 11:12 am

    Last line should be:

    setInterval(draw,1000);
    

    No parentheses. Otherwise you are CALLING the function, not PASSING it.

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

Sidebar

Related Questions

<p id=specialp>some content</p> <script> document.getElementById('specialp').onclick=alert('clicked'); </script> I'm just starting out with Javascript, and I
I have a code like this... function diagnoseprint() { document.getElementById('goeshere').innerHTML=localStorage['timetable']; var decode = JSON.parse(localStorage['timetable']);
I'm just starting to get into jquery/javascript programming so this may be a beginner
I'm just starting out with Knockout (and javascript for that matter). The problem is
I'm just starting to learn JavaScript, and stumbled over Underscore.js and jQuery . Underscore
I'm just starting out learning javascript, and tried to write a little script that
I'm just starting to learn Javascript; I do however have experience in programming using
I'm just starting with Android development and I'm coming from JavaScript/HTML world so I'm
I'm just starting out with jQuery and want to use $(document).ready() to dynamically generate
Hi just starting to learn this language, more of a javascript/PHP guy... I can't

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.