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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:08:56+00:00 2026-05-18T04:08:56+00:00

May be some one has had similar experiences on this dilemma and can help

  • 0

May be some one has had similar experiences on this dilemma and can help me out here…

Basically, I have a canvas element on which I draw several rectangles in a loop using

context.fillRect (x, y, width, height)

Now, I want some of the rectangles to be hotspots and respond to click events. I can find out the exact (x,y) of a click event using event.layerX and event.layerY.

Given that I know the following:

  • the exact x,y of the click
  • the x,y,width and height of every rectangle

how do I find out if the click event occurred inside the perimeter a certain rectangle or not?
and,
which rectangle the click event occurred 0n?

Is there like a mathematical formula for this?

Any help would be much appreciated, and if I’m not clear enough, let me know…

Thanks

EDIT
Is there no better way than to loop through all rectangles and check their position and dimensions?

  • 1 1 Answer
  • 1 View
  • 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-18T04:08:56+00:00Added an answer on May 18, 2026 at 4:08 am

    This is a general topology problem frequently encountered when using maps.

    The algorithm for deciding whether a point is within ANY polygon (rectangles, circles, irregular shapes) is as follows:

    • From the point being checked construct any line in any direction until the edge of the screen area in which your polygons are located

    • if the line intersects any polygon boundary at an odd number of places then it is within that polygon

    • if the line intersects any polygon boundary an even number of places it is outside that polygon.

             ------------
             |           |
             |        o--|-------------------    1 intersection: inside
             |           |
             -------------
             ------------
             |           |
      o------|-----------|-------------------    2 intersections: outside
             |           |
             -------------
      

    Notes:

    • The direction of the line is irrelevant

    • Will not work if a polygon is cut at the side of the screen without closure

    • If the polygon cuts over itself then the algorithm can fail if the line
      happens to pass through the cutpoint (eg in the figure 8 considered as a
      single polygon where the line passes exactly through where upper and lower
      parts connect of the figure connect)

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

Sidebar

Related Questions

I'm wondering if someone can help with this or has had a similar problem.
Looking for some practical advice here and any experiences people have had in a
I have the following String Sample however in some occurences there may be one
I have a favorite table containing some fields like login_id,driver_id(One login_id may have many
I have an interesting problem to solve here that may require some creative direction.
I've been researching this one and found references to similar problems here and there,
Given a type (Candidate) which has muiltiple fields one may score (here one concrete
It may be a bit too specific, but maybe someone here has experience extending
When we want to modify some value in one object we may use two
Some background: an 'image' is part of one 'photoshoot', and may be a part

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.