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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:55:38+00:00 2026-05-28T00:55:38+00:00

I cannot figure out how to do this. I was translating the character and

  • 0

I cannot figure out how to do this. I was translating the character and background at the same time, but if there’s any hiccup, the character position slides out of the viewable area of the canvas, and I need the canvas translation to be based off the position of the player (hero.x, hero.y).

Currently I have

var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 640;
canvas.height = 480;

//then in my update function
if (38 in keysDown && hero.y > 0){ //UP KEY PRESSED KEY
ctx.translate(0,12);   //translate background position +12y
hero.y -= hero.speed * modifier; //move player up on the background image

else if (40 in keysDown && hero.y < 3750-64){ //DOWN KEY PRESSED
ctx.translate(0, -12); 
hero.y += hero.speed * modifier;    
}
}

That moves the player and the canvas but not guaranteed together…if it freezes at all, the player is off center or even off screen.

The viewable canvas area is 640×480, but the background image you can navigate on is 5,000 x 3750.

On the web browser, when it doesn’t freeze, it works how I want, moving the player and background at the same pace as the character.

However, that same rate on the phone puts the player much faster than the screen translates which means the player walks right out of the viewable area even though it still moves the background.

If I do ctx.translate(hero.x, hero.y) and use the hero.x, hero.y coordinates of the player, or some variation of it minus an offset, it moves the background BY that measurement each time I press the key instead of moving it TO that position.

How can I make everything conditional on the players position to move both the player and background, but together, or automatically adjust next update() to center on the player….?????

  • 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-28T00:55:39+00:00Added an answer on May 28, 2026 at 12:55 am

    How can I make everything conditional on the players position to move both the player and background, but together, or automatically adjust next update() to center on the player

    Well, the easy way would be to actually always draw the player in the center! In other words, never ever change or translate his or her coordinates. Instead worrying about translating everything else in relation to that.

    Since you want the player to always be in the center, you should always draw the player at the center of the canvas (640/2 x 480/2 for you)

    Then you’ll want to do is keep a canvas offset for X and Y and draw everything (Background, etc) using that offset, then reset the transformation and draw the hero in the plain old center.

    So your draw function might look something like this:

    function draw() {
        ctx.clearRect(0,0,500,500);
        // Save the default transformation matrix
        ctx.save();
        // Translate by the background's offset
        ctx.translate(xoffset, yoffset);
        // Draw the background (and maybe other things) translated
        ctx.fillStyle = backgroundGradient; 
        ctx.fillRect(0,0,500,500);  
        // We restore to the default transformation
        // This will draw the hero not translated at all
        // That means we can always draw the hero in the center!
        ctx.restore();
        // hero is a black rectangle
        ctx.fillRect(240, 240, 20, 20);
    }
    

    Here is a live example that works with mouse down and up. There is a big “sun” for the background that moves, while the “stays” rectangle stays still because it is literally always drawn in the same place:

    http://jsfiddle.net/3CfFE/

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

Sidebar

Related Questions

Cannot figure this out, how to find the translated position of the background relative
I am learning to use polymorphism in C#, but cannot figure out this one.
I just cannot figure this out, it looks really simple but I'm relatively new
I know this is probably really obvious, but I cannot figure out why I
I cannot figure out why this code works locally on my PC (localhost) but
I apologize if this is a newbie question, but I cannot figure out why
I cannot figure out why this is not working. I get the same thing
i've just seen this strange thing in a tutorial, but cannot figure out what
After loosing much sleep I still cannot figure this out: The code below (its
I cannot figure out why I get this error during check-in. I checked in

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.