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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:05:47+00:00 2026-06-06T19:05:47+00:00

I have this canvas mousedown event: $(‘#canvas’).mousedown(function(e) { var coords = canvas.getMousePos(e); // user

  • 0

I have this canvas mousedown event:

$('#canvas').mousedown(function(e) {
    var coords = canvas.getMousePos(e); // user helper function

    if (coords.x >= 0 && coords.x <= 16 && coords.y >= 32 && coords.y <= 48) {
        tool = "fill";
    } else if (coords.x >= 16 && coords.x <= 32 && coords.y >= 32 && coords.y <= 48) {
        tool = "circle";
    }
});

I am also drawing images on this canvas like so in my document ready function:

var imgCircle = new Image();
imgCircle.src = "circle.png";

imgCircle.onload = function() {
    toolboxContext.drawImage(imgCircle, 16, 32);
}

I don’t like using hardcoded values like that in my if statements. I was wondering if there was a way to either get the local coordinates of the images in the canvas or actually register events for the clicking of said images. Thanks for reading.

  • 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-06T19:05:48+00:00Added an answer on June 6, 2026 at 7:05 pm

    You can’t register directly events on your objects but it’s easy to register events on the canvas as in

    canvas.addEventListener("mousedown", function(e) {
    

    (you’re maybe more interested in “mouseup”)

    And then you can get the coordinates in the canvas with

    e.pageX-$(canvas).offset().left
    

    and

    e.pageY-$(canvas).offset().top
    

    (here using jQuery for easy cross-platform positionning of the canvas but you can use standard javascript)

    After that, you just have to compare with the positions of your images that you must keep somewhere. I personnally use for that effect a Rect class :

    function Rect(x,y,w,h){
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
    Rect.prototype.contains = function(x, y) {
        return x>=this.x && x<=this.x+this.w && y>=this.y && y<=this.y+this.h;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this source code: var Game = function() { var canvas = document.getElementById('world');
I have this: <Canvas x:Name=LayoutRoot KeyDown=LayoutRoot_KeyDown> </Canvas> In a newly-minted Ag 3 application in
I have a MyCanvas class that extends JComponent. On this canvas I have drawn
I have designed this gui in netBeans which has a canvas , a couple
I have a Grid inside a Canvas defined like this: <Canvas x:Name=outerCanvas> <Grid Grid.Row=1
this is my code and have a error: $('#map_canvas').mouseover(function(e){ console.log(e.offset().left+' '+e.offset().top) }) thanks i
This is the required behaviour: I have various controls present on the canvas e.g.
hei, assume that i have this code: <StackPanel> <Canvas x:Name=canvas Height=400 Width=235> <Image x:Name=image
I have a drawing application using html5 canvas. When the user is drawing and
I have a main canvas 'blackboard' in a panel, this canvas has itself several

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.