过时
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
CanvasRenderingContext2D
方法
addHitRegion()
adds a hit region to the bitmap.
Canvas hit regions make hit detection easy. They let you route events to DOM elements, and make it possible for users to explore the canvas without seeing it.
void ctx.addHitRegion(options);
选项
argument is optional. When provided, it is an
对象
which can contain the following properties:
path
Path2D
object describing the area of the hit region. If not provided, the current path is used.
fillRule
nonzero
:
non-zero winding rule
. Default rule.
evenodd
:
even-odd winding rule
.
id
The ID for this hit region to reference it for later use in events, for example.
parentID
The ID of the parent region for cursor fallback and navigation by accessibility tools.
cursor
cursor
to use when the mouse is over this region (defaults to
继承
). Inherits the cursor of the parent hit region, if any, or the canvas element's cursor.
控制
null
.
label
null
.
role
null
.
addHitRegion
方法
此范例演示
addHitRegion()
方法。
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.addEventListener('mousemove', function(event) {
if(event.region) {
alert('ouch, my eye :(');
}
});
ctx.beginPath();
ctx.arc(100, 100, 75, 0, 2 * Math.PI);
ctx.lineWidth = 5;
ctx.stroke();
// Eyes
ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI);
ctx.arc(130, 80, 10, 0, 2 * Math.PI);
ctx.fill();
ctx.addHitRegion({id: "eyes"});
// Mouth
ctx.beginPath();
ctx.arc(100, 110, 50, 0, Math.PI);
ctx.stroke();
Edit the code below to see your changes update live in the canvas. (If you don't see the full smiley, check the browser compatibility table to see if your current browser supports hit regions already; you might need to activate a preference.)
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas>
<div class="playable-buttons">
<input id="edit" type="button" value="Edit" />
<input id="reset" type="button" value="Reset" />
</div>
<textarea id="code" class="playable-code" style="height:250px">
ctx.beginPath();
ctx.arc(100, 100, 75, 0, 2 * Math.PI, false);
ctx.lineWidth = 5;
ctx.stroke();
// eyes
ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI, false);
ctx.arc(130, 80, 10, 0, 2 * Math.PI, false);
ctx.fill();
ctx.addHitRegion({id: "eyes"});
// mouth
ctx.beginPath();
ctx.arc(100, 110, 50, 0, Math.PI, false);
ctx.stroke();</textarea>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;
function drawCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
eval(textarea.value);
}
reset.addEventListener("click", function() {
textarea.value = code;
drawCanvas();
});
edit.addEventListener("click", function() {
textarea.focus();
});
canvas.addEventListener("mousemove", function(event){
if(event.region) {
alert("ouch, my eye :(");
}
});
textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);
Canvas hit regions have been removed from the WHATWG Living Standard, although discussions about future standardization are ongoing. See https://github.com/whatwg/html/issues/3407 了解更多信息。
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
addHitRegion
|
Chrome
Yes
Disabled
|
Edge
≤79
Disabled
|
Firefox
30
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
30
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
控制
|
Chrome
Yes
Disabled
|
Edge
≤79
Disabled
|
Firefox
30
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 30 | Opera Android No | Safari iOS No | Samsung Internet Android No |
fillRule
|
Chrome
Yes
Disabled
|
Edge
≤79
Disabled
|
Firefox No | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
id
|
Chrome
Yes
Disabled
|
Edge
≤79
Disabled
|
Firefox
30
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 30 | Opera Android No | Safari iOS No | Samsung Internet Android No |
| other hit region options | Chrome No | Edge No | Firefox No | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
path
弃用
非标
|
Chrome
Yes
Disabled
|
Edge
≤79
Disabled
|
Firefox
39
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 30 | Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
不支持
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
用户必须明确启用此特征。
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()