Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 724485 of CanvasRenderingContext2D.addHitRegion()

  • Revision slug: Web/API/CanvasRenderingContext2D.addHitRegion
  • Revision title: CanvasRenderingContext2D.addHitRegion()
  • Revision id: 724485
  • Created:
  • Creator: Taken
  • Is current revision? No
  • Comment

Revision Content

{{APIRef}} {{SeeCompatTable}}

The CanvasRenderingContext2D.addHitRegion() method of the Canvas 2D API adds a hit region to the bitmap. This allows you to make hit detection easier, lets you route events to DOM elements, and makes it possible for users to explore the canvas without seeing it.

Syntax

void ctx.addHitRegion(options);

Options

The options argument is optional. When provided, it is an {{jsxref("Object")}} which can contain the following properties:

path
A {{domxref("Path2D")}} object describing the area of the hit region. If not provided, the current path is used.
fillRule
The fill rule to use (defaults to "nonzero").
id
The ID for this hit region to reference it for later use in events, for example.
parentID
The ID of the parent region for cursor fallback and navigation by accessibility tools.
cursor
The {{cssxref("cursor")}} to use when the mouse is over this region (defaults to "inherit"). Inherits the cursor of the parent hit region, if any, or the canvas element's cursor.
control
An element (descendant of the canvas) to which events are to be routed. Defaults to null.
label
A text label for accessibility tools to use as a description of the region, if there is no control. Defaults to null.
role
An ARIA role for accessibility tools to determine how to represent this region, if there is no control. Defaults to null.

Examples

Using the addHitRegion method

This is just a simple code snippet which uses the addHitRegion method.

HTML

<canvas id="canvas"></canvas>

JavaScript

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

canvas.addEventListener("mousemove", function(event){
  if(event.region) {
    alert("ouch, my eye :(");
  }
});

ctx.beginPath();
ctx.arc(100, 100, 75, 0, 2 * Math.PI, false);
ctx.lineWidth = 5;
ctx.stroke();

// eyes
ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI, false);
ctx.arc(130, 80, 10, 0, 2 * Math.PI, false);
ctx.fill();
ctx.addHitRegion({id: "eyes"});

// mouth
ctx.beginPath();
ctx.arc(100, 110, 50, 0, Math.PI, false);
ctx.stroke();

Edit the code below and see your changes update live in the canvas (if you don't see the full smiley, check in the browser compatibility table, if your current browser supports hit regions already, you might need to activate a preference).

Playable code
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas>
<div class="playable-buttons">
  <input id="edit" type="button" value="Edit" />
  <input id="reset" type="button" value="Reset" />
</div>
<textarea id="code" class="playable-code" style="height:250px">
ctx.beginPath();
ctx.arc(100, 100, 75, 0, 2 * Math.PI, false);
ctx.lineWidth = 5;
ctx.stroke();

// eyes
ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI, false);
ctx.arc(130, 80, 10, 0, 2 * Math.PI, false);
ctx.fill();
ctx.addHitRegion({id: "eyes"});

// mouth
ctx.beginPath();
ctx.arc(100, 110, 50, 0, Math.PI, false);
ctx.stroke();</textarea>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;

function drawCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  eval(textarea.value);
}

reset.addEventListener("click", function() {
  textarea.value = code;
  drawCanvas();
});

edit.addEventListener("click", function() {
  textarea.focus();
});

canvas.addEventListener("mousemove", function(event){
  if(event.region) {
    alert("ouch, my eye :(");
  }
});

textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);

