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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:05:05+00:00 2026-05-26T06:05:05+00:00

I am attempting to write my own button object in JavaScript that can be

  • 0

I am attempting to write my own button object in JavaScript that can be placed on an HTML5 canvas. The problem I am having is with adding the event listener to my function. My assumption is that I need to extend an object that actually has the addEventListener method, but I am unsure how to do this in JavaScript. Below is a snippet of some of my code:

function Button(bContext, ...) {
    var self = this;
    var context = bContext;
    ...


    self.addEventListener('mouseover', self.mouseOverListener, false);
    ...

    self.drawMe = function() {
    ...
    };

    self.mouseOverListener = function() {
        alert("Hovering");
    };
};

The ‘…’ is just me shortening the code for this post, in actuality there is a lot more stuff there but it is not relevant to this question.

  • 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-26T06:05:06+00:00Added an answer on May 26, 2026 at 6:05 am

    You can easily add the addEventListener method to your object (in most browsers anyway, might not work in IE < 8)

    self.addEventListener = document.body.addEventListener
    

    However, it won’t magically start triggering click events when it’s clicked. You’d still have to generate those yourself by monitoring events on the <canvas> element. As such, it’s definitely better to just roll your own event listener, or incorporate one from an existing library (see below) than try to inherit from the DOM one, as that can get sketchy.

    Odds are pretty high that you’ll just want to use a <canvas> library like raphael.js. Which has all of this stuff baked in, and impliments it’s own Element object with DOM-like events. That said, if you really want to roll your own solution, it’s pretty straightforward as long as you only want to capture click events inside rectangular areas (finding points on curved elements is another matter).

    Here’s some quick pseudocode

    var buttons = []; // stores all button instances
    var canvas = document.getElementById('#myCanvas');
    canvas.bind('mousemove',function(event){
      var i = buttons.length;
      while(i--){
        var box = { 
           x: [buttons[i].x, buttons[i].pos.x+buttons[i].width], 
           y: [buttons.y, buttons[i].pos.y+buttons[i].height]
        };
        if(event.clientX > box.x[0] && event.clientX < box.x[1] && event.clientY > box.y[0] && event.clientY < box.y[1]){
           buttons[i].click();
        }
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently attempting to write my own program that mirrors the pmap command, specifically
I've been attempting to write a Lisp macro that would perfom the equivalent of
I am attempting to write an application that uses libCurl to post soap requests
I'm attempting to write a Python C extension that reads packed binary data (it
I am attempting to write a one-line Perl script that will toggle a line
I am attempting to write a time management tool with wxPython that is ideally
Currently I'm attempting to write my own wxObject, and I would like for the
I've been playing around in depth with attempting to write my own version of
For the past few days I have been attempting to write my own shell
I'm attemping to write my own bootloader and i'm having issues with writing to

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.