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

  • Home
  • SEARCH
  • 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 8706457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:37:07+00:00 2026-06-13T03:37:07+00:00

So I’m attempting to write a simple sprite loading method using a canvas, and

  • 0

So I’m attempting to write a simple sprite loading method using a canvas, and im noticing a terrible refresh rate. This is the draw method I’m using.

function DrawSprite(context,
                drawX,
                drawY,
                drawH,
                drawW,
                spriteXIndex,
                spriteYIndex,
                spriteW,
                spriteH,
                image){
    var imageObj = new Image();
    imageObj.onload = function() {
        //draw cropped image
        var sourceX = (spriteXIndex * spriteW);
        var sourceY = (spriteYIndex * spriteH);

        context.drawImage(imageObj, sourceX, sourceY, spriteW, spriteH, drawX, drawY, drawW, drawH);
    };
    imageObj.src = image;

}

When ever this is called i get a very noticeable redraw of the image. I’ve thought about a couple solutions but I’m not sure the best method of implementing them. First is a double buffer. But im not sure how to implement that other than creating a second canvas and drawing it underneath and handle the swap using z-index. The other is storing the images and not recreating it every time I want to draw a sprite. I’m going to try the second method first and preload the images before I start the program, but I was hoping for input regarding the best way to handle this.

  • 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-13T03:37:08+00:00Added an answer on June 13, 2026 at 3:37 am

    What you seem to be doing is creating a new image and loading it from the source everytime which will definitely cause performance issues. The best way to handle it is to load the image once save and reuse it. Below is a very simple example. The fiddle is a little more complex on the loading portion since Im loading an image from a service http://retroships.com

    function Sprite(options){
        this.load(options.imagePath);  
        this.x = 0;
        this.y = 50;
        this.isLoaded = false;
    }
    
    Sprite.prototype.load = function(imagePath){
        this.image = new Image();
        var that = this;   
    
        this.image.src = imagePath;
    
        this.image.onload = function(){
            that.isLoaded = true;
        }
    }
    
    Sprite.prototype.draw = function(){
        ctx.drawImage(this.image, this.x, this.y);
    }
    
    // to use, you can pass other params and handle them like x/y/width/height what have you.
    var yourSprite = new Sprite({imagePath : "pathtoimage.png"});
    
    function render(){
        if(yourSprite.isLoaded){
            canvasContext.drawImage(yourSprite.image, yourSprite.x, yourSprite.y);
        }
    }
    

    Working Demo

    • 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’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of 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.