exporting_for_web.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. Web export uses *asm.js*, a highly optimizable subset of JavaScript.
  12. A 64-bit browser is required to run games in asm.js format. Most notably,
  13. this is a problem with Firefox, which on Windows is shipped as a
  14. 32-bit application by default.
  15. Serving the files
  16. -----------------
  17. The default ``.html`` file can be used as ``DirectoryIndex`` and can be
  18. renamed to e.g. ``index.html`` at any time, its name is never depended on.
  19. It can also be inserted into another HTML file as an ``<iframe>`` element.
  20. Users must allow **third-party** cookies when playing a game presented in an
  21. iframe.
  22. The other exported files are served as they are next to the ``.html`` file,
  23. names unchanged.
  24. The ``.mem`` and ``.pck`` files are binary, usually delivered with the
  25. MIME-type ``application/octet-stream``.
  26. Delivering the files with gzip compression is recommended especially for the
  27. ``.pck``, ``.asm.js`` and ``.mem`` files, which are usually large in size.
  28. Export options
  29. --------------
  30. Turning on **Debugging Enabled** when exporting will, in addition to enabling
  31. various debug features of the engine, display a debug output below the canvas,
  32. displaying JavaScript and engine errors. If controls are
  33. enabled as well, display of this output can be toggled.
  34. You can also use the browser-integrated developer console, usually opened with
  35. the F12 key, which often shows more information, including WebGL errors.
  36. **Memory Size** is fixed and must thus be set during export. Try using no more
  37. than necessary to strain users' browsers as little as possible.
  38. **Enable Run** will add a button between the *Stop scene* and *Play edited Scene*
  39. buttons in the editor to quickly open the game in the default browser for
  40. testing.
  41. The remaining options customize the generated HTML page:
  42. **Title** is the content of the ``<title>`` element of the page, usually used by
  43. browsers as the tab and window name. The title set here is only displayed until
  44. the game is started, afterwards the title is set to the application name set in
  45. the project settings.
  46. **Head Include** and **Style Include** are appended into the ``<head>`` and
  47. CSS ``<style>`` elements respectively. This allows, for example, linking
  48. web fonts for use in the page.
  49. **Font Family** is the CSS ``font-family`` used on the page, without terminating
  50. semicolon.
  51. **Controls Enabled** toggles display of controls, offering e.g. a toggle for
  52. output display in debug mode and a fullscreen button.
  53. In the default page, the controls are displayed in the top-right corner on top
  54. of the canvas, which can get in the way in games that use the cursor.
  55. Web export limitations
  56. ----------------------
  57. Exported files must not be reused
  58. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. The exported files ending with ``.html`` and ``fs.js`` are adjusted on export
  60. specifically for that game's version and the given export options. They must
  61. not be reused in futher exports.
  62. Some functions must be called from input callbacks
  63. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. Browsers do not allow arbitrarily **entering full screen** at any time. The same
  65. goes for **capturing the cursor**. Instead, these actions have to occur as a
  66. response to a JavaScript input event. In Godot, this is most easily done by
  67. entering full screen from within an input callback such as ``_input`` or
  68. ``_unhandled_input``.
  69. Starting exported games from the local file system
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. Many browsers will not load exported projects when **opened locally**
  72. per ``file://`` protocol. To get around this, use a local server.
  73. Python offers an easy method for this, using ``python -m SimpleHTTPServer``
  74. with Python 2 or ``python -m http.server`` with Python 3 will serve the current
  75. working directory on ``http://localhost:8000``.
  76. Locale lookup
  77. ~~~~~~~~~~~~~
  78. Godot tries to detect the user's locale using information provided by the
  79. browser, but this is rather unreliable. A better way is to use CGI to read the
  80. HTTP ``Accept-Language`` header. If you assign its value to the JavaScript
  81. property ``Module.locale`` after the ``Module`` objects is created, but before
  82. the engine starts, Godot will use that value to initialize the locale.
  83. In any case, users should always be offered the option to configure the locale
  84. manually.