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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:41:45+00:00 2026-06-06T23:41:45+00:00

I m Drawing the image on canvas with this code and it successfully draw

  • 0

I m Drawing the image on canvas with this code
and it successfully draw the image on canvas now i want to move the image on canvas for that i write the code i check that if the right key of my keyboard is pressed i will increment the x coordinate of an image if left key is pressed i will decrement the x coordinate but image is not moving on the canvas

player = new Image();
player.src = "game_character.png";
context.drawImage(player,player.x * wallDim + wallDim ,player.y * wallDim + wallDim ,50,50);

how to move an image on canvas

 var handleInput = function(event, keyState) {
        switch(event.which) {
              case 37: { // Left Arrow
                    keyDown.arrowLeft = keyState;
                    break;
              }
              case 38: { // Up Arrow
                    keyDown.arrowUp = keyState;
                    break;
              }
              case 39: { // Right Arrow
                    keyDown.arrowRight = keyState;
                    break;
              }
              case 40: { // Down Arrow
                    keyDown.arrowDown = keyState;
                    break;
              }
        }
  }

  /**
  * physics
  *
  * This function contains the basic logic for the maze.
  */
  var physics = function() {
       console.log("physics ");
       console.log("first condition "+keyDown.arrowRight +player.x+1);
        if(keyDown.arrowLeft && player.x-1 >= 0 && map[player.y][player.x-1] != 1) {
              player.x--;
              redraw = true;
        }

        if(keyDown.arrowUp && player.y-1 >= 0 && map[player.y-1][player.x] != 1) {
              player.y--;
              redraw = true;
        }

        if(keyDown.arrowRight && player.x+1 < map[0].length && map[player.y][player.x+1] != 1) {
              console.log("arrow right");
              player.x++;
              redraw = true;
        }

        if(keyDown.arrowDown && player.y+1 < map.length && map[player.y+1][player.x] != 1) {
              player.y++;
              redraw = true;
        }
        if(keyDown.arrowRight && player.x+1 >= map[0].length)
        {
            player.x++;
            document.getElementById("canvas_div").style.display="none";
            document.getElementById("end_screen_div").style.display="block";
            //alert("completed");
        }
  }

  /**
  * draw
  *
  * This function simply draws the current state of the game.
  */
  var draw = function() {

        // Don't redraw if nothing has changed
        if(!redraw)
              return;

        context.clearRect(0, 0, cols, rows);
        context.beginPath();

        // Draw the maze
        for(var a = 0; a < rows; a++) {
              for(var b = 0; b < cols; b++) {
                    switch(map[a][b]) {
                          case C.EMPTY: context.fillStyle = colors.empty; break;
                          case C.WALL: context.fillStyle = colors.wall; break;
                    }

                        context.fillRect(b * wallDim, a * wallDim, wallDim, wallDim); // x, y, width, height
              }
        }

        // Draw the player
     /* context.fillStyle = colors.player;
        context.arc(
              player.x * wallDim + wallDim / 2, // x position
              player.y * wallDim + wallDim / 2, // y position
              wallDim / 2, // Radius
              0, // Starting angle
              Math.PI * 2, // Ending angle
              true // antiClockwise
        );*/


    player = new Image();
    player.src = "game_character.png";

    context.drawImage(player,player.x * wallDim + wallDim ,player.y * wallDim + wallDim ,50,50);

    var firstplayer=new Image();
    firstplayer.src="top_character01.png";

    context.drawImage(firstplayer,680,0,60,60);

    var secondplayer= new Image();
    secondplayer.src="top_character02.png";

    context.drawImage(secondplayer,750,0,60,60);

    context.fill();
    context.closePath();

        redraw = false;
  }
  • 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-06T23:41:46+00:00Added an answer on June 6, 2026 at 11:41 pm

    In your draw method, you reinitialize the player each time :

    player = new Image();
    player.src = "game_character.png";
    

    So you erase the player.x modified by your event handler.

    You should initialize the player only once, outside the draw function. You can move the initialization like this :

    var player = new Image();
    player.src = "game_character.png";
    var draw = function() {
    

    There is absolutely no need to call player.src = "game_character.png"; inside the draw function.

    As a general rule, when dealing with animation, try to remove all what you can from the draw function, which should be as fast as possible.

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

Sidebar

Related Questions

I'm drawing an image to a canvas element. I then have code that depends
On drawing on canvas if i use this code to draw the bitmap then
How to do in HTML5 canvas Image animating? I am have this code now:
I'm drawing an Image on the canvas using the drawImage function. This is how
When I try to draw an image to a Canvas element I get this
I have the following simple code that draws rectangle <Canvas Name=MainImageLayer > <Image >
I'm using fill() while drawing this image in my canvas. I load the image
I'm drawing text in an HTML5 canvas on top of an image (a meme
I have a problem with drawing image. How I can draw image in precision
I'm drawing an image to a canvas (using the cityTexture method in http://haldean.github.com/citycanvas/city.js )

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.