EyeDropper API example

EyeDropper API example. EyeDropper.open() reads a pixel from the screen after a gesture and returns sRGBHex.

The problem

A picker on the page only sees the page. An input, a canvas, getImageData. The pixel you want is often next to the tab, not in it.

The method

EyeDropper.open() asks the OS for one pixel from the screen. The promise resolves to sRGBHex. No alpha. Escape rejects. A signal can reject too. Needs a user gesture and a secure context.

What to do

  1. Construct. Run open(). Pick anything you can see. The stage takes the hex.
  2. Run open({ signal }). The delay select arms AbortController. Pick first, or wait.

new EyeDropper()

No options. One instance is enough. Construct again to replace it.

const dropper = new EyeDropper()

open()

Needs a user gesture. This Run is that gesture. The OS overlay can sample the screen, not only this tab. Escape rejects with AbortError.

const result = await dropper.open()
result.sRGBHex

open({ signal })

While the overlay is up, the page does not get clicks. Abort is a timer, not a button. Escape still cancels. The select is the delay.

const controller = new AbortController()
setTimeout(() => controller.abort(), 2000)
await dropper.open({ signal: controller.signal })

All Web API labs

  • Notifications API example
  • Sanitizer API example
  • Translator API example