CanvasRenderingContext2D.addHitRegion()

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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 algorithm by which to determine if a point is inside or outside the hit region.
Possible values:
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 example demonstrates the addHitRegion() method.

HTML

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

JavaScript

const canvas = document.getElementById('canvas');
const 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);
ctx.lineWidth = 5;
ctx.stroke();

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

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

Edit the code below to 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).

Specifications

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic support
Experimental
Chrome Full support Yes
Disabled
Full support Yes
Disabled
Disabled This feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ? Firefox Full support 30
Disabled
Full support 30
Disabled
Disabled From version 30: this feature is behind the canvas.hitregions.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android Full support 30
Disabled
Full support 30
Disabled
Disabled From version 30: this feature is behind the canvas.hitregions.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
id
Experimental
Chrome Full support Yes
Disabled
Full support Yes
Disabled
Disabled This feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ? Firefox Full support 30
Disabled
Full support 30
Disabled
Disabled From version 30: this feature is behind the canvas.hitregions.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android Full support 30Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
control
Experimental
Chrome Full support Yes
Disabled
Full support Yes
Disabled
Disabled This feature is behind the canvas.hitregions.enabled preference. To change preferences in Chrome, visit chrome://flags.
Edge ? Firefox Full support 30
Disabled
Full support 30
Disabled
Disabled From version 30: this feature is behind the canvas.hitregions.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android Full support 30Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
path
Experimental
Chrome Full support Yes
Disabled
Full support Yes
Disabled
Disabled This feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ? Firefox Full support 39
Disabled
Full support 39
Disabled
Disabled From version 39: this feature is behind the canvas.hitregions.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android Full support 30Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
fillRule
Experimental
Chrome Full support Yes
Disabled
Full support Yes
Disabled
Disabled This feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ? Firefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
other hit region options
Experimental
Chrome No support NoEdge ? Firefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

Document Tags and Contributors

Contributors to this page: mfluehr, sideshowbarker, fscholz, nmve, Taken
Last updated by: mfluehr,