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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:49:19+00:00 2026-05-21T11:49:19+00:00

Hi guys I work on a simple game: there si a blue square moving

  • 0

Hi guys
I work on a simple game: there si a blue square moving in a canvas, if you clic on the square you win if you clic outside you lose. I don’t know how to compare the mouse position with the square position…
Here is my source code

<head>  
<script type="text/javascript" src="jquery-1.5.2.min.js"></script>  
<script>
var context;
var x=6; // the start position
var y=6; // same
var dx=1; // the speed  
var dy=1; // the speed  


function init()
{
  context= myCanvas.getContext('2d');
  setInterval(draw,6); 
  clicrect();
}

function draw()
{
  context.clearRect(0,0, 300,300);
  context.beginPath();
  context.fillStyle="#0000ff";
  context.fillRect(x,y,50,50);
  context.closePath();
  context.fill();
  // Boundary Logic
if( x<1 || x>250-50) dx=-dx;
if( y<1 || y>300-50) dy=-dy;
x+=dx;
y+=dy;

}

function clicrect() {
$('#myCanvas').click(function (e)
{
    if (e.target == document.getElementById("myCanvas:animated"))  //that's where I am strugglin, I try to compare the mouse position with the blue square position, but the myCanvas:animated don't work.
        alert("In");
    else
        alert("Out!");
});
}

</script>
</head>
<body onLoad="init();">
  <canvas id="myCanvas" width="300" height="300" >
  </canvas>
</body>

thanks for your help 🙂

  • 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-21T11:49:20+00:00Added an answer on May 21, 2026 at 11:49 am

    Have a look at a this – looks like a reasonable place to start – elements in canvas are NOT in the DOM so jquery itself cannot interact directly

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

Sidebar

Related Questions

This will be simple for you guys: var uri = new Uri(pack://application:,,,/LiftExperiment;component/pics/outside/elevator.jpg); imageBitmap =
hey guys, i know how to create a simple php file that mails some
hello guys i have one simple program which copying itself. Its work great when
Ok guys just a small game: I have some specifications for a project. At
Hi guys anyone know what the helll is going on here? ERROR SNIPPET: Loading
Hay guys, I've wrote a simple upload method for my pictures class Picture(models.Model): path
Hey guys really quick question, I have a simple test that I was doing
Hey guys, just wondering if their is a simple way to create an Item
Hay guys, I'm doing some work where i have a large collection of forms.
Hey guys, this is simple but I can't make it workn... blah! I have

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.