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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:27:20+00:00 2026-05-26T11:27:20+00:00

I am learning HTML5 and JavaScript and am attempting to draw an animated image.

  • 0

I am learning HTML5 and JavaScript and am attempting to draw an animated image. I thought the easiest way to do this would be to create an image with the frames in a row, as below.

Image http://html5stuff.x10.mx/HTML5%20Test/alien_green_strip8.png

Then a only part of the image would be draw at a time. I followed this tutorial.

This is a link to what I have made:

html5stuff.x10.mx/HTML5%20Test/page.html

The problem is, the image isn’t being drawn. It’s something within the drawSprite function, because when I change it to a simple “ctx.drawImage(sprite.source, x, y)”, it does draw the image (just as a whole without the animation, obviously). Please note that though there is an option for rotating the image, I have not yet added support for that. Also, keys.js is not being used yet though it is included.

  • 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-26T11:27:20+00:00Added an answer on May 26, 2026 at 11:27 am

    The reason is because sprite.imagenum is not defined when drawSprite is called.

    This is because in some places you use imagenum and others imgnum, so correct that typo and you’re good to go!


    TOTALLY OPTIONAL:

    But now that thats answered lets take a look at your js to get a better idea of how to structure this. You have:

    function Sprite(){
      var imagenum = null; //The number of images
      var width = null; //The width of each image
      var height = null; //The height on each image
      var xoffset = null; //The origin on each image
      var yoffset = null;
      var source = null; //The location of each image
    }
    
    function drawSprite(sprite, subimg, x, y, w, h, angle){
      ctx.drawImage(sprite.source, Math.floor(subimg) * sprite.width, 0, w * sprite.imagenum, h, x - sprite.xoffset * (w/sprite.width), y - sprite.yoffset * (h/sprite.height), w, h);
    }
    

    All those var statements are actually doing nothing. It should be:

    function Sprite(){
      this.imagenum = null; //The number of images
      this.width = null; //The width of each image
      this.height = null; //The height on each image
      this.xoffset = null; //The origin on each image
      this.yoffset = null;
      this.source = null; //The location of each image
    }
    

    in order to correctly set them as you were envisioning. Also, you can rewrite drawSprite so that the sprites are drawing themselves, so that you don’t need to pass them as an argument:

    // now we can use "this" instead of "sprite"
    Spite.prototype.draw = function(subimg, x, y, w, h, angle){
        // put this on several lines just so we can see it easier
        ctx.drawImage(this.source,
          Math.floor(subimg) * this.width,
          0,
          w * this.imagenum, h,
          x - this.xoffset * (w/this.width),
          y - this.yoffset * (h/this.height),
          w, h);
        }
    

    Then instead of:

    drawSprite(img, index, 128, 128, 32, 32, 0); // img is a sprite
    

    We can write:

    img.draw(index, 128, 128, 32, 32, 0); // img is a sprite
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning about history in HTML5, in this example (open the JavaScript browser
Learning a little about T-SQL, and thought an interesting exercise would be to generate
Im just learning javascript and I'm just wondering why this doesn't work. I've created
I am a beginner in JavaScript/HTML. I'm learning my way around the DOM and
We are starting to create an application using JavaScript and HTML5 which will use
I am learning to code JavaScript. In this script, I want a link to
I just started learning JavaScript and am wondering why this simple snippet hangs when
I'm a desktop application developer, and I plan learning html5, but as it's not
I'm very interested in learning about the new feature in HTML5 called web sockets.
I've been asked to create a stand-alone webapp using straight HTML and Javascript that

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.