ウェブアプリを構成する

ウェブアプリは、Interactive Canvas を使用するアクションのユーザー インターフェース(UI)です。 既存のウェブ技術(HTML、CSS、JavaScript、 WebAssembly など)を使用して、ウェブアプリの設計と開発を行います。ほとんどの場合、インタラクティブは Canvas は、ウェブ コンテンツをブラウザのようにレンダリングできますが、いくつかの制限があります。 セキュリティを確保するために 適用されますUI の設計を始める前に 設計ガイドラインに記載されている設計原則を考慮してください。 Firebase Hosting を使用することをおすすめします。 ウェブアプリをデプロイします

ウェブアプリの HTML と JavaScript では、次のことを行います。

このページでは、ウェブアプリの作成で推奨される方法、 会話型アクションとウェブアプリ間の通信、および一般的なガイドライン 制限します。

UI はどの方法でも作成できますが、 主なライブラリは次のとおりです。

  • Greensock: 複雑なアニメーションを作成する場合。
  • Pixi.js: WebGL で 2D グラフィックを描画する場合。
  • Three.js: WebGL で 3D グラフィックを描画する場合。
  • HTML5 Canvas の描画: シンプルな描画の場合。

アーキテクチャ

シングルページ アプリケーション アーキテクチャを使用することを強くおすすめします。この 最適なパフォーマンスを実現し、継続的な会話を 向上させることができますInteractive Canvas はフロントエンドと併用できる VueAngularReact などのフレームワークを使用して、 状態管理に役立ちます。

HTML ファイル

HTML ファイルで UI の外観を定義します。このファイルは、インタラクティブ モードの Canvas API。これにより、ウェブアプリ間の通信が可能になります。 会話型アクションを作成します

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Interactive 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/log.js"></script>
    <script type="module" src="js/main.js"></script>
  </body>
</html>
    

会話型アクションとウェブアプリ間の通信

ウェブアプリと会話型アクションを作成し、インタラクティブ スペースに読み込んだら、 使用する場合は、ウェブアプリと Canvas の 会話型アクションは相互作用します。そのためには、ウェブアプリを含むファイルを修正します。 できます。

action.js

このファイルには、コールバックを定義し、 interactiveCanvas によるメソッド。コールバックを使用すると 会話型アクションからの情報やリクエストに応答する一方で、 会話型アクションに情報やリクエストを送信する方法を提供する。

interactiveCanvas.ready(callbacks); を HTML ファイルに追加して初期化と コールバックを登録します。

JavaScript

/**
* This class is used as a wrapper for Google Assistant Canvas Action class
* along with its callbacks.
*/
export class Action {
 /**
  * @param  {Phaser.Scene} scene which serves as a container of all visual
  * and audio elements.
  */
 constructor(scene) {
   this.canvas = window.interactiveCanvas;
   this.gameScene = scene;
   const that = this;
   this.intents = {
     GUESS: function(params) {
       that.gameScene.guess(params);
     },
     DEFAULT: function() {
       // do nothing, when no command is found
     },
   };
 }

 /**
  * Register all callbacks used by the Interactive Canvas Action
  * executed during game creation time.
  */
 setCallbacks() {
   const that = this;
   // Declare the Interactive Canvas action callbacks.
   const callbacks = {
     onUpdate(data) {
       const intent = data[0].google.intent;
       that.intents[intent ? intent.name.toUpperCase() : 'DEFAULT'](intent.params);
     },
   };
   // Called by the Interactive Canvas web app once web app has loaded to
   // register callbacks.
   this.canvas.ready(callbacks);
 }
}
    

main.js

main.js JavaScript モジュールは、action.js ファイルと scene.js ファイルをインポートし、 ウェブアプリの読み込み時に、それぞれのインスタンスが作成されます。また、このモジュールでは Interactive Canvas のコールバックを登録します。

JavaScript

import {Action} from './action.js';
import {Scene} from './scene.js';
window.addEventListener('load', () => {
  window.scene = new Scene();
  // Set Google Assistant Canvas Action at scene level
  window.scene.action = new Action(scene);
  // Call setCallbacks to register Interactive Canvas
  window.scene.action.setCallbacks();
});
    

scene.js

scene.js ファイルはウェブアプリのシーンを作成します。以下は、 scene.js からの抜粋:

JavaScript

const view = document.getElementById('view');

// initialize rendering and set correct sizing
this.radio = window.devicePixelRatio;
this.renderer = PIXI.autoDetectRenderer({
  transparent: true,
  antialias: true,
  resolution: this.radio,
  width: view.clientWidth,
  height: view.clientHeight,
});
this.element = this.renderer.view;
this.element.style.width = `${this.renderer.width / this.radio}px`;
this.element.style.height = `${(this.renderer.height / this.radio)}px`;
view.appendChild(this.element);

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

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

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

タッチ操作のサポート

Interactive Canvas アクションは、ユーザーのタップ操作にも応答できます。 生成します。Interactive Canvas の設計ガイドラインに沿って、 アクションを「音声第一」に開発する必要があります。とはいえ、 ディスプレイはタップ操作をサポートしています。

サポート タッチは、会話形式の応答のサポートに似ています。ただし、 ユーザーの音声による応答ではなく、クライアント側の JavaScript が ウェブアプリの要素を変更する際に使用します。

こちらの例では、 Pixi.js ライブラリ:

JavaScript


this.sprite = PIXI.Sprite.from('triangle.svg');

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

トラブルシューティング

一方、Actions Console のシミュレータを使用して、 キャンバス アクションを使用すると、開発中に発生したエラーも ユーザー インターフェース上の Interactive Canvas ウェブアプリにおすすめしますこれらの情報は エラーも確認できます。

これらのエラー メッセージを Google Cloud プラットフォームのログで次の操作を行います。

  1. Actions Console で Actions プロジェクトを開きます。
  2. 上部のナビゲーションで [Test](テスト)をクリックします。
  3. [View logs in Google Cloud Platform] リンクをクリックします。
で確認できます。

ユーザーのログインエラーによるデバイスはログビューアに時系列で表示されます。

エラータイプ

Google Cloud Platform のログに表示されるウェブアプリのエラーには、次の 3 種類があります。

  • 10 秒以内に ready が呼び出されない場合に発生するタイムアウト
  • onUpdate() によって返された Promise が満たされなかった場合に発生するタイムアウト 10 秒以内
  • ウェブアプリで検出されない JavaScript ランタイム エラー
で確認できます。

JavaScript エラーの詳細を表示

ウェブアプリ内の JavaScript ランタイム エラーの詳細が 使用できます。JavaScript ランタイム エラーの詳細を確認するには、 手順は次のとおりです。

  1. 適切なクロスオリジン リソース シェアリングが構成されていることを確認する (CORS)HTTP レスポンス ヘッダー。詳しくは クロスオリジン リソース シェアリングをご覧ください。
  2. インポートした <script> タグに crossorigin="anonymous"HTML ファイル。次のコード スニペットのようになります。
<script crossorigin="anonymous" src="<SRC>"></script>

ガイドラインと制限事項

コンテンツを作成する際は、次のガイドラインと制限事項を考慮してください。 ウェブアプリをデプロイできます。

  • Cookie は使用不可
  • ローカル ストレージは使用不可
  • 位置情報は使用不可
  • カメラは使用不可
  • 録音または録画は不可
  • ポップアップは使用不可
  • 200 MB のメモリ上限を超えないようにする
  • コンテンツのレンダリング時にアクション名ヘッダーを考慮 画面上部)
  • 動画へのスタイルの適用は不可
  • 一度に使用できるメディア要素は 1 つのみ
  • ウェブ SQL データベースは使用不可
  • Web Speech APISpeechRecognition インターフェースはサポートされていません。
  • ダークモード設定を適用できません
  • スマートディスプレイでの動画再生がサポートされています。このモジュールの サポートされているメディア コンテナの形式とコーデックについては、Google Nest Hub のコーデックをご覧ください。

クロスオリジン リソース シェアリング

Interactive Canvas のウェブアプリは iframe でホストされ、オリジンが設定されているため クロスオリジン リソース シェアリング(CORS)を有効にする必要があります。 ウェブサーバーとストレージ リソースを保護します。このプロセスにより null オリジンからのリクエストを受け入れます。

次のステップ

ウェブアプリに機能を追加するには、クライアントを使用して構築を続ける、または サーバーサイド フルフィルメント