{{ EmbedLiveSample('Playable_code', 700, 520) }}

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-addhitregion", "CanvasRenderingContext2D.addHitRegion")}} {{Spec2('HTML WHATWG')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}}[1] {{CompatGeckoDesktop(30)}} [2] {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
id {{CompatVersionUnknown}}[1] {{CompatGeckoDesktop(30)}} [2] {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
control {{CompatVersionUnknown}}[1] {{CompatGeckoDesktop(30)}} [2] {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
path {{CompatVersionUnknown}}[1] {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
fillRule {{CompatVersionUnknown}}[1] {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
other hit region options {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo }} {{ CompatNo }} {{CompatGeckoMobile(30)}} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
id {{ CompatNo }} {{ CompatNo }} {{CompatGeckoMobile(30)}} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
control {{ CompatNo }} {{ CompatNo }} {{CompatGeckoMobile(30)}} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
path {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
fillRule {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
other hit region options {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}

Compatibility notes

  • [1] This feature is behind a feature flag. Set the flag ExperimentalCanvasFeatures to true to enable it.
  • [2] This feature is behind a feature preference setting. In about:config, set canvas.hitregions.enabled to true.

See also

  • {{domxref("CanvasRenderingContext2D.removeHitRegion()")}} {{experimental_inline}}
  • {{domxref("CanvasRenderingContext2D.clearHitRegions()")}} {{experimental_inline}}

Revision Source

<div>{{APIRef}} {{SeeCompatTable}}</div>

<p>The <code><strong>CanvasRenderingContext2D</strong></code><strong><code>.addHitRegion()</code></strong> method of the Canvas 2D API adds a hit region to the bitmap. This allows you to make hit detection easier, lets you route events to DOM elements, and makes it possible for users to explore the canvas without seeing it.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
void <var><em>ctx</em>.addHitRegion(<em>options</em>);</var>
</pre>

<h3 id="Options">Options</h3>

<p>The <code>options</code> argument is optional. When provided, it is an {{jsxref("Object")}} which can contain the following properties:</p>

<dl>
 <dt><code>path</code></dt>
 <dd>A {{domxref("Path2D")}} object describing the area of the hit region. If not provided, the current path is used.</dd>
 <dt><code>fillRule</code></dt>
 <dd>The fill rule to use (defaults to "<code>nonzero</code>").</dd>
 <dt><code>id</code></dt>
 <dd>The ID for this hit region to reference it for later use in events, for example.</dd>
 <dt><code>parentID</code></dt>
 <dd>The ID of the parent region for cursor fallback and navigation by accessibility tools.</dd>
 <dt><code>cursor</code></dt>
 <dd>The {{cssxref("cursor")}} to use when the mouse is over this region (defaults to "<code>inherit</code>"). Inherits the cursor of the parent hit region, if any, or the canvas element's cursor.</dd>
 <dt><code>control</code></dt>
 <dd>An element (descendant of the canvas) to which events are to be routed. Defaults to <code>null</code>.</dd>
 <dt><code>label</code></dt>
 <dd>A text label for accessibility tools to use as a description of the region, if there is no control. Defaults to <code>null</code>.</dd>
 <dt><code>role</code></dt>
 <dd>An <a href="/en-US/docs/Web/Accessibility/ARIA">ARIA role</a> for accessibility tools to determine how to represent this region, if there is no control. Defaults to <code>null</code>.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<h3 id="Using_the_addHitRegion_method">Using the <code>addHitRegion</code> method</h3>

<p>This is just a simple code snippet which uses the <code>addHitRegion</code> method.</p>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">
&lt;canvas id="canvas"&gt;&lt;/canvas&gt;
</pre>

<h4 id="JavaScript">JavaScript</h4>

<pre class="brush: js;">
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

canvas.addEventListener("mousemove", function(event){
&nbsp; if(event.region) {
&nbsp;&nbsp;&nbsp; alert("ouch, my eye :(");
&nbsp; }
});

ctx.beginPath();
ctx.arc(100, 100, 75, 0, 2 * Math.PI, false);
ctx.lineWidth = 5;
ctx.stroke();

// eyes
ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI, false);
ctx.arc(130, 80, 10, 0, 2 * Math.PI, false);
ctx.fill();
ctx.addHitRegion({id: "eyes"});

// mouth
ctx.beginPath();
ctx.arc(100, 110, 50, 0, Math.PI, false);
ctx.stroke();
</pre>

<p>Edit the code below and see your changes update live in the canvas (if you don't see the full smiley, check in the browser compatibility table, if your current browser supports hit regions already, you might need to activate a preference).</p>

<div style="display:none">
<h6 id="Playable_code">Playable code</h6>

<pre class="brush: html">
&lt;canvas id="canvas" width="400" height="200" class="playable-canvas"&gt;&lt;/canvas&gt;
&lt;div class="playable-buttons"&gt;
&nbsp; &lt;input id="edit" type="button" value="Edit" /&gt;
&nbsp; &lt;input id="reset" type="button" value="Reset" /&gt;
&lt;/div&gt;
&lt;textarea id="code" class="playable-code" style="height:250px"&gt;
ctx.beginPath();
ctx.arc(100, 100, 75, 0, 2 * Math.PI, false);
ctx.lineWidth = 5;
ctx.stroke();

// eyes
ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI, false);
ctx.arc(130, 80, 10, 0, 2 * Math.PI, false);
ctx.fill();
ctx.addHitRegion({id: "eyes"});

// mouth
ctx.beginPath();
ctx.arc(100, 110, 50, 0, Math.PI, false);
ctx.stroke();&lt;/textarea&gt;
</pre>

<pre class="brush: js">
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;

function drawCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  eval(textarea.value);
}

reset.addEventListener("click", function() {
  textarea.value = code;
  drawCanvas();
});

edit.addEventListener("click", function() {
  textarea.focus();
});

canvas.addEventListener("mousemove", function(event){
  if(event.region) {
    alert("ouch, my eye :(");
  }
});

textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);
</pre>
</div>

<p>{{ EmbedLiveSample('Playable_code', 700, 520) }}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-addhitregion", "CanvasRenderingContext2D.addHitRegion")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td><code>Basic support</code></td>
   <td>{{CompatVersionUnknown}}[1]</td>
   <td>{{CompatGeckoDesktop(30)}} [2]</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>id</code></td>
   <td>{{CompatVersionUnknown}}[1]</td>
   <td>{{CompatGeckoDesktop(30)}} [2]</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>control</code></td>
   <td>{{CompatVersionUnknown}}[1]</td>
   <td>{{CompatGeckoDesktop(30)}} [2]</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>path</code></td>
   <td>{{CompatVersionUnknown}}[1]</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>fillRule</code></td>
   <td>{{CompatVersionUnknown}}[1]</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td>other hit region options</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{CompatGeckoMobile(30)}}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>id</code></td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{CompatGeckoMobile(30)}}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>control</code></td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{CompatGeckoMobile(30)}}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>path</code></td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>fillRule</code></td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td>other hit region options</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
 </tbody>
</table>
</div>

<h3 id="Compatibility_notes">Compatibility notes</h3>

<ul>
 <li>[1] This feature is behind a feature flag. Set the flag <code>ExperimentalCanvasFeatures</code> to <code>true</code> to enable it.</li>
 <li>[2] This feature is behind a feature preference setting. In about:config, set <code>canvas.hitregions.enabled</code> to <code>true</code>.</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("CanvasRenderingContext2D.removeHitRegion()")}} {{experimental_inline}}</li>
 <li>{{domxref("CanvasRenderingContext2D.clearHitRegions()")}} {{experimental_inline}}</li>
</ul>
Revert to this revision