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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:42:13+00:00 2026-06-14T21:42:13+00:00

I am writing a simple page with JQuery and HTML5 canvas tags where I

  • 0

I am writing a simple page with JQuery and HTML5 canvas tags where I move a shape on the canvas by pressing ‘w’ for up, ‘s’ for down, ‘a’ for left, and ‘d’ for right. I have it all working, but I would like the shape to start moving at a constant speed upon striking a key. Right now there is some kind of hold period and then the movement starts. How can I get the movement to occur immediately?

Here is the important part of my code:

<script src="jquery.js"></script>
<script src="JQuery.print.js"></script>    
<body>
<canvas id="myCanvas" width="500" height="200" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<br><br>
start navigating<input type="text" id = "enter"/>
<div id = "soul2">
coords should pop up here
</div>
<div id = "soul3">
key should pop up here
</div>

<script>

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
//keypress movements
var xtriggered = 0;
var keys = {};
var north = -10;
var east = 10;
var flipednorth = 0;

$(document).ready(function(e){
  $("input").keydown(function(){
    keys[event.which] = true;
    if (event.which == 13) {
         event.preventDefault();
      }
    //press w for north
    if (event.which == 87) {
         north++;
         flipednorth--;
      }
    //press s for south
    if (event.which == 83) {
         north--;
         flipednorth++;
      }
    //press d for east
     if (event.which == 68) {
         east++;
      }
    //press a for west
    if (event.which == 65) {
         east--;
      }
     var  msg = 'x: ' + flipednorth*5 + ' y: ' + east*5;
     ctx.beginPath();
     ctx.arc(east*6,flipednorth*6,40,0,2*Math.PI);
     ctx.stroke();
     $('#soul2').html(msg);
    $('#soul3').html(event.which );
     $("input").css("background-color","#FFFFCC");
  });

  $("input").keyup(function(){
    delete keys[event.which];
    $("input").css("background-color","#D6D6FF");
  });
});

</script>

please let me know if I shouldn’t be posting code this lengthy.

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

    What you’re missing is a “game loop”.

    Instead of reacting directly to key down or up in deciding whether and when to move your shape around the canvas, you need to use the key events to keep track of which keys are down at any given moment, and then check the key state from a setTimeout()-based loop running independent of the key events.

    You’ve started to do that with your keys variable keeping track of whether a given key is down at any moment:

    // in keydown
    keys[event.which] = true;
    // in keyup
    delete keys[event.which];
    

    …except that event is not defined – you need to make it a parameter of the event handler, and also none of your code ever actually checks the values in keys.

    Here’s a simplified version of what I’m talking about:

    $(document).ready(function(e){
      var keys = {};
    
      $("input").keydown(function(event){
        keys[event.which] = true;
      }).keyup(function(event){
        delete keys[event.which];
      });
    
      function gameLoop() {
        // w for north
        if (keys[87]) {
           north++;
           flipednorth--;
        }
        //press s for south
        if (keys[83]) {
           north--;
           flipednorth++;
        }
        // etc. for other keys
    
        // code to move objects and repaint canvas goes here
    
        setTimeout(gameLoop, 20);
      }
      gameLoop();
    });
    

    Demo: http://jsfiddle.net/ktHdD/1/

    The gameLoop() function will run ever 20 milliseconds or so (you can vary this as needed, but that’s fast enough for reasonably smooth animation). Each time it runs it checks if any keys are down and if so adjusts the relevant position variables and repaints. If you have other objects that move around automatically (e.g., in a game there might be bad guys) you’d update them in the gameLoop() function too.

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

Sidebar

Related Questions

I'm writing a simple web page that displays a table. It the right column
I have a simple html5 test page which uses LocalStorage to display / save
I am writing one simple page in APS.Net with VB.Net as the scripting language.
I'm writing jQuery for a page that is a complex mess of many DOM
I have a form in jQuery where I update all the images via a
I'm writing a simple asp page to show a team rota based on some
I'm writing a simple jQuery that to change the font size of an element
I'm about to write a simple HTML5 + JavaScript (jQuery) app in my spare
I have created a simple file upload page. To keep it really simple, I
I'm writing a simple AJAX call that returns a full HTML page and then

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.