CanvasRenderingContext2D.drawFocusIfNeeded()

The CanvasRenderingContext2D.drawFocusIfNeeded() method of the Canvas 2D API draws a focus ring around the current or given path, if the specified element is focused.

Syntax

void ctx.drawFocusIfNeeded(element);
void ctx.drawFocusIfNeeded(path, element);

Parameters

element
The element to check whether it is focused or not.
path
A Path2D path to use.

Examples

Using the drawFocusIfNeeded method

This is just a simple code snippet which uses the drawFocusIfNeeded() method.

HTML

<canvas id="canvas">
  <input id="button" type="range" min="1" max="12">
</canvas>

JavaScript

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

button.focus();

ctx.beginPath();
ctx.rect(10, 10, 30, 30);
ctx.drawFocusIfNeeded(button);

Edit the code below to see your changes update live in the canvas:

Specifications

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

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 ? Firefox Full support 32
Full support 32
Full support 29
Disabled
Disabled From version 29: this feature is behind the canvas.focusring.enabled preference. To change preferences in Firefox, visit about:config.
Full support 28
Alternate Name
Alternate Name Uses the non-standard name: drawSystemFocusRing
IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 32
Full support 32
Full support 29
Disabled
Disabled From version 29: this feature is behind the canvas.focusring.enabled preference. To change preferences in Firefox, visit about:config.
Full support 28
Alternate Name
Alternate Name Uses the non-standard name: drawSystemFocusRing
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Path parameterChrome Full support YesEdge ? Firefox No support NoIE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android No support NoOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.

See also

Document Tags and Contributors

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