customizing_html5_shell.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. .. _doc_customizing_html5_shell:
  2. Customizing the Web export HTML page
  3. ====================================
  4. Rather than the default HTML page that comes with the export templates, it is
  5. also possible to use a custom HTML page. This allows drastic customization of
  6. the final web presentation and behavior. The path to the custom HTML page is
  7. specified in the export options as ``Html/Custom Html Shell``.
  8. The default HTML page is available in the Godot Engine repository at
  9. `/misc/dist/html/full-size.html <https://github.com/godotengine/godot/blob/3.1/misc/dist/html/full-size.html>`__.
  10. Some simple use-cases where customizing the default page is useful include:
  11. - Loading files from a different directory
  12. - Loading a ``.zip`` file instead of a ``.pck`` file as main pack
  13. - Loading engine files from a different directory than the main pack file
  14. - Adding a click-to-play button so games can be started in full-screen mode
  15. - Loading some extra files before the engine starts, so they are available in
  16. the file system later
  17. - Passing custom "command line" arguments, e.g. ``-s`` to start a MainLoop script
  18. Another sample HTML page is available at `/misc/dist/html/fixed-size.html <https://github.com/godotengine/godot/blob/3.1/misc/dist/html/fixed-size.html>`__.
  19. This page uses a fixed size canvas with an output widget below. However, the
  20. F12 browser console should be preferred as it can display additional
  21. information, such as WebGL errors.
  22. Placeholder substitution
  23. ------------------------
  24. When exporting the game, several placeholders in the HTML page are replaced
  25. with values depending on the export:
  26. +------------------------------+-----------------------------------------------+
  27. | Placeholder | Substituted by |
  28. +==============================+===============================================+
  29. | ``$GODOT_BASENAME`` | Basename of exported files without suffixes, |
  30. | | e.g. ``game`` when exporting ``game.html`` |
  31. +------------------------------+-----------------------------------------------+
  32. | ``$GODOT_DEBUG_ENABLED`` | ``true`` if debugging, ``false`` otherwise |
  33. +------------------------------+-----------------------------------------------+
  34. | ``$GODOT_HEAD_INCLUDE`` | Custom string to include just before the end |
  35. | | of the HTML ``<head>`` element |
  36. +------------------------------+-----------------------------------------------+
  37. The HTML file must evaluate the JavaScript file ``$GODOT_BASENAME.js``. This
  38. file defines a global ``Engine`` object used to start the engine, :ref:`see
  39. below <doc_javascript_engine_object>` for details.
  40. The boot splash image is exported as ``$GODOT_BASENAME.png`` and can be used
  41. e.g. in ``<img />`` elements.
  42. ``$GODOT_DEBUG_ENABLED`` can be useful to optionally display e.g. an output
  43. console or other debug tools.
  44. ``$GODOT_HEAD_INCLUDE`` is replaced with the string specified by the export
  45. option ``Html/Head Include``.
  46. .. _doc_javascript_engine_object:
  47. The ``Engine`` object
  48. ---------------------
  49. The JavaScript global object ``Engine`` is defined by ``$GODOT_BASENAME.js``
  50. and serves as an interface to the engine start-up process.
  51. The API is based on and requires basic understanding of `Promises <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises>`__.
  52. The object itself has only the following methods:
  53. .. js:function:: Engine.load(basePath)
  54. Load the engine from the passed base path.
  55. :param string basePath: Base path of the engine to load.
  56. :returns: Promise which resolves once the engine is loaded.
  57. .. js:function:: Engine.unload()
  58. Unload the engine to free memory.
  59. This is called automatically once the engine is started unless
  60. explicitly disabled using :js:func:`engine.setUnloadAfterInit`.
  61. .. js:function:: Engine.isWebGLAvailable([majorVersion = 1])
  62. Check whether WebGL is available.
  63. :param number majorVersion:
  64. The major WebGL version to check for. Defaults to 1 for *WebGL 1.0*.
  65. :returns:
  66. ``true`` if the given major version of WebGL is available, ``false``
  67. otherwise.
  68. .. js:function:: Engine.setWebAssemblyFilenameExtension(extension)
  69. When loading the engine, the filename extension of the WebAssembly module
  70. is assumed to be ``wasm``. This function allows usage of an alternate
  71. extension.
  72. .. code-block:: js
  73. Engine.setWebAssemblyFilenameExtension('dat');
  74. // Load 'mygame.dat' as WebAssembly module.
  75. Engine.load('mygame');
  76. This is useful for outdated hosts that only accept uploads of files with
  77. certain filename extensions.
  78. :param string extension:
  79. Filename extension without preceding dot.
  80. .. Note::
  81. Depending on the host, using an alternate filename extension can prevent
  82. some start-up optimizations. This occurs when the file is delivered with a
  83. MIME-type other than :mimetype:`application/wasm`.
  84. Starting an ``Engine`` instance
  85. -------------------------------
  86. :js:class:`Engine` also acts a class:
  87. .. js:class:: Engine
  88. An instance of the engine that can be started, usually a game.
  89. Instantiate the class using the ``new`` operator:
  90. .. code-block:: js
  91. var engine = new Engine();
  92. This yields an :js:class:`Engine` instance, referred to as ``engine`` with a
  93. lower-case ``e`` from here.
  94. To start such an instance, the global ``Engine`` object must be loaded,
  95. then the ``engine`` instance must be initialized and finally started.
  96. .. js:function:: engine.init([basePath])
  97. Initialize the instance. The instance can then be started with one of the
  98. ``start`` functions, usually :js:func:`engine.startGame`.
  99. :param string basePath:
  100. The base path to the engine, same as in :js:func:`Engine.load`.
  101. Must be passed only if the engine hasn't been loaded yet.
  102. :returns: Promise that resolves once the engine is loaded and initialized.
  103. .. js:function:: engine.preloadFile(file[, path])
  104. Load a file so it is available in the file system once the instance runs. Must
  105. be called **before** starting the instance.
  106. :param file:
  107. If type is string, the file will be loaded from that path.
  108. If type is ``ArrayBuffer`` or a view on one, the buffer will used as
  109. the content of the file.
  110. :param string path:
  111. Path by which the file will be available. Mandatory if ``file`` is not
  112. a string. If not passed, the path is derived from the URL of the loaded
  113. file.
  114. :returns: Promise that resolves once the file is preloaded.
  115. .. js:function:: engine.start([arg1, arg2, …])
  116. Starts the instance of the engine, using the passed strings as
  117. command line arguments. This allows great control over how the engine is
  118. started, but usually the other methods starting with ``engine.start`` are
  119. simpler and should be used instead.
  120. If the instance has not yet been initialized with :js:func:`engine.init`,
  121. it will be.
  122. The engine must be loaded beforehand.
  123. Requires that the engine has been loaded, and that a canvas can be found on
  124. the page.
  125. :param string variadic: Command line arguments.
  126. :returns: Promise that resolves once the engine started.
  127. .. js:function:: engine.startGame(mainPack)
  128. Initializes the engine if not yet initialized and starts the game with the
  129. main pack loaded from the passed URL.
  130. If the engine isn't loaded yet, the base path of the passed URL will be
  131. used to load the engine.
  132. This function ignores overrides of filenames and their extensions to start
  133. only the main pack passed as argument.
  134. :param string mainPack:
  135. Path to the main pack to start. Also used as base path to load the
  136. engine if not loaded already.
  137. :returns: Promise that resolves once the game started.
  138. Configuring start-up behaviour
  139. ------------------------------
  140. Beside starting the engine, other methods of the engine instance allow
  141. configuring the behavior:
  142. .. js:function:: engine.setUnloadAfterInit(enabled)
  143. Specify whether the Engine will be unloaded automatically after the
  144. instance is initialized.
  145. This frees browser memory by unloading files that are no longer needed once
  146. the instance is initialized. However, if more instances of the engine will
  147. be started, the Engine will have to be loaded again.
  148. Enabled by default.
  149. :param boolean enabled:
  150. ``true`` if the engine shall be unloaded after initializing,
  151. ``false`` otherwise.
  152. .. js:function:: engine.setCanvas(canvasElem)
  153. Specify a canvas to use.
  154. By default, the first canvas element on the page is used for rendering.
  155. :param HTMLCanvasElement canvasElem: The canvas to use.
  156. .. js:function:: engine.setCanvasResizedOnStart(enabled)
  157. Specifies whether the canvas will be resized to the width and height
  158. specified in the project settings on start.
  159. Enabled by default.
  160. :param boolean enabled:
  161. ``true`` if the canvas shall be resized on start, ``false`` otherwise.
  162. .. js:function:: engine.setLocale(locale)
  163. By default, the engine will try to guess the locale to use from the
  164. JavaScript environment. It is usually preferable to use a server-side
  165. user-specified locale, or at least use the locale requested in the HTTP
  166. ``Accept-Language`` header. This method allows specifying such a custom
  167. locale string.
  168. For example, with PHP:
  169. .. code-block:: php
  170. engine.setLocale(<?php echo Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); ?>);
  171. :param string locale:
  172. Locale.
  173. .. seealso:: List of :ref:`locales <doc_locales>`.
  174. .. js:function:: engine.setExecutableName(execName)
  175. Specify the virtual filename of the executable.
  176. A real executable file doesn't exist for the HTML5 platform. However,
  177. a virtual filename is stored by the engine for compatibility with other
  178. platforms.
  179. By default, the base name of the loaded engine files is used.
  180. This method allows specifying another name.
  181. This affects the output of :ref:`OS.get_executable_path() <class_OS_method_get_executable_path>`
  182. and the automatically started main pack, :file:`{ExecutableName}.pck`.
  183. :param string execName: Executable name.
  184. Customizing the presentation
  185. ----------------------------
  186. The following methods are used to implement the presentation:
  187. .. js:function:: engine.setProgressFunc(callback)
  188. Set the callback for displaying download progress.
  189. :param function callback:
  190. Callback called once per frame with two number arguments:
  191. bytes loaded so far, and total bytes to load.
  192. .. code-block:: js
  193. function printProgress(current, total) {
  194. console.log("Loaded " + current + " of " + total + " bytes");
  195. }
  196. engine.setProgressFunc(printProgress);
  197. If the total is 0, it couldn't be calculated. Possible reasons
  198. include:
  199. - Files are delivered with server-side chunked compression
  200. - Files are delivered with server-side compression on Chromium
  201. - Not all file downloads have started yet (usually on servers without
  202. multi-threading)
  203. .. Note::
  204. For ease of use, the callback is only called once per frame, so that usage
  205. of ``requestAnimationFrame()`` is not necessary.
  206. .. js:function:: engine.setStdoutFunc(callback)
  207. Specify the standard output stream callback.
  208. :param function callback:
  209. Callback function called with one argument, the string to print.
  210. .. code-block:: js
  211. function printStdout(text) {
  212. console.log(text);
  213. }
  214. engine.setStdoutFunc(printStdout);
  215. This method should usually only be used in debug pages. The
  216. ``$GODOT_DEBUG_ENABLED`` placeholder can be used to check for this.
  217. By default, ``console.log()`` is used.
  218. .. js:function:: engine.setStderrFunc(callback)
  219. Specify the standard error stream callback.
  220. :param function callback:
  221. Callback function called with one argument, the string to print.
  222. .. code-block:: js
  223. function printStderr(text) {
  224. console.warn("Error: " + text);
  225. }
  226. engine.setStderrFunc(printStderr);
  227. This method should usually only be used in debug pages. The
  228. ``$GODOT_DEBUG_ENABLED`` placeholder can be used to check for this.
  229. By default, ``console.warn()`` is used.
  230. Accessing the Emscripten ``Module``
  231. -----------------------------------
  232. If you know what you're doing, you can access the runtime environment
  233. (Emscripten's ``Module``) as ``engine.rtenv``. Check the official Emscripten
  234. documentation for information on how to use it:
  235. https://kripken.github.io/emscripten-site/docs/api_reference/module.html