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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:36:55+00:00 2026-05-15T20:36:55+00:00

I have a sprite I’m animating on an html canvas using normal sprite sheet

  • 0

I have a sprite I’m animating on an html canvas using normal sprite sheet blitting. On certain key events I’d like to change the direction of the sprite (ie, flip it, or rotate it 180 degrees) without changing anything (the other sprites) on the canvas.

Does anyone know how to do 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-05-15T20:36:56+00:00Added an answer on May 15, 2026 at 8:36 pm

    While I appreciate Shtééf’s answer, after a bit of research, I have found that rotating the canvas you are actually using to display doesn’t seem to be ideal. The saving, rotating and restoring while trying to create complex animations (aka think Street Fighter 2 not astroids) causes the canvas to flicker in even Chrome.

    I have found however a usable strategy. The idea here is that you actually create two canvases, one will be for your game and the other will be a backbuffer of sorts and it will be used to rotate or scale your sprites. You essentially transform the backbuffer canvas, draw the image in question, then transfer it to your main canvas and restore (or not) the backbuffer. In this manner, you only rotate the hidden canvas and only effect the sprite in question not the entire game board.

    The code looks something like this (work in progress):

    mainContext.clearRect(lastXpos, lastYpos, lastWidth, lastHeight);
    backContext.clearRect(0, 0, lastWidth, lastHeight);
    lastXpos = xpos;
    lastYpos = ypos;
    lastWidth = width;
    lastHeight = height;
    
    
    backContext.save();
    
    //check the direction of the sprite
    //turn the backContext to this direction   
    //SPRITE_INVERTED==-1
    if (spriteXDirection == SPRITE_INVERTED || spriteYDirection == SPRITE_INVERTED) 
    {
        var horScale = 0;
        var verScale = 0;
    
        if (spriteXDirection == SPRITE_INVERTED)
        {
        horScale = width;
        }
    
        if (spriteYDirection == SPRITE_INVERTED)
        {
        verScale = height;
        }
    
    
        backContext.translate(horScale, verScale);
        backContext.scale(spriteXDirection, spriteYDirection);
    
    
    }
    
    
    //draw the sprite not we always use 0,0 for top/left
    backContext.drawImage(animations[currentPlay].sheet,
                    animationX,
                    animationY,
                    width,
                    height, 0, 0, width, height);
    
    //Get the image data from the back context
    var image = backContext.getImageData(0, 0, width, height);
    
    
    //flip the back context back to center - or not, I haven't decided how to optimize this yet.
    backContext.restore();
    
    
    //transfer the image to your main context
    mainContext.putImageData(image, xpos, ypos);
    

    This has saved me a lot of headaches in understanding how to translate my sprites without having everything on my gameboard move all over the place. It also seems to perform better then modifying the main context.

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

Sidebar

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.