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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:35:42+00:00 2026-06-04T05:35:42+00:00

I want to animate a ball using html5 and i implemented this small script

  • 0

I want to animate a ball using html5 and i implemented this small script . However, I cannot see any animation .. How do I fix this ?

<html>
    <head>
        <meta charset="utf-8">
        <script>
            canvas = document.getElementById("canvas");         
            window.onload=function(){
            if (document.createElement("canvas").getContext){
                //alert("browser supports canvas");
                //console.log(document.getElementById("canvas").getContext);
                canvas = document.getElementById("canvas");
                shape = new shapes();
                shape.drawball(canvas,100,"red");




                }
            };


function shapes(){
    this.drawtriangle = function(canvas){
        triangles = new triangle(0,0,0,200,200,200);
        triangles.draw(canvas.getContext('2d'));    
    }

    this.drawball = function(canvas,radius,color) {
        ball = new Ball(radius,color);
        ball.draw(canvas.getContext('2d'),canvas);
    }
}


function coordinates(x1,y1){
    this.x = x1;
    this.y = y1;
}





function angle(angle,speed){
    this.angle = angle;
    this.speed = speed;
}

function Ball(radius,color){
    this.origin = new coordinates(100,100);
    this.radius = (radius === "undefined" ) ? 40 : radius;
    this.color = (color === "undefined") ? red : color;
    this.rotation = 0;
    this.index  = 0;
    this.angles = new angle(0,0.2);
}

Ball.prototype.draw = function(context,canvas){

    context.fillStyle = this.color;
    context.strokeStyle = "blue";
    context.rotate(this.rotation);
    context.beginPath();
        context.arc(this.origin.x,this.origin.y,this.radius,0,(Math.PI*2),true)
    context.closePath();
    context.fill();
    context.stroke();
    this.animate(context,canvas);
}

Ball.prototype.animate = function(context,canvas){
        if (this.angles.angle < 1){
context.clearRect(0,0,1000,1000);
        console.log("Animating ... ");  
        this.origin.x = this.origin.x + 10; 
        this.origin.y = this.origin.y + 10; 
        this.angles.angle = this.angles.angle + this.angles.speed;
        window.requestAnimationFrame(this.draw(context));



    }
}


        </script>
        <style>

            body {
                background-color: #bbb;
                }       
            #canvas {
                background-color: #fff;
            }
        </style>
    </head>

    <body>
        <canvas id="canvas" width="1000px" height="1000px">
            Your browser dows bot suppoet canvas

        </canvas>


    </body>
</html>
  • 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-04T05:35:44+00:00Added an answer on June 4, 2026 at 5:35 am

    Your call to requestAnimationFrame() is not passing a callback, it’s executing a function and passing its return value which is undefined. I would suggest you change this:

    Ball.prototype.animate = function(context,canvas) {
        if (this.angles.angle < 1) {
            context.clearRect(0,0,1000,1000);
            console.log("Animating ... ");  
            this.origin.x = this.origin.x + 10; 
            this.origin.y = this.origin.y + 10; 
            this.angles.angle = this.angles.angle + this.angles.speed;
            window.requestAnimationFrame(this.draw(context));
        }
    }
    

    to this:

    Ball.prototype.animate = function(context,canvas) {
        if (this.angles.angle < 1) {
            context.clearRect(0,0,1000,1000);
            console.log("Animating ... ");  
            this.origin.x = this.origin.x + 10; 
            this.origin.y = this.origin.y + 10; 
            this.angles.angle = this.angles.angle + this.angles.speed;
            var self = this;
            window.requestAnimationFrame(function() {self.draw(context)});
        }
    }
    

    so that you pass an appropriate callback function to requestAnimationFrame().

    Now that you’ve included all the code, here is another issue. You can’t do this:

    canvas = document.getElementById("canvas");  
    

    in javascript in the head tag because the DOM is not yet loaded so it will not find that object. You must do that only when the DOM has been loaded either by waiting for an event that signifies the DOM has been loaded or by running the javascript at the every end of the <body> section AFTER all DOM elements.

    Then, thirdly, you have to use the browser-specific form of requestAnimationFrame since each browser may have it’s own prefix. I used this code:

    var reqestAnimationFrame = 
        window.requestAnimationFrame ||
        window.mozRequestAnimationFrame || 
        window.msRequestAnimationFrame ||
        window.webkitRequestAnimationFrame;
    

    When I put your script into a jsFiddle and make the above changes, what I find is that the animation runs so quickly that it isn’t seen. Your code will need to add a time element to it so that the animation runs over a particular time period. Usually this is done by defining a duration for the animation and at each animation step, you scale the position of the animation based on what percentage of the duration has elapsed.

    Here’s an example of a time-based animation using requestAnimationFrame: http://jsfiddle.net/jfriend00/nRE7S/

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

Sidebar

Related Questions

I have a few divs that I want to animate using jQuery. However, I
I want to animate imageviews using the scale animation the problem is that after
I am new to Silverlight and want to animate a ball that is shot
I want to use .animate to change colors when .bind('click') is fired, using jquery
My app has a small web view that I want to animate into place
i want to animate my two window using NSWindowFlipper i get the code from
I want to animate a glowing light. In animation ie when I press the
I want to animate an absolute-positioned image with right:xxxPX, let's say. Wen the animation
I want to animate two jquery OBJECTS at the same time (using the jquery
I want to Animate two Different layouts. Example I already have the animation the

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.