Skip to main content

highlight

Especially during development we might want to visually track what happens when executing our script. When it comes to image search, it's one thing to see in e.g. the log that we found a match, but a visual indicator would be even better.

highlight is exactly this!

Configuration#

highlight works by overlaying a Region of interest with an opaque highlight window.

Highlight duration and opacity are once again configurable properties on the screen.config object.

Highlighting regions#

highlight receives a Region specifying the area to highlight. It will then overlay the given region with an opaque highlight window.

const { screen, Region } = require("@nut-tree/nut-js");
(async () => {
screen.config.highlightDurationMs = 3000;
const highlightRegion = new Region(50, 100, 200, 300);
await screen.highlight(highlightRegion);
})();

Auto Highlighting#

The way the API is structured, it's really easy to highlight regions located by e.g. find:

const { screen } = require("@nut-tree/nut-js");
(async () => {
screen.config.resourceDirectory = "/resouce/path";
screen.config.highlightDurationMs = 3000;
await screen.highlight(screen.find("image.png"));
})();

However, manually adding highlights is not only cumbersome, but also requires additional effort in case we want to remove it again before running our script in production.

Therefore, nut.js provides an auto-highlight mechanism which is toggleable via config property. Highlight during development, disable it in production!

const { screen } = require("@nut-tree/nut-js");
(async () => {
screen.config.resourceDirectory = "/resouce/path";
screen.config.autoHighlight = true;
screen.config.highlightDurationMs = 1500;
await screen.find("test.png");
})();

With auto highlight turned on, we no longer have to manually care about highlighting find results. Once find returns a valid Region, it will be highlighted. And since waitFor reuses find, auto-highlight works there as well!

Summary#

  • nut.js provides a way to visually debug image search results.
  • Both the highlight duration and the highlight window opacity are configurable via config object.
  • Auto highlight will automatically highlight results returned from find