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.
void ctx.drawFocusIfNeeded(element); void ctx.drawFocusIfNeeded(path, element);
elementpathPath2D path to use.drawFocusIfNeeded methodThis is just a simple code snippet which uses the drawFocusIfNeeded() method.
<canvas id="canvas"> <input id="button" type="range" min="1" max="12"> </canvas>
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:
| Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of 'CanvasRenderingContext2D.drawFocusIfNeeded' in that specification. | Living Standard | Initial definition |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | Yes | ? | 32
|
No | Yes | Yes |
Path parameter |
Yes | ? | No | No | Yes | Yes |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes | Yes | ? | 32
|
Yes | Yes | Yes |
Path parameter |
Yes | Yes | ? | No | Yes | Yes | Yes |
CanvasRenderingContext2D
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawFocusIfNeeded