CanvasRenderingContext2D.isPointInPath()

The CanvasRenderingContext2D.isPointInPath() method of the Canvas 2D API reports whether or not the specified point is contained in the current path.

Syntax

ctx.isPointInPath(x, y [, fillRule]);
ctx.isPointInPath(path, x, y [, fillRule]);

Parameters

x
The x-axis coordinate of the point to check.
y
The y-axis coordinate of the point to check.
fillRule
The algorithm by which to determine if a point is inside or outside the path.
Possible values:
path
A Path2D path to check against. If unspecified, the current path is used.

Return value

Boolean
A Boolean, which is true if the specified point is contained in the current or specified path, otherwise false.

Examples

Checking a point in the current path

This example uses the isPointInPath() method to check if a point is within the current path.

HTML

<canvas id="canvas"></canvas>
<p>In path: <code id="result">false</code></p>

JavaScript

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

ctx.rect(10, 10, 100, 100);
ctx.fill();
result.innerText = ctx.isPointInPath(30, 70);

Result

Checking a point in the specified path

Whenever you move the mouse, this example checks whether the cursor is in a circular Path2D path. If yes, the circle becomes green, otherwise it is red.

HTML

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

JavaScript

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

// Create circle
const circle = new Path2D();
circle.arc(150, 75, 50, 0, 2 * Math.PI);
ctx.fillStyle = 'red';
ctx.fill(circle);

// Listen for mouse moves
canvas.addEventListener('mousemove', function(event) {
  // Check whether point is inside circle
  if (ctx.isPointInPath(circle, event.clientX, event.clientY)) {
    ctx.fillStyle = 'green';
  }
  else {
    ctx.fillStyle = 'red';
  }

  // Draw circle
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.fill(circle);
});

Result

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.isPointInPath' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Path parameterChrome Full support YesEdge Full support YesFirefox Full support 31IE No support NoOpera Full support YesSafari No support NoWebView Android ? Chrome Android ? Edge Mobile Full support YesFirefox Android Full support 31Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

Gecko-specific note

  • Prior to Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4), this method incorrectly failed to multiply the specified point's coordinates by the current transformation matrix before comparing it to the path. Now this method works correctly even if the context is rotated, scaled, or otherwise transformed.

See also

Document Tags and Contributors

Contributors to this page: mfluehr, fscholz, nmve, arronei, erikadoyle, notetienne
Last updated by: mfluehr,