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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:59:50+00:00 2026-06-08T02:59:50+00:00

I’m very new to Html5 canvas and Javascript. I’m trying this : function animate()

  • 0

I’m very new to Html5 canvas and Javascript. I’m trying this :

function animate() {    
   var image1 = new Image();
   image.src = /path
   var image2 = new Image();
   image2.src = /path
   for(;;)
   {
        //change value of x and y so that it looks like moving
        context.beginPath();
        context.drawImage(<image>, x, y );
        context.closePath();
        context.fill();
   }
}

EDIT:

And I call the animate function each 33ms :

if (playAnimation) {
            // Run the animation loop again in 33 milliseconds
            setTimeout(animate, 33);
        };

If I follow the answer given here, I get the image struck and its not moving any further.

  • 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-08T02:59:51+00:00Added an answer on June 8, 2026 at 2:59 am

    Update: Based on new information in the question, your problem (restated) is that you want to either

    1. wait for all images to load first, and then start animating with them, or
    2. start animating and only use an image if it is available.

    Both are described below.

    1. Loading many images and proceeding only when they are finished

    With this technique we load all images immediately and when the last has loaded we run a custom callback.

    Demo: http://jsfiddle.net/3MPrT/1/

    // Load images and run the whenLoaded callback when all have loaded;
    // The callback is passed an array of loaded Image objects.
    function loadImages(paths,whenLoaded){
      var imgs=[];
      paths.forEach(function(path){
        var img = new Image;
        img.onload = function(){
          imgs.push(img);
          if (imgs.length==paths.length) whenLoaded(imgs);
        }
        img.src = path;
      });
    }
    
    var imagePaths = [...]; // array of strings
    loadImages(imagePaths,function(loadedImages){
      setInterval(function(){ animateInCircle(loadedImages) }, 30);
    });
    

    2. Keeping track of all images loaded so far

    With this technique we start animating immediately, but only draw images once they are loaded. Our circle dynamically changes dimension based on how many images are loaded so far.

    Demo: http://jsfiddle.net/3MPrT/2/

    var imagePaths = [...]; // array of strings
    var loadedImages = [];  // array of Image objects loaded so far
    
    imagePaths.forEach(function(path){
      // When an image has loaded, add it to the array of loaded images
      var img = new Image;
      img.onload = function(){ loadedImages.push(img); }
      img.src = path;
    });
    
    setInterval(function(){
      // Only animate the images loaded so far
      animateInCircle(loadedImages);
    }, 100);
    

    And, if you wanted the images to rotate in a circle instead of just move in a circle:

    Rotating images: http://jsfiddle.net/3MPrT/7/

    ctx.save();
    ctx.translate(cx,cy); // Center of circle
    ctx.rotate( (angleOffset+(new Date)/3000) % Math.TAU );
    ctx.translate(radius-img.width/2,-img.height/2);
    ctx.drawImage(img,0,0);
    ctx.restore();
    

    Original answer follows.

    In general, you must wait for each image loading to complete:

    function animate(){
      var img1 = new Image;
      img1.onload = function(){
        context.drawImage(img1,x1,y1);
      };
      img1.src = "/path";
    
      var img2 = new Image;
      img2.onload = function(){
        context.drawImage(img2,x2,y2);
      };
      img2.src = "/path";
    }
    

    You may want to make this code more DRY by using an object:

    var imgLocs = {
      "/path1" : { x:17, y:42  },
      "/path2" : { x:99, y:131 },
      // as many as you want
    };
    
    function animate(){
      for (var path in imgLocs){
        (function(imgPath){
          var xy = imgLocs[imgPath];
          var img = new Image;
          img.onload = function(){
            context.drawImage( img, xy.x, xy.y );
          }
          img.src = imgPath;
        })(path);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.