exporting_for_web.rst 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. .. _doc_exporting_for_web:
  2. Exporting for the Web
  3. =====================
  4. Exporting for the web generates several files to be served from a web server,
  5. including a default HTML page for presentation. A custom HTML file can be
  6. used, see :ref:`doc_compiling_for_web`.
  7. The default HTML file is designed to fit the game perfectly without cutting off
  8. parts of the canvas when the browser window is scaled to the game's dimensions.
  9. This way it can be inserted into an ``<iframe>`` with the game's size, as is
  10. common on most web game hosting sites.
  11. Serving the files
  12. -----------------
  13. The generated ``.html`` file can be used as ``DirectoryIndex`` and can be
  14. renamed to e.g. ``index.html`` at any time, its name is never depended on.
  15. It can also be inserted into another HTML file as an ``<iframe>`` element.
  16. Users must allow **third-party** cookies when playing a game presented in an
  17. iframe.
  18. The ``.mem``, ``.pck`` (and ``.wasm`` when using WebAssembly) files are
  19. binary, usually delivered with MIME-type ``application/octet-stream``.
  20. Delivering the files with gzip compression is recommended especially for the
  21. ``.pck``, ``.asm.js``, ``.mem`` and ``.wasm`` files, which are usually large in
  22. size. The WebAssembly binary (``.wasm``) file compresses particularly well.
  23. Export options
  24. --------------
  25. Turning on **Debugging Enabled** when exporting will display a debug output
  26. below the canvas, displaying JavaScript and engine errors. If controls are
  27. enabled as well, display of this output can be toggled.
  28. **Memory Size** is fixed and must thus be set during export. Try using no more
  29. than necessary to strain users' browsers as little as possible.
  30. For WebAssembly builds the minimum value can be used since memory growth is
  31. enabled.
  32. **Enable Run** will add a button between the *Stop scene* and *Play edited Scene*
  33. buttons in the editor to quickly open the game in the default browser for
  34. testing.
  35. The remaining options customize the generated HTML page:
  36. **Title** is the content of the ``<title>`` element of the page, usually used by
  37. browsers as the tab and window name. The title set here is only displayed until
  38. the game is started, afterwards the title is set to the application name set in
  39. the project settings.
  40. **Head Include** and **Style Include** are appended into the ``<head>`` and
  41. CSS ``<style>`` elements respectively. This allows, for example, linking
  42. web fonts for use in the page.
  43. **Font Family** is the CSS ``font-family`` used on the page, without terminating
  44. semicolon.
  45. **Controls Enabled** toggles display of controls, offering e.g. a toggle for
  46. output display in debug mode and a fullscreen button.
  47. In the default page, the controls are displayed in the top-right corner on top
  48. of the canvas, which can get in the way in games that use the cursor.
  49. Calling JavaScript from script
  50. ------------------------------
  51. In web builds, the ``JavaScript`` singleton is available. If offers a single
  52. method called ``eval`` that works similar to the JavaScript function of the
  53. same name. It takes a string as an argument and executes it as JavaScript code.
  54. This allows interacting with the browser in ways not possible with script
  55. languages integrated into Godot.
  56. In order to keep the code compatible with other platforms, check if the
  57. JavaScript singleton is available before using it::
  58. var JavaScript
  59. func _ready():
  60. # retrieve the singleton here, will return `null` on platforms other than web
  61. Globals.get_singleton("JavaScript")
  62. func my_func():
  63. # call JavaScript.eval only if available
  64. if JavaScript:
  65. JavaScript.eval("alert('Calling JavaScript per GDScript!');")
  66. The ``eval`` method also accepts a second, optional Boolean argument, which
  67. specifies whether to execute the code in the global execution context,
  68. defaulting to ``false`` to prevent polluting the global namespace::
  69. func my_func2():
  70. if JavaScript:
  71. # execute in global execution context,
  72. # thus adding a new JavaScript global variable `MyGlobal`
  73. JavaScript.eval("var MyGlobal = {};", true)