Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. EyeDropper

EyeDropper

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

TheEyeDropper interface represents an instance of an eyedropper tool that can be opened and used by the user to select colors from the screen.

Constructor

EyeDropper()Experimental

Returns a newEyeDropper instance.

Instance methods

TheEyeDropper interface doesn't inherit any methods.

EyeDropper.open()Experimental

Returns a promise that resolves to an object that gives access to the selected color.

Examples

Opening the eyedropper tool and sampling a color

This example shows how to open an eyedropper tool and wait for the user to either select a pixel from the screen, or pressEscape to cancel the eyedropper mode.

HTML

html
<button>Open the eyedropper</button> <span></span>

JavaScript

js
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();  eyeDropper    .open()    .then((result) => {      resultElement.textContent = result.sRGBHex;      resultElement.style.backgroundColor = result.sRGBHex;    })    .catch((e) => {      resultElement.textContent = e;    });});

Result

Aborting the eyedropper mode

This example shows that the eyedropper mode can also be aborted before the user has selected a color or pressedEscape.

HTML

html
<button>Open the eyedropper</button> <span></span>

JavaScript

js
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

Specifications

Specification
EyeDropper API
# eyedropper-interface

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp