The HTML code

Let's create a file named js-with-glue.html in the /chapter-06-interact-with-js folder and populate it with the following contents:

<!doctype html>
<html lang="en-us">
<head>
<title>Interact with JS using Glue Code</title>
</head>
<body>
<h1>Interact with JS using Glue Code</h1>
<canvas id="myCanvas" width="255" height="255"></canvas>
<div style="margin-top: 16px;">
<button id="actionButton" style="width: 100px; height: 24px;">Pause</button>
<span style="width: 100px; margin-left: 8px;">Status:</span>
<span id="runStatus" style="width: 100px;"></span>
</div>
<script type="application/javascript" src="js-with-glue.js"></script>
<script type="application/javascript">
Module()
.then(result => {
const m = result.asm;
m._init();

// Move the rectangle by 1px in the x and y every 20 milliseconds:
const loopRectMotion = () => {
setTimeout(() => {
m._moveRect();
if (m._getIsRunning()) loopRectMotion();
}, 20)
};

// Enable you to pause and resume the rectangle movement:
document.querySelector('#actionButton')
.addEventListener('click', event => {
const newIsRunning = !m._getIsRunning();
m._setIsRunning(newIsRunning);
event.target.innerHTML = newIsRunning ? 'Pause' : 'Start';
if (newIsRunning) loopRectMotion();
});

loopRectMotion();
});
</script>
</body>
</html>

We added two <span> elements to display the status of the rectangle's movement, along with a corresponding label. We're using Emscripten's Promise-like API to load the module and reference the functions from the compiled code. We're no longer passing in the _jsFillRect and _jsClearRect functions to the module because we're handling that within the js-with-glue.c file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset