Html Utils extension

defold-html-utils

Features

  • Focus the canvas and check focus state.
  • Save/load strings in localStorage.
  • Detect mobile devices.

Setup

You can use defold-html-utils in your own project by adding this project as Defold library dependencies.
Open your game.project file and in the dependencies field under project add:

https://github.com/d954mas/defold-html-utils/archive/master.zip

Usage

Example is in main/main.script.

function init(self)
    html_utils.focus()
    print("focused:", html_utils.is_focused())

    local ok_save, saved_data = html_utils.save_data("test/path", "hello")
    print("save:", ok_save, saved_data)

    local ok_load, loaded_data = html_utils.load_data("test/path")
    print("load:", ok_load, loaded_data)

    print("mobile:", html_utils.is_mobile())
end

API

  • html_utils.focus() — focus the HTML5 canvas.
  • html_utils.is_focused() -> boolean — return whether the canvas is focused.
  • html_utils.load_data(path: string) -> boolean, string — load data by key.
  • html_utils.save_data(path: string, data: string) -> boolean, string — save data by key.
  • html_utils.is_mobile() -> boolean — detect mobile device.

Platforms

HTML5 only.

6 Likes

Good stuff! Thanks for sharing. I wonder if some of these should be part of the built in functions? I can definitely see is_mobile() being very useful to decide about input methods (virtual controller vs keyboard etc). What about the others? How are you using them?

5 Likes

focus, after every ads finish on poki. Focus must be return by poki, but manually focus make it better.

localstorage better then indexdb

IsMobile worked good. I use it to show wasd tooltip on pc, or virtualpad and buttons on mobile.
IsMobile use deprecated api, but i don’t find better alternative. At least you have 4 ways:)

https://blog.devgenius.io/4-ways-to-detect-mobile-browsers-in-javascript-943b66657524

1 Like

Agree! They are definitely useful.

@d954mas What’s the main advantage of using local storage instead of indexdb?

sys.load can’t be used for HTML5 apps running on iframe from a different origin (cross-origin iframe)
– use localStorage instead because of this limitation on default IndexedDB

3 Likes