![Yoctopuce Yocto-Display User Manual Download Page 47](http://html1.mh-extra.com/html/yoctopuce/yocto-display/yocto-display_user-manual_3418946047.webp)
setTimeout
(
refresh
,
5
);
}
startDemo
();
As explained at the beginning of this chapter, you need to have Node.js v7.6 or later installed to try
this example. When done, you can type the following two commands to automatically download and
install the dependencies for building this example:
npm install
You can the start the sample code within Node.js using the following command, replacing the [...] by
the arguments that you want to pass to the demo code:
node demo.js
[
...
]
Same example, but this time running in a browser
If you want to see how to use the library within a browser rather than with Node.js, switch to the
directory
example_html/Doc-GettingStarted-Yocto-Display
. You will find there a single HTML file,
with a JavaScript section similar to the code above, but with a few changes since it has to interact
through an HTML page rather than through the JavaScript console.
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
charset
=
"UTF-8">
<
title
>
Hello World
<
/
title
>
<
script
src
=
"../../lib/yocto_api.js"><
/
script
>
<
script
src
=
"../../lib/yocto_display.js"><
/
script
>
<
script
>
var disp, l1;
var h, w, y, x, vx, vy;
async function startDemo() {
await YAPI.LogUnhandledPromiseRejections();
await YAPI.DisableExceptions();
// Setup the API to use the VirtualHub on local machine
let errmsg = new YErrorMsg();
if (await YAPI.RegisterHub('127.0.0.1', errmsg) != YAPI.SUCCESS) {
alert('Cannot contact VirtualHub on 127.0.0.1: ' + errmsg.msg);
}
// Select specified device, or use first available one
let serial = document.getElementById('serial').value;
if (serial == '') {
// by default use any connected module suitable for the demo
let anydiplay = YDisplay.FirstDisplay();
if (anydiplay) {
let module = await anydiplay.module();
serial = await module.get_serialNumber();
document.getElementById('serial').value = serial;
}
}
disp = YDisplay.FindDisplay( ".display");
//clean up
await disp.resetAll();
// retreive the display size
w = await disp.get_displayWidth();
h = await disp.get_displayHeight();
// reteive the first layer
let l0 = await disp.get_displayLayer(0);
// display a text in the middle of the screen
await l0.drawText(w / 2, h / 2, l0.ALIGN_CENTER, "Hello world!");
// visualize each corner
await l0.moveTo(0, 5);
await l0.lineTo(0, 0);
9. Using Yocto-Display with JavaScript / EcmaScript
www.yoctopuce.com
41
Summary of Contents for Yocto-Display
Page 1: ...Yocto Display User Manual...
Page 2: ......
Page 40: ...34 www yoctopuce com...
Page 52: ...46 www yoctopuce com...
Page 84: ...78 www yoctopuce com...
Page 92: ...86 www yoctopuce com...
Page 130: ...124 www yoctopuce com...
Page 422: ...416 www yoctopuce com...