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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:03:51+00:00 2026-05-28T06:03:51+00:00

I have a HTML5 canvas on which I have drawn several shapes. What I

  • 0

I have a HTML5 canvas on which I have drawn several shapes.

What I want to happen, is that when the mouse is clicked on any shape the shape should get selected (at least it can tell what kind of shape is selected).

Thank you.

  • 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-28T06:03:52+00:00Added an answer on May 28, 2026 at 6:03 am

    Try using an existing canvas library (or create your own) that has an event when a shape is being selected.

    The example below uses the Kinetic JS library, and the example below is from HTML5 Canvas Region Events Example:

    var triangle = new Kinetic.Shape(function(){
        var context = this.getContext();
        context.beginPath();
        context.lineWidth = 4;
        context.strokeStyle = "black";
        context.fillStyle = "#00D2FF";
        context.moveTo(120, 50);
        context.lineTo(250, 80);
        context.lineTo(150, 170);
        context.closePath();
        context.fill();
        context.stroke();
    });
    
    triangle.on("mouseout", function(){
        writeMessage(messageLayer, "Mouseout triangle");
    });
    
    triangle.on("mousemove", function(){
        var mousePos = stage.getMousePosition();
        var x = mousePos.x - 120;
        var y = mousePos.y - 50;
        writeMessage(messageLayer, "x: " + x + ", y: " + y);
    });
    
    shapesLayer.add(triangle);
    
    var circle = new Kinetic.Shape(function(){
        var canvas = this.getCanvas();
        var context = this.getContext();
        context.beginPath();
        context.arc(380, canvas.height / 2, 70, 0, Math.PI * 2, true);
        context.fillStyle = "red";
        context.fill();
        context.lineWidth = 4;
        context.stroke();
    });
    
    circle.on("mouseover", function(){
        writeMessage(messageLayer, "Mouseover circle");
    });
    circle.on("mouseout", function(){
        writeMessage(messageLayer, "Mouseout circle");
    });
    circle.on("mousedown", function(){
        writeMessage(messageLayer, "Mousedown circle");
    });
    circle.on("mouseup", function(){
        writeMessage(messageLayer, "Mouseup circle");
    });
    
    shapesLayer.add(circle);
    
    stage.add(shapesLayer);
    stage.add(messageLayer);
    

    In addition, I have included some mouse-in detection if the cursor is within the shape, without using any javascript libraries.

    Rectangular-based mouse-in detection:

    function isCursorWithinRectangle(x, y, width, height, mouseX, mouseY) {
        if(mouseX > x && mouseX < x + width && mouseY > y && mouseY < y + height) {
            return true;
        }
        return false;
    }
    

    Circle-based mouse-in detection:

    function isCursorWithinCircle(x, y, r, mouseX, mouseY) {
        var distSqr = Math.pow(x - mouseX, 2) + Math.pow(y - mouseY, 2);
    
        if(distSqr < r * r) {
            return true;
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML5 canvas image where I've drawn some points and I want
i have some coordinates, which i want to draw on a HTML5 canvas step-by-step.
Basically I have several canvas drawings on my page what I want to happen
I have created a basic shape in HTML canvas element which works fine. The
I have external html5 canvas that you can paint on some lines using your
I have an HTML canvas on which a user draws some shapes. Now suppose
I have a little mouse effect which draws on a canvas, however when the
I have a doodle application that saves an html5 canvas to an image in
I have an HTML5 canvas element which I would like to save to the
My project has an HTML5 Canvas on which graphical objects are drawn repeatedly. These

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.