This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The CanvasRenderingContext2D method addHitRegion() adds a hit region to the bitmap.

Canvas hit regions make hit detection easy. They let you route events to DOM elements, and make it possible for users to explore the canvas without seeing it.


void ctx.addHitRegion(options);


The options argument is optional. When provided, it is an Object which can contain the following properties:

A Path2D object describing the area of the hit region. If not provided, the current path is used.
The algorithm by which to determine if a point is inside or outside the hit region.
Possible values:
The ID for this hit region to reference it for later use in events, for example.
The ID of the parent region for cursor fallback and navigation by accessibility tools.
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.
An element (descendant of the canvas) to which events are to be routed. Defaults to null.
A text label for accessibility tools to use as a description of the region, if there is no control. Defaults to null.
An ARIA role for accessibility tools to determine how to represent this region, if there is no control. Defaults to null.


Using the addHitRegion method

This example demonstrates the addHitRegion() method.


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


const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

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

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

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

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

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


Canvas hit regions have been removed from the WHATWG Living Standard, although discussions about future standardization are ongoing. See for more information.

Browser compatibility

No compatibility data found for api.CanvasRenderingContext2D.addHitRegion.
Check for problems with this page or contribute missing data to mdn/browser-compat-data.

See also