Build your web app

A web app is the UI for an Action that uses Interactive Canvas. You can use existing web technologies (HTML, CSS, and JavaScript) to design and develop your web app. For the most part, Interactive Canvas is able to render web content like a browser, but there are a few restrictions enforced for user privacy and security. Before you begin designing your UI, consider the design principles outlined in the Design guidelines section.

The HTML and JavaScript for your web app do the following:

  • Register Interactive Canvas event callbacks.
  • Initialize the Interactive Canvas JavaScript library.
  • Provide custom logic for updating your web app based on the state.

This page goes over the recommended ways to build your web app, how to enable communication between your web app and fulfillment, and general guidelines and restrictions.

While you can use any method to build your UI, Google recommends using the following libraries:

Architecture

Google strongly recommends using a single-page application architecture. This approach allows for optimal performance and supports continuous conversational user experience. Interactive Canvas can be used in conjunction with front-end frameworks like Vue, Angular, and React, which help with state management.

HTML file

The HTML file defines how your UI looks. This file also loads the Interactive Canvas JavaScript library, which enables communication between your web app and your conversational Action.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Immersive Canvas Sample</title>
    <!-- Disable favicon requests -->
    <link rel="shortcut icon" type="image/x-icon" href="data:image/x-icon;,">
    <!-- Load Interactive Canvas JavaScript -->
    <script src="https://www.gstatic.com/assistant/interactivecanvas/api/interactive_canvas.min.js"></script>
    <!-- Load PixiJS for graphics rendering -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.8.7/pixi.min.js"></script>
    <!-- Load Stats.js for fps monitoring -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
    <!-- Load custom CSS -->
    <link rel="stylesheet" href="css/main.css">
  </head>
  <body>
    <div id="view" class="view">
      <div class="debug">
        <div class="stats"></div>
        <div class="logs"></div>
      </div>
    </div>
    <!-- Load custom JavaScript after elements are on page -->
    <script src="js/main.js"></script>
    <script src="js/log.js"></script>
  </body>
</html>

Communicate between fulfillment and web app

Now that you've built your web app and fulfillment and loaded in the Interactive Canvas library in your web app file, you need to define how your web app and fulfillment interact. To do this, you'll modify the file that contains your web app logic.

Web app custom logic

This file contains the code to define callbacks and invoke methods through interactiveCanvas. Callbacks allow your web app to respond to information or requests from the conversational Action, while methods provide a way to send information or requests to the conversational Action.

Add interactiveCanvas.ready(callbacks); to your HTML file to initialize and register callbacks:

// main.js
const view = document.getElementById('view');

// initialize rendering and set correct sizing
const renderer = PIXI.autoDetectRenderer({
  antialias: true,
  width: view.clientWidth,
  height: view.clientHeight,
});
view.appendChild(renderer.view);

// center stage and normalize scaling for all resolutions
const stage = new PIXI.Container();
stage.position.set(view.clientWidth / 2, view.clientHeight / 2);
stage.scale.set(Math.max(renderer.width, renderer.height) / 1024);

// load a sprite from a svg file
const sprite = PIXI.Sprite.from('triangle.svg');
sprite.anchor.set(0.5);
sprite.tint = 0x00FF00; // green
stage.addChild(sprite);

let spin = true;
// register interactive canvas callbacks
const callbacks = {
  onUpdate(data) {
    console.log('onUpdate', JSON.stringify(data));
    if ('tint' in data) {
      sprite.tint = data.tint;
    }
    if ('spin' in data) {
      spin = data.spin;
    }
  },
};
interactiveCanvas.ready(callbacks);

// toggle spin on tap of the triangle
sprite.interactive = true;
sprite.buttonMode = true;
sprite.on('pointerdown', () => {
  spin = !spin;
});

// code to be ran per frame
let last = performance.now();
const frame = () => {
  // calculate time differences for smooth animations
  const now = performance.now();
  const delta = now - last;

  // rotate the triangle only if spin is true
  if (spin) {
    sprite.rotation += delta / 1000;
  }

  last = now;

  renderer.render(stage);
  requestAnimationFrame(frame);
};
frame();

Support touch interactions

Your Interactive Canvas Action can respond to your user's touch as well as their vocal inputs. Per the Interactive Canvas design guidelines, you should develop your Action to be "voice-first". That being said, some Smart Displays support touch interactions.

Supporting touch is similar to supporting conversational responses; however, instead of a vocal response from the user, your client-side JavaScript looks for touch interactions and uses those to change elements in the web app.

You can see an example of this in the sample, which uses the Pixi.js library:

...
const sprite = PIXI.Sprite.from('triangle.svg');
...
sprite.interactive = true; // Enables interaction events
sprite.buttonMode = true; // Changes `cursor` property to `pointer` for PointerEvent
sprite.on('pointerdown', () => {
  spin = !spin;
});
...

In this case, the value of the spin variable is sent through the interactiveCanvas API as an update callback. The fulfillment has logic that triggers an intent based on the value of spin.

...
app.intent('pause', (conv) => {
  conv.ask(`Ok, I paused spinning. What else?`);
  conv.ask(new HtmlResponse({
    data: {
      spin: false,
    },
  }));
});
...

Restrictions

Take the following restrictions into consideration as you develop your web app:

  • No cookies
  • No local storage
  • No geolocation
  • No camera usage
  • No popups
  • Stay under the 200mb memory limit
  • 3P Header takes up upper portion of screen
  • No styles can be applied to videos
  • Only one media element may be used at a time
  • No HLS video
  • No Web SQL database
  • No support for the SpeechRecognition interface of the Web Speech API.
  • No audio or video recording

Cross-origin resource sharing

Because Interactive Canvas web apps are hosted in an iframe and the origin is set to null, you must enable cross-origin resource sharing (CORS) for your web servers and storage resources. This allows your assets to accept requests from null origins.