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);
					

参数

element

The element to check whether it is focused or not.

path
Path2D path to use.

范例

Managing button focus

This example draws two buttons on a canvas. The drawFocusIfNeeded() method is used to draw a focus ring when appropriate.

HTML

<canvas id="canvas">
  <button id="button1">Continue</button>
  <button id="button2">Quit</button>
</canvas>
					

JavaScript

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const button1 = document.getElementById('button1');
const button2 = document.getElementById('button2');
document.addEventListener('focus', redraw, true);
document.addEventListener('blur', redraw, true);
canvas.addEventListener('click', handleClick, false);
redraw();
function redraw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawButton(button1, 20, 20);
  drawButton(button2, 20, 80);
}
function handleClick(e) {
  // Calculate click coordinates
  const x = e.clientX - canvas.offsetLeft;
  const y = e.clientY - canvas.offsetTop;
  // Focus button1, if appropriate
  drawButton(button1, 20, 20);
  if (ctx.isPointInPath(x, y)) {
    button1.focus();
  }
  // Focus button2, if appropriate
  drawButton(button2, 20, 80);
  if (ctx.isPointInPath(x, y)) {
    button2.focus();
  }
}
function drawButton(el, x, y) {
  const active = document.activeElement === el;
  const width = 150;
  const height = 40;
  // Button background
  ctx.fillStyle = active ? 'pink' : 'lightgray';
  ctx.fillRect(x, y, width, height);
  // Button text
  ctx.font = '15px sans-serif';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';
  ctx.fillStyle = active ? 'blue' : 'black';
  ctx.fillText(el.textContent, x + width / 2, y + height / 2);
  // Define clickable area
  ctx.beginPath();
  ctx.rect(x, y, width, height);
  // Draw focus ring, if appropriate
  ctx.drawFocusIfNeeded(el);
}
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.drawFocusIfNeeded' in that specification.
实时标准 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
drawFocusIfNeeded Chrome Yes Edge 14 Firefox 32
32
29 Disabled
Disabled From version 29: this feature is behind the canvas.focusring.enabled preference. To change preferences in Firefox, visit about:config.
28 Alternate Name
Alternate Name Uses the non-standard name: drawSystemFocusRing
IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 32
32
29 Disabled
Disabled From version 29: this feature is behind the canvas.focusring.enabled preference. To change preferences in Firefox, visit about:config.
28 Alternate Name
Alternate Name Uses the non-standard name: drawSystemFocusRing
Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
路径 参数 Chrome Yes Edge ≤79 Firefox No IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

用户必须明确启用此特征。

用户必须明确启用此特征。

使用非标名称。

另请参阅

元数据

  • 最后修改: