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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:27:39+00:00 2026-05-24T02:27:39+00:00

So I’m trying to create a method where the you move an image around

  • 0

So I’m trying to create a method where the you move an image around a canvas element. This is relevant in that in creating many kinds of games, you’d need a background image to move around properly against the canvas and the player’s movement. The problem is that you always draw relative to the canvas’s (0,0) point in the top left corner. So what I’m going for in a conceptualization where pressing right (for example) would be conceived as moving the CANVAS right, when really you’re moving the image left. It could be argued that this is unnecessary, but honestly thinking about it the other way kind of gives me a headache. I think this way of relating everything to a larger absolute field would be easier to program with a large number of objects.

The problem is, I’ve messed around with my code in Pycharm but I keep getting canvas not defined and similar errors. Please help me fix this up! So without further ado, here’s my code! (and any other ways to clean up my code is appreciated, I’m pretty new to JS!)

//Animates a moving black dot on the canvas.

//Variables for parameters
var gameloopId;
var speed=6;
var canvas;
var background;
var circle;
var ctx;

//Wait for document to be ready then start
$(document).ready(function(){
    console.log('document is ready');
    init();

});

//Holds the relative coordinates.
function Canvas(){
    this.x=0;//relative X
    this.y=0;//relative Y
    //Calulate screen height and width
    this.width = parseInt($("#canvas").attr("width"));
    this.height = parseInt($("#canvas").attr("height"));
}
canvas=new Canvas();

//Define an object
function Object(){
    this.absX=0;
    this.absY=0;
    this.x=this.absX-canvas.x;
    this.y=this.absY-canvas.y;
}

//Circle Object
function Circle(radius){
    this.radius=radius;
}
Circle.prototype= new Object(); //Circle is an Object
function drawCircle(){
        // Create the circle
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#000000";
        ctx.beginPath();
        ctx.arc(circle.x,circle.y,circle.radius,0,Math.PI*2,true);
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
    }
Background= Image();
Background.prototype=new Object(); //Background is an Object
background= new Background()

function drawBackground(){
        //draw the background
        ctx.drawImage(background,background.x,background.y);
    }

function init(){
    console.log('function init()');
    initSettings();


    //Insert event handler for keyboard movement of circle (space clearInterval)
    $(document).keydown(function(e){

        if(e.keyCode=='37'){    //Left key
            circle.absX+=speed;
            canvas.x+=speed;}

        if(e.keyCode=='38'){    //Up key
            circle.absY-=speed;
            canvas.y-=speed;}

        if(e.keyCode=='39'){    //Right key
            circle.absX+=speed;
            canvas.x+=speed;}

        if(e.keyCode=='40'){    //Down key
            circle.absX+=speed;
            canvas.y+=speed;}

        if(e.keyCode=='32'){    //Space Bar
            console.log('spacebar');
            clearInterval(gameloopId);
            initSettings();
            gameloopId = setInterval(gameLoop,10);
        }
    });

    $(document).keyup(function(e){

        if(e.keyCode=='37'){
        console.log('left');}//Left key

        if(e.keyCode=='38'){
        console.log('up');}//Up key

        if(e.keyCode=='39'){
        console.log('right');}//Right key

        if(e.keyCode=='40'){
        console.log('down');}//Down key
    });

    //Initialize loop of "game"
    gameloopId = setInterval(gameLoop,10);
}

function initSettings(){
    console.log('initSettings');

    //Set up canvas
    ctx = document.getElementById('canvas').getContext('2d');

    //center circle on the horizontal axis
    console.log('setting circle coords');
    circle = new Circle(15);
    circle.x = parseInt(canvas.width/2);
    circle.y = canvas.height - 40;

    //Put background at (0,0)
    background.x=0;
    background.y=0;
    background.src="http://127.0.0.1:8000/static/back.jpg";
    console.log("background width:"+background.width);
    console.log("background height:"+background.height);
}

function gameLoop(){
    //console.log('function gameLoop()');

    //Has it reached far left side?
    if(circle.x<circle.radius)
    {
        circle.x=circle.radius
    }

    //Has it reached far right side?
    if(circle.x>canvas.width - circle.radius)
    {
        circle.x=canvas.width - circle.radius
    }

    //Has it reached top?
    if(circle.y<circle.radius)
    {
        circle.y=circle.radius
    }

    //has it reached bottom?
    if(circle.y>canvas.height - circle.radius)
    {
        circle.y=canvas.height - circle.radius
    }

    //has background reached left?
    if(background.x < canvas.width-background.width)
    {
        background.x= canvas.width-background.width;
    }

    //has background reached right?
    if(background.x>0)
    {
        background.x=0;
    }

    //has background reached top?
    if(background.y < canvas.height-background.height)
    {
        background.y = canvas.height-background.height;
    }

    //has background reached bottom?
    if(background.y>0)
    {
        background.y=0;
    }

    //Clear the screen (i.e. a draw a clear rectangle the size of the screen)
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.save();

    //draw background
    drawBackground();
    // draw the circle
    drawCircle();


    ctx.restore();

}

