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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:46:59+00:00 2026-06-13T01:46:59+00:00

I am trying to animate a ball that follows the cursor in Javascript using

  • 0

I am trying to animate a ball that follows the cursor in Javascript using the canvas element. I pass a reference to my canvas object to the function named followMouse, but when I try to get its context, firebug gives me this error:

TypeError: canvas.getContext is not a function

When I log the canvas reference to the console, it displays the canvas element. I don’t know why then I cannot get the context in the function. Anyone know what is going on?

Here is my code:

function drawCircle(x, y, canvas) {
    var context = canvas.getContext('2d');
    context.beginPath();
    context.arc(x, y, 40, 0, 2 * Math.PI);
    context.fill(); 
}

function followMouse(canvas, mousePos) {
    var context = canvas.getContext("2d");
    console.log(canvas);
    context.clearRect(0, 0, 700, 700);
    var xPos = xPos + (mousePos.x - xPos) / 100;
    var yPos = yPos + (mousePos.y - yPos) / 100;
    drawCircle(xPos, yPos, canvas);
    window.requestAnimFrame(followMouse, canvas);
}

window.onload = function() {
    var canvas = document.getElementById("main");

    var mousePos = {
        x: 0,
        y: 0
    };

    canvas.addEventListener('mousemove', function(evt) {
           var pos = getMousePos(canvas, evt);
           mousePos.x = pos.x;
           mousePos.y = pos.y;
    }, false);

    followMouse(canvas, mousePos);
}
  • 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-13T01:47:00+00:00Added an answer on June 13, 2026 at 1:47 am

    Your line window.requestAnimFrame(followMouse, canvas); calls followMouse(timestamp), where timestamp is a number passed to the callback when it is fired by requestAnimationFrame. This results in a call to timestamp.getContext, which is obviously invalid.

    Instead, wrap your call inside an anonymous function:

    window.requestAnimFrame(function() { followMouse(canvas, mousePos) }, canvas);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to animate a triangle flipping in canvas using javascript. The problem is
I'm trying to animate a Canvas-based texture that is mapped onto a plane, like
I'm trying to animate a overlay using opacity when you hover the element, however
I am trying to animate a 3d mesh that chnages over time using trimesh
I'm using AS3.0 and I'm trying to make a function that will generate a
I am trying to animate an element that is absolutely positioned and positioned bottom
I'm trying to animate a block level element using jQuery. The page loads with
I am trying to animate a div moving 200px horizontally in JavaScript. The code
I'm trying to animate drop up using negative top, the animation went okay, but
I'm trying to animate a UIWebview being removed from the screen. I'm using an

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.