CanvasRenderingContext2D.addHitRegion()

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

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 Object which can contain the following properties:

path
A 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 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);

Specifications

Specification Status Comment
WHATWG HTML Living Standard
The definition of 'CanvasRenderingContext2D.addHitRegion' in that specification.
Living Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes)[1] 30 (30) [2] No support No support No support
id (Yes)[1] 30 (30) [2] No support No support No support
control (Yes)[1] 30 (30) [2] No support No support No support
path (Yes)[1] 39 (39) [2] No support No support No support
fillRule (Yes)[1] No support No support No support No support
other hit region options No support No support No support No support No support
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support No support 30.0 (30) No support No support No support
id No support No support 30.0 (30) No support No support No support
control No support No support 30.0 (30) No support No support No support
path No support No support 39.0 (39) No support No support No support
fillRule No support No support No support No support No support No support
other hit region options No support No support No support No support No support No support

[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

Document Tags and Contributors

 Contributors to this page: fscholz, Taken
 Last updated by: fscholz,