EDIT:(UPDATED CODE!)

//Animates a moving black dot on the canvas.

//Variables for parameters
var gameloopId;
var speed=6;
var canvas;
var background;
var circle;
var ctx;

//Wait for document to be ready then start
$(document).ready(function(){
    console.log('document is ready');
    init();

});

//Holds the relative coordinates.
function Canvas(){
    this.x=0;//relative X
    this.y=0;//relative Y
    //Calulate screen height and width
    this.width = parseInt($("#canvas").attr("width"));
    this.height = parseInt($("#canvas").attr("height"));
}

//Define an object
function MyObject(){
    this.absX=0;
    this.absY=0;
    this.x=this.absX-canvas.x;
    this.y=this.absY-canvas.y;
}

//Circle MyObject
function Circle(radius){
    this.radius=radius;
}
Circle.prototype= new MyObject(); //Circle is an MyObject
function drawCircle(){
        // Create the circle
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#000000";
        ctx.beginPath();
        ctx.arc(circle.x,circle.y,circle.radius,0,Math.PI*2,true);
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
    }
function Background(){
    this.img= Image();
}
Background.prototype=new MyObject(); //Background is an MyObject

function drawBackground(){
        //draw the background
        ctx.drawImage(background,background.x,background.y);
    }

function init(){
    console.log('function init()');
    initSettings();


    //Insert event handler for keyboard movement of circle (space clearInterval)
    $(document).keydown(function(e){

        if(e.keyCode=='37'){    //Left key
            circle.absX+=speed;
            canvas.x+=speed;}

        if(e.keyCode=='38'){    //Up key
            circle.absY-=speed;
            canvas.y-=speed;}

        if(e.keyCode=='39'){    //Right key
            circle.absX+=speed;
            canvas.x+=speed;}

        if(e.keyCode=='40'){    //Down key
            circle.absX+=speed;
            canvas.y+=speed;}

        if(e.keyCode=='32'){    //Space Bar
            console.log('spacebar');
            clearInterval(gameloopId);
            initSettings();
            gameloopId = setInterval(gameLoop,10);
        }
    });

    $(document).keyup(function(e){

        if(e.keyCode=='37'){
        console.log('left');}//Left key

        if(e.keyCode=='38'){
        console.log('up');}//Up key

        if(e.keyCode=='39'){
        console.log('right');}//Right key

        if(e.keyCode=='40'){
        console.log('down');}//Down key
    });

    //Initialize loop of "game"
    gameloopId = setInterval(gameLoop,10);
}

function initSettings(){
    console.log('initSettings');

    //Set up canvas
    canvas=new Canvas();
    ctx = document.getElementById('canvas').getContext('2d');

    //center circle on the horizontal axis
    console.log('setting circle coords');
    circle = new Circle(15);
    circle.x = parseInt(canvas.width/2);
    circle.y = canvas.height - 40;

    //Put background at (0,0)
    background= new Background();
    background.x=0;
    background.y=0;
    background.img.src="http://127.0.0.1:8000/static/back.jpg";
    console.log("background width:"+background.width);
    console.log("background height:"+background.height);
}

function gameLoop(){
    //console.log('function gameLoop()');

    //Has it reached far left side?
    if(circle.x<circle.radius)
    {
        circle.x=circle.radius
    }

    //Has it reached far right side?
    if(circle.x>canvas.width - circle.radius)
    {
        circle.x=canvas.width - circle.radius
    }

    //Has it reached top?
    if(circle.y<circle.radius)
    {
        circle.y=circle.radius
    }

    //has it reached bottom?
    if(circle.y>canvas.height - circle.radius)
    {
        circle.y=canvas.height - circle.radius
    }

    //has background reached left?
    if(background.x < canvas.width-background.width)
    {
        background.x= canvas.width-background.width;
    }

    //has background reached right?
    if(background.x>0)
    {
        background.x=0;
    }

    //has background reached top?
    if(background.y < canvas.height-background.height)
    {
        background.y = canvas.height-background.height;
    }

    //has background reached bottom?
    if(background.y>0)
    {
        background.y=0;
    }

    //Clear the screen (i.e. a draw a clear rectangle the size of the screen)
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.save();

    //draw background
    drawBackground();
    // draw the circle
    drawCircle();


    ctx.restore();

}



