Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can easily add the <code>addEventListener</code> method to your object (in most browsers anyway, might not work in <code>IE &lt; 8</code>)</p> <pre><code>self.addEventListener = document.body.addEventListener </code></pre> <p>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 <code>&lt;canvas&gt;</code> element. As such, it's <strong>definitely</strong> 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.</p> <p>Odds are pretty high that you'll just want to use a <code>&lt;canvas&gt;</code> library like <a href="http://raphaeljs.com/" rel="nofollow">raphael.js</a>. Which has all of this stuff baked in, and impliments it's own <code>Element</code> object with DOM-like events. That said, if you <em>really</em> 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). </p> <p>Here's some quick pseudocode</p> <pre><code>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 &gt; box.x[0] &amp;&amp; event.clientX &lt; box.x[1] &amp;&amp; event.clientY &gt; box.y[0] &amp;&amp; event.clientY &lt; box.y[1]){ buttons[i].click(); } } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload