The CanvasRenderingContext2D.drawFocusIfNeeded() method of the Canvas 2D API draws a focus ring around the current path or given path, If a given 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 Path2Dpath 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
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var button = document.getElementById('button');
button.focus();
ctx.beginPath();
ctx.rect(10, 10, 30, 30);
ctx.drawFocusIfNeeded(button);
Edit the code below and see your changes update live in the canvas:
Specifications
| Specification | Status | Comment | 
|---|---|---|
| WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D.drawFocusIfNeeded' in that specification. | Living Standard | Initial definition | 
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari | 
|---|---|---|---|---|---|
| Basic support | (Yes) | 29 (29)[1] 32 (32)[2] | No support | (Yes) | (Yes) | 
| Path parameter | (Yes) | No support | No support | (Yes) | (Yes) | 
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | 
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | 29 (29)[1] 32 (32)[2] | No support | (Yes) | (Yes) | 
| Path parameter | (Yes) | (Yes) | No support | No support | (Yes) | (Yes) | 
[1] In Gecko 28 (Firefox 28 / Thunderbird 28 / SeaMonkey 2.25 / Firefox OS 1.3), this method was implemented as drawSystemFocusRing, but has been renamed to the standard drawFocusIfNeeded in Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26).
[2] Prior to Gecko 32 (Firefox 32 / Thunderbird 32 / SeaMonkey 2.29 / Firefox OS 2.0) this method was deactivated by default behind the flag canvas.focusring.enabled.
See also
- The interface defining it, CanvasRenderingContext2D.