This example shows that the eyedropper mode can also be aborted before the user has selected a color or pressed Escape.
HTML
<button id="start-button">Open the eyedropper</button> <span id="result"></span>
JavaScript
document.getElementById("start-button").addEventListener("click", () => {
const resultElement = document.getElementById("result");
if (!window.EyeDropper) {
resultElement.textContent =
"Your browser does not support the EyeDropper API";
return;
}
const eyeDropper = new EyeDropper();
const abortController = new AbortController();
eyeDropper
.open({ signal: abortController.signal })
.then((result) => {
resultElement.textContent = result.sRGBHex;
resultElement.style.backgroundColor = result.sRGBHex;
})
.catch((e) => {
resultElement.textContent = e;
});
setTimeout(() => {
abortController.abort();
}, 2000);
});
Result
\n
The EyeDropper
interface represents an instance of an eyedropper tool that can be opened and used by the user to select colors from the screen.
"}},{"type":"prose","value":{"id":"constructor","title":"Constructor","isH3":false,"content":"
\n
"}},{"type":"specifications","value":{"id":"specifications","title":"Specifications","isH3":false,"specifications":[{"bcdSpecificationURL":"https://wicg.github.io/eyedropper-api/#eyedropper-interface","title":"EyeDropper API"}],"query":"api.EyeDropper"}},{"type":"browser_compatibility","value":{"id":"browser_compatibility","title":"Browser compatibility","isH3":false,"query":"api.EyeDropper"}}],"isActive":true,"isMarkdown":true,"isTranslated":false,"locale":"en-US","mdn_url":"/en-US/docs/Web/API/EyeDropper","title":"EyeDropper"}],"popularity":null,"short_title":"EyeDropper","sidebarHTML":"