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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:46:41+00:00 2026-06-10T08:46:41+00:00

I am new to JavaScript and am trying to learn it pretty fast. I

  • 0

I am new to JavaScript and am trying to learn it pretty fast. I need help with the keyboard event keys. I am trying to move an image in JavaScript using WASD. I can get it to move up, down, left, and right, but I cannot figure out how to get it to move diagonally. Help is greatly appreciated.

"use strict";

var cvs;
var ctx;
var imagex=100;
var imagey=100;

function keydown_callback(ev){
    if(ev.keyCode === 68 )
        imagex += 5;
    if(ev.keyCode === 65 )
        imagex -= 5;
    if(ev.keyCode === 87 )
        imagey -= 5;
    if(ev.keyCode === 83 )
        imagey += 5;
    draw();
 }

function draw(){
    ctx.clearRect(0,0,600,600);
    var background = new Image();
    background.src="wallpaper.jpg";
    ctx.drawImage(background, 0, 0, 600, 600);
    var img = new Image();
    img.src="ninja.png";
    ctx.drawImage(img,imagex,imagey,128,256);
}

function main(){
    var tmp = document.getElementsByTagName("body");
    var body = tmp[0];
    body.addEventListener("keydown",keydown_callback);
    cvs = document.getElementById("foo");
    ctx = cvs.getContext("2d");
    draw();
}
  • 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-10T08:46:43+00:00Added an answer on June 10, 2026 at 8:46 am

    See http://jsfiddle.net/7xkD7/1/ (click first to the result cell)

    "use strict";
    var cvs,
        ctx,
        imagex=100,
        imagey=100,
        mov={'x+':0,'x-':0,'y+':0,'y-':0};
    function get(thing,key){
        for(var i in window[thing]){
            if(window[thing][i].indexOf(key)!==-1){return i;}
        }
        return false;
    }
    function changeMov(key,val){
        switch(key){
            case 68:case 39:
                mov['x+']=val;break;
            case 65:case 37:
                mov['x-']=val;break;
            case 87:case 38:
                mov['y-']=val;break;
            case 83:case 40:
                mov['y+']=val;break;
            default:return false;
        }
    }
    
    function keydown_callback(ev){
        var key=ev.which||ev.keyCode;
        if(changeMov(key,1)===false){return;}
        draw();
        ev.preventDefault();
        return false;
    }
    function keyup_callback(ev){
        var key=ev.which||ev.keyCode;
         if(changeMov(key,0)===false){return;}
    }
    function draw(){
        imagex+=5*(mov['x+']-mov['x-']);
        imagey+=5*(mov['y+']-mov['y-']);
        ctx.clearRect(0,0,600,600);
        var background = new Image();
        background.src="http://images1.videolan.org/images/largeVLC.png";
        ctx.drawImage(background, 0, 0, 100, 100);
        var img = new Image();
        img.src="http://www.nikon.com/swf/img/thumbnail_ophthalmic_lenses.png";
        ctx.drawImage(img,imagex,imagey,128,256);
    }
    
    function main(){
        var body = document.body;
        body.addEventListener("keydown",keydown_callback);
        body.addEventListener("keyup",keyup_callback);
        cvs = document.getElementById("foo");
        ctx = cvs.getContext("2d");
        draw();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm completely new to the javascript and ajax world but trying to learn. Right
New to javascript and such, trying to create a loop for fading logos using
I'm new to Javascript and I was trying to figure out how to move
I'm new to javascript and I'm trying to learn. I'd like my webpage to
I'm currently trying to learn Knockout.JS - I'm also relatively new to JavaScript itself.
I'm trying to learn how to add new methods in the String.prototype in JavaScript.
I'm new to JavaScript and trying to learn more about what it can do
I'm new to JavaScript and I'm trying to learn it from internet resources. While
i'm trying to learn regex parse in javascript. I need to read the text
I am new to JavaScript and am trying to learn how to upload images

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.