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.
void ctx.drawFocusIfNeeded(element); void ctx.drawFocusIfNeeded(path, element);
path
Path2D
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>
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:
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D.drawFocusIfNeeded' in that specification. | Living Standard | Initial definition |
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
.
CanvasRenderingContext2D
.
© 2005–2017 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