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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:52:49+00:00 2026-05-16T07:52:49+00:00

I’ve been messing around with the canvas element in html5, and this is what

  • 0

I’ve been messing around with the canvas element in html5, and this is what I’ve got after a bit of experimenting

function canvasMove(e) {

    var canvas = document.getElementById('game');

    if(canvas.getContext) {
        var draw = canvas.getContext('2d');

        draw.fillStyle = 'rgba(0,0,0,0.5)';
        draw.fillRect('10', '10', '100', '100');    

        var code;

        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        var character = String.fromCharCode(code);

        if(character == '&') { draw.translate(0, -10); }
        if(character == '(') { draw.translate(0, 10); }
        if(character == '%') { draw.translate(-10, 0); }
        if(character == "'") { draw.translate(10, 0); }
    }
}

What it does is moves the rectangle whenever you press the arrow keys [Arrow keys were showing up as &, (, % and ‘, not sure if this is the same for everyone but it’s just an experiment]. Anyway, I can move the rectangle about but it leaves a sort of residue, as in it doesn’t delete it’s previous form, so what I get is a very basic etch-n’-sketch using a very thick brush.

What I want to do is be able to delete the previous form of the rectangle so that only the new translated version is left.

On top of that I’d like to know how to make it move say, horizontally, by pressing maybe left and up simultaneously. I am aware my code probably isn’t very versatile, but any help us much appreciated.

Thanks 🙂

  • 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-16T07:52:49+00:00Added an answer on May 16, 2026 at 7:52 am

    I made an example for you. Your HTML has to call my init() function. I used:

    <body onLoad="init()">
    

    Let me know if you have any problems with it

    var canvas;
    var draw;
    
    var WIDTH;
    var HEIGHT;
    
    var x = 10;
    var y = 10;
    
    // in my html I have <body onLoad="init()">
    function init() {
        canvas = document.getElementById('game');
        HEIGHT = canvas.height;
        WIDTH = canvas.width;
        draw = canvas.getContext('2d');
    
        // every 30 milliseconds we redraw EVERYTHING.
        setInterval(redraw, 30);
    
        // canvas.keydown = canvasMove;
    
        document.onkeydown = canvasMove; 
    }
    
    //wipes the canvas context
    function clear(c) {
        c.clearRect(0, 0, WIDTH, HEIGHT);
    }
    
    //clears the canvas and draws the rectangle at the appropriate location
    function redraw() {
        clear(draw);
        draw.fillStyle = 'rgba(0,0,0,0.5)';
        draw.fillRect(x, y, '100', '100');   
    }
    
    function canvasMove(e) {
      if(e.keyCode == '38') { y -= 1; }
      if(e.keyCode == '40') { y += 1; }
      if(e.keyCode == '37') { x -= 1; }
      if(e.keyCode == "39") { x += 1; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.