JS Action/Object SDK
A JS Object is a screen object you can customize with JavaScript in EasyBuilder Pro. Use it to draw a chart from device data, respond to a tap, or change what the operator sees when a value changes.
A JS Action runs JavaScript when an operator presses a Combo Button or when an Action Trigger condition is met. With Action Trigger (Global), it can run in the background on the HMI without a cMT Viewer connection.
If this is your first JS Object, follow the tutorials to read a Config value, change the object's State, and build a JS Object–based Toggle Switch.
How a JS Object Works
- Configuration (
config) holds the values, device addresses, and subscriptions you set up in the editor. - State (
state) lets the script switch between appearances you define in the editor, such as the released and pressed images of a button. - Widget container (
widget) holds the parts the script adds. AMouseAreahandles pointer or touch input, and aCanvasdraws shapes or text.
In EasyBuilder X, use a Custom Model.
Quick Start
In a JS Object script, this refers to the JS Object. Expand the examples to see how a script uses its configuration, state, and widgets.
Inspect the configuration and state
console.log("Hello World");
console.log("Config:", this.config);
console.log("State:", this.state);MouseArea
To capture any mouse event, use MouseArea. It must be created and added to the widget container for it to start working.
Log a mouse press
var ma = new MouseArea();
ma.on("mousedown", function(e) {
console.log("I am clicked");
})
this.widget.add(ma);Canvas
To draw something, use Canvas. Same as MouseArea, it must be created and added to widget container.
Draw text and a rectangle
var canvas = new Canvas();
this.widget.add(canvas);
// draw text
canvas.fillStyle = "black";
canvas.font = `10px Consolas`;
canvas.fillText("Hello World", 0, 10);
// draw a rectangle
canvas.fillStyle = 'green';
canvas.fillRect(20, 20, 150, 100);Read with await
To read Device/PLC data, configure the address beforehand in the [Config] tab of JS Object.
Assume we have configured one called param1 as driver.Address. We can retrieve its value by calling driver.promises.getData with await:
Read a device value with await
var data = await driver.promises.getData(this.config.param1, 1);
console.log(data.values[0]);Read with a Callback
Alternatively, use driver.getData with a callback:
Read a device value with a callback
driver.getData(this.config.param1, 1, function(err, data) {
if (err) {
console.log('Error:', err.message);
} else {
console.log(data.values[0]);
}
});Runtime Context
A JS Object runs in the viewer that displays its screen. If two cMT Viewers open the same window, each viewer can run its own instance of the object. Within a viewer, JS Objects in the same window share a JavaScript context and its window global.
JavaScript in JS Object
Navigating to a URL with window.location requires a browser environment. In JS Object, use Web Request to fetch data from a URL and Canvas to display it on the HMI. See Global for the objects and functions available to scripts.
JS Action
JS Action runs JavaScript on demand via [Action Trigger] or [Combo Button]. It can run JavaScript code in the background (see note 1) without placing a JS Object on the screen. To use, in [Action Trigger] or [Combo Button], add a JS Action with [Execute JS] and run scripts as needed.
Notes:
- To run JS Action only once in the background, use [Value changed] or [Condition Object] mode in [Action Trigger (Global)]. Other JS Actions will run in the foreground GUI, HMI or cMT Viewer, and you may find multiple JS Actions running on each of them.
- JS Action does not require any screen-related operation so some functions are unavailable:
- Classes: Canvas, CanvasGradient, Container, ImageData, MouseArea, MouseEvent, Widget
- Global functions:
cancelAnimationFrame,requestAnimationFrame- Any
setTimeoutorsetIntervalhave to finish or be cancelled before the next action group can start.
Changelog
| Version | Date | Changes |
|---|---|---|
| v6.07.02.282 / v6.07.01.448 | 2022-07-29 | Fixed console logs from [Execute JS] in [Action Trigger] not showing |
| v6.07.01.188 / v6.06.02.380 | 2022-03-02 | Fixed canvas.translate() drawing incorrectly on iOS devices |
| v6.07.01.131 | 2022-02-11 | Added JS Action for [Action Trigger] and [Combo Button]. Use [Execute JS] to run JS Action. |
| v6.06.02.291 / v6.06.01.602 | 2021-12-28 | Fixed canvas.translate() and canvas.rotate() drawing incorrectly |
| v6.06.02.052 | 2021-08-25 | Added canvas.getImageData(), canvas.createImageData(), canvas.putImageData() |
| v6.05.02.439 | 2021-04-22 | Fixed console.log() in cMT Diagnoser |
| v6.05.01 | 2020 | First official release of JS Object |