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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:24:03+00:00 2026-06-16T21:24:03+00:00

I am trying to create a game using HTML5 Canvas and Javascript and I

  • 0

I am trying to create a game using HTML5 Canvas and Javascript and I am unable to figure out how to (correctly) make the character move diagonally after clicking the canvas with the mouse. I did manage to make the character move diagonally, but I can’t set a speed so that regardless of the distance, it will move at the same speed… the further away you click, the faster it moves and the closer you click, the slower it moves. I want the speed to be the same regardless of where you click.

Here is what I have so far: http://pastebin.com/hUJBiQHq

How can I move the character diagonally on the canvas?

  • 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-16T21:24:04+00:00Added an answer on June 16, 2026 at 9:24 pm

    I actually went ahead and made quite a few changes. Ill try and go over each one.

    The first thing I did was remove a lot of the setTimout calls. You don’t really need a seperate one for movement and rendering. I also made it stop checking if the game was loaded after its actually loaded. Now once the game is loaded it will begin rendering.

    You also had your onclick event inside of the timeout which wasn’t needed. I moved it outside. A better approach and a subject for future reading would be using addEventListener explained nicely here

    Another thing I added right at the top is a shim for requestAnimationFrame it is much better to use this over setTimeout and if you notice if it runs on a browser that doesn’t support animation frame it will fallback on a timeout. There are many benefits to using it explained here.

    Now onto the issue at hand!

     var tx = newX - posX,
         ty = newY - posY,
    

    tx and ty are target x, and target y. They are the mouse coords subtracted from the player coords. You then can use these to perform a distance check like so,

    dist = Math.sqrt(tx * tx + ty * ty);
    

    Next I check if the distance is greater than the speed, if it is we need to move closer to our position.

    if (dist >= speed) {
    

    The next part calculates the velocity needed. We take the target x, and target y, and divide those by the distance which will give use the amount of pixels we need to travel to get to the target essentially. We then multiply this number by speed which gives us distance to move per tick. You then add these velocities to the players position to move the player (better explanation needed.. im terrible at explaining any math concepts :?)

        velX = (tx / dist) * speed;
        velY = (ty / dist) * speed;
        posX += velX;
        posY += velY;
    

    Live Demo of it all working

    Full Code

    (function () {
      var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
      window.requestAnimationFrame = requestAnimationFrame;
    })();
    
    var canvas = document.getElementById('game');
    var context = canvas.getContext('2d');
    
    canvas.width = canvas.height = 500;
    
    var gameReady = true;
    var players = [];
    var posX = 350;
    var posY = 200;
    var newX = 350;
    var newY = 200;
    
    // new vars needed for movement
    var velX = 0;
    var velY = 0;
    var speed = 5;
    
    function movePlayer() {
    
      var tx = newX - posX,
        ty = newY - posY,
        dist = Math.sqrt(tx * tx + ty * ty);
    
      if (dist >= speed) {
        velX = (tx / dist) * speed;
        velY = (ty / dist) * speed;
        posX += velX;
        posY += velY;
      }
    }
    
    function isGameReady() {
      if (gameReady) {
        drawCanvas();
      } else {
        setTimeout(isGameReady, 100);
      }
    }
    
    canvas.onmousedown = function (e) {
      newX = e.offsetX; // -33;
      newY = e.offsetY; // - 55.25;
    }
    
    
    function drawCanvas() {
      movePlayer();
      context.clearRect(0, 0, canvas.width, canvas.height);
      context.fillRect(posX, posY, 10, 10);
      requestAnimationFrame(drawCanvas);
    }
    
    isGameReady();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a canvas game using the proccessing.js library. I use
I'm trying to create a multiplayer game using websockets, node and JavaScript. What is
I am trying to create a board game using html/css/javascript to be played on
I'm trying to create a game of memory using Javascript/jQuery. Basically, a random array
I am trying to create a platformer game and i am trying to make
I'm using VS2010 express to create a game built with xna . I'm trying
I'm trying to make this 2-player web game application using Spring MVC. I have
I am trying to create a game in using wp7 ( windows phone 7
I'm trying to create a game using the best practices to my knowledge. I
I'm trying to create a chess game board with 8x8 buttons. I'm using nested

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.