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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:21:08+00:00 2026-05-26T16:21:08+00:00

I am using HTML 5 for the first time, and playing around with this

  • 0

I am using HTML 5 for the first time, and playing around with this HTML5 canvas tutorial.

I have changed the example to include 4 images and I want to be able to detect which image was clicked on before doing a redirect on the click event.

Here is my code as it currently is, can somebody tell me how I could detect which image has been clicked in my click event? Also, the mouseout event seems to not always resize the image, any ideas why?

<!DOCTYPE HTML>
<html>
<head>
    <style>
        body {
            margin: 0px;
            padding: 0px;
        }

        #myCanvas {
            border: 0px;
        }
    </style>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic2d-v1.0.4.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script>
        function initRectangles(rectangles){
            // initialize an array of rectangles that provide
            // rectangular paths and properties for the images
            return [{
                name: "buffalo",
                image: null,
                x: 40,
                y: 45,
                width: 80,
                height: 80,
                scale: 1
            }, {
                name: "indianapolis",
                image: null,
                x: 125,
                y: 45,
                width: 80,
                height: 80,
                scale: 1
            }, {
                name: "miami",
                image: null,
                x: 210,
                y: 45,
                width: 80,
                height: 80,
                scale: 1
            }, {
                name: "nyjets",
                image: null,
                x: 295,
                y: 45,
                width: 80,
                height: 80,
                scale: 1
            }];
        }

        function loadImages(sources, callback){
            var images = {};
            var loadedImages = 0;
            var numImages = 0;
            for (var src in sources) {
                numImages++;
            }
            for (var src in sources) {
                images[src] = new Image();
                images[src].onload = function(){
                    if (++loadedImages >= numImages) {
                        callback(images);
                    }
                };
                images[src].src = sources[src];
            }
        }

        function mapImages(rectangles, images){
            // map images to rectangles
            var counter = 0;
            for (var img in images) {
                rectangles[counter++].image = images[img];
            }
        }

        function initStage(images){
            var rectangles = initRectangles(rectangles);
            mapImages(rectangles, images);

            kin = new Kinetic_2d("myCanvas");
            var context = kin.getContext();

            kin.setDrawStage(function () {
                this.clear();
                var mousePos = kin.getMousePos();

                for (var n = 0; n < rectangles.length; n++) {
                    var rect = rectangles[n];

                    context.save();
                    context.translate(rect.x, rect.y);
                    context.scale(rect.scale, rect.scale);
                    kin.beginRegion();
                    var rectX = -1 * rect.width / 2;
                    var rectY = -1 * rect.height / 2;
                    context.drawImage(rect.image, rectX, rectY, rect.width, rect.height);
                    context.beginPath();
                    context.rect(rectX, rectY, rect.width, rect.height);
                    context.closePath();

                    this.addRegionEventListener("mouseover", function () {
                        document.body.style.cursor = "pointer";
                        rectangles[n].scale = 1.07;
                    });
                    this.addRegionEventListener("mouseout", function () {
                        document.body.style.cursor = "default";
                        rectangles[n].scale = 1;
                    });

                    this.closeRegion();
                    context.restore();
                }
            });
        }

        window.onload = function(){
            var sources = {
                buffalo: "buffalo.png",
                indianapolis: "indianapolis.png",
                miami: "miami.png",
                nyjets: "nyjets.png"
            };

            loadImages(sources, initStage);

            $("#myCanvas").click(function (e) {


            }); 
        };
    </script>
</head>
<body>
    <canvas id="myCanvas" width="400" height="90">
    </canvas>
</body>
</html>
  • 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-26T16:21:08+00:00Added an answer on May 26, 2026 at 4:21 pm

    Try using event mouseup:

    this.addRegionEventListener("mouseup", function() {
        alert("mouse up");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Ajax via jQuery for the first time ever. I have an html
This is my first time using map tag in html. I assume getting the
I am using this workflow: http://reinh.com/blog/2009/03/02/a-git-workflow-for-agile-teams.html where he mentions: First, and while in your
I am designing a small website using HTML + JSP. This is my first
This is really weird. Its my first time with using the mustache library and
This is my first time using Stack Overflow, so if I've done something wrong
I am using jquery layout for the first time. I have it working using
I have a jboss seam 2.2.2 project and it is my first time using
I am playing around with ASP.NET MVC for the first time, so I apologize
This is my first time using XSLT. I'm trying to create a file that

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.