/
index.md
115 lines (84 loc) · 2.87 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: CanvasRenderingContext2D.drawFocusIfNeeded()
slug: Web/API/CanvasRenderingContext2D/drawFocusIfNeeded
---
{{APIRef}}
**`CanvasRenderingContext2D.drawFocusIfNeeded()`** はキャンバス 2D API のメソッドで、引数で与えられた要素にフォーカスが当たった時に、現在のパスもしくは指定されたパスの周りにフォーカスリングを描画します。
## 構文
```js
void ctx.drawFocusIfNeeded(element);
void ctx.drawFocusIfNeeded(path, element);
```
### 引数
- element
- : フォーカスしたかどうかをチェックする要素。
- `path`
- : 利用する {{domxref("Path2D")}} パス。
## 例
### ボタンのフォーカスの管理
この例では、キャンバス上に 2 つのボタンを描画します。 `drawFocusIfNeeded()` メソッドは、必要に応じてフォーカスリングを描画するために使用されます。
#### HTML
```html
<canvas id="canvas">
<button id="button1">続ける</button>
<button id="button2">終了</button>
</canvas>
```
#### JavaScript
```js
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;
// 必要に応じて button1 をフォーカスする
drawButton(button1, 20, 20);
if (ctx.isPointInPath(x, y)) {
button1.focus();
}
// 必要に応じて button2 をフォーカスする
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;
// ボタンの背景
ctx.fillStyle = active ? "pink" : "lightgray";
ctx.fillRect(x, y, width, height);
// ボタンのテキスト
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);
// クリック可能な領域を定義
ctx.beginPath();
ctx.rect(x, y, width, height);
// 必要に応じてフォーカスリングを描画
ctx.drawFocusIfNeeded(el);
}
```
#### 結果
{{EmbedLiveSample('Managing_button_focus', 700, 180)}}
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}
## 関連情報
- このメソッドを定義しているインターフェイス: {{domxref("CanvasRenderingContext2D")}}