enter code here
  • 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-24T02:27:40+00:00Added an answer on May 24, 2026 at 2:27 am

    I figured it out! I had a lot of stupid stuff in my ode and lots of bugs – for example background.img is an Image, but all over the place I was trying to get background.width instead of background.img.width. I also refactored several functions to make things prettier (to em at least). Thanks to the above for your help! Here’s my “final” code, at least as of right now:

    //Animates a moving black dot on the canvas.
    
    //Variables for parameters
    var gameloopId;
    var speed=6;
    //var canvas;
    var background;
    var circle;
    var ctx;
    
    //Wait for document to be ready then start
    $(document).ready(function(){
        console.log('document is ready');
        init();
    
    });
    
    //Holds the relative coordinates.
    var canvas = new function Canvas(){
        this.x=0;//relative X
        this.y=0;//relative Y
        //Calulate screen height and width
        this.width = parseInt($("#canvas").attr("width"));
        this.height = parseInt($("#canvas").attr("height"));
    };
    
    //Define an object
    function MyObject(){
        this.absX=0;
        this.absY=0;
        this.x=this.absX-canvas.x;
        this.y=this.absY-canvas.y;
    
        this.updateplace = function (){
            this.x=this.absX-canvas.x;
            this.y=this.absY-canvas.y;
        };
    }
    
    //Circle MyObject
    function Circle(radius){
        this.radius=radius;
        this.draw=function(){
            // Create the circle
            ctx.strokeStyle = "#000000";
            ctx.fillStyle = "#000000";
            ctx.beginPath();
            ctx.arc(circle.x,circle.y,circle.radius,0,Math.PI*2,true);
            ctx.closePath();
            ctx.stroke();
            ctx.fill();
        }
    }
    Circle.prototype= new MyObject(); //Circle is an MyObject
    
    function Background(){
        this.img= Image();
        this.draw=function(){
            ctx.drawImage(background.img,background.x,background.y);
        }
    }
    Background.prototype=new MyObject(); //Background is an MyObject
    
    function init(){
        console.log('function init()');
        initSettings();
    
    
        //Insert event handler for keyboard movement of circle (space clearInterval)
        $(document).keydown(function(e){
    
            if(e.keyCode=='37'){    //Left key
                circle.absX-=speed;
                canvas.x-=speed;}
    
            if(e.keyCode=='38'){    //Up key
                circle.absY-=speed;
                canvas.y-=speed;}
    
            if(e.keyCode=='39'){    //Right key
                circle.absX+=speed;
                canvas.x+=speed;}
    
            if(e.keyCode=='40'){    //Down key
                circle.absY+=speed;
                canvas.y+=speed;}
    
            if(e.keyCode=='32'){    //Space Bar
                console.log('spacebar');
                clearInterval(gameloopId);
                initSettings();
                gameloopId = setInterval(gameLoop,10);
            }
        });
    
        $(document).keyup(function(e){
    
            if(e.keyCode=='37'){
            console.log('left');}//Left key
    
            if(e.keyCode=='38'){
            console.log('up');}//Up key
    
            if(e.keyCode=='39'){
            console.log('right');}//Right key
    
            if(e.keyCode=='40'){
            console.log('down');}//Down key
        });
    
        //Initialize loop of "game"
        gameloopId = setInterval(gameLoop,10);
    }
    
    function initSettings(){
        console.log('initSettings');
    
        //Set up canvas
        ctx = document.getElementById('canvas').getContext('2d');
        canvas.width = parseInt($("#canvas").attr("width"));
        canvas.height = parseInt($("#canvas").attr("height"));
    
        //center circle on the horizontal axis
        console.log('setting circle coords');
        circle = new Circle(15);
        circle.absX = parseInt(canvas.width/2);
        circle.absY = canvas.height - 40;
    
        //Put background at (0,0)
        background= new Background();
        background.x=0;
        background.y=0;
        background.img.src="http://127.0.0.1:8000/static/back.jpg";
        console.log("background width:"+background.img.width);
        console.log("background height:"+background.img.height);
        console.log("Right Bound:"+(background.img.width- canvas.width))
    }
    
    function gameLoop(){
        //console.log('function gameLoop()');
    
        //Has it reached far left side?
        if(circle.absX<circle.radius)
        {
            circle.absX=circle.radius
        }
    
        //Has it reached far right side?
    
        if(circle.absX>background.img.width - circle.radius)
        {
            circle.absX=background.img.width - circle.radius
        }
    
        //Has it reached top?
        if(circle.absY<circle.radius)
        {
            circle.absY=circle.radius
        }
    
        //has it reached bottom?
        if(circle.absY>background.img.height - circle.radius)
        {
            circle.absY=background.img.height - circle.radius
        }
    
        //has canvas reached right bound?
        if(canvas.x > background.img.width- canvas.width)
        {
            canvas.x= background.img.width- canvas.width;
        }
    
        //has canvas reached left bound?
        if(canvas.x<0)
        {
            canvas.x=0;
        }
    
        //has background reached bottom bound?
        if(canvas.y > background.img.height - canvas.height)
        {
            canvas.y = background.img.height - canvas.height;
        }
    
        //has background reached top bound?
        if(canvas.y<0)
        {
            canvas.y=0;
        }
    
        //Clear the screen (i.e. a draw a clear rectangle the size of the screen)
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.save();
    
        //draw background
        background.updateplace();
        background.draw();
        // draw the circle
        circle.updateplace();
        circle.draw();
    
    
        ctx.restore();
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.