Files
2025-01-03 20:07:29 -08:00
..
2025-01-03 20:07:29 -08:00
2025-01-03 20:07:29 -08:00
2024-02-15 21:03:17 +01:00

nut.js Screen Control

nut.js allows to search and wait for images on your screen to either verify certain conditons or use it for further processing.

Configuration

The nut.js screen comes with a config object which allows to configure it's behaviour.

confidence

screen.config.confidence specifies the required matching percentage required to mark a possible candidate for a given template image a match.

autoHighlight

screen.config.autoHighlight is a boolean toggle which enables automated highlighting of image search results. This will highlight the matching Region by showing an opaque window.

highlightDurationMs

screen.config.highlightDurationMs configures the duration in milliseconds a highlight window is shown.

highlightOpacity

screen.config.highlightOpacity configures the opacity of highlight windows. Ranges from 0 (fully transparent) to 1 (fully opaque).

resourceDirectory

screen.config.resourceDirectory configures the location to load assets from. This allows configuring resource locations depending on e.g. the current operating system.

One could provide multiple folders containing platform specific template images and chose the correct resource directory at runtime. Following this scheme loading of platform specific images would be possible without changes to the source.

capture

capture allows you to capture a screenshot and store it to your filesystem.

find

find takes a filename relative to the configured resourceDirectory and tries to find a match on the main screen. It is possible to override the the configured matching confidence and search region providing LocationParameters. In case of a match, the corresponding Region on screen is returned.

await mouse.move(straightTo(centerOf(await screen.find(imageResource("image.png")))));

height

height returns the main screen's height in pixels.

highlight

When working with template images to e.g. move the mouse to certain positions it can be quite cumbersome to follow along without visual clues.

highlight allows you to display an opaque window overlay which makes it easier to visually follow detection / movement.

await screen.highlight(await screen.find(imageResource("image.png")));

on

on allows you to register callbacks which will be executed once find returns a match for a given template image.

This way it's possible to repeatedly execute actions whenever a certain image is detected on screen.

waitFor

Similar to find, waitFor will search for a template image on a system's main screen.

While find will fail immediately if no match is found, waitFor allows to configure a timeout in milliseconds during which the screen will repeatedly be scanned for the template image. Once the configured timeout is reached with no match, waitFor will fail.

await mouse.move(straightTo(centerOf(await screen.waitFor(imageResource("image.png"), 3000))));

width

width returns the main screen's width in pixels.