With some games, you want to make sure you notify the user that the canvas does not currently have focus, and give them a visual reminder to click the canvas once to give it focus. This can be more important for games which use keyboard for input.
You can use this as a module as a quick helper to do this check. The default id of Defold’s canvas is just “canvas” but you can pass a different element id in if you want
-- Use to check if your game canvas has focus or not
local M = {}
function M.has_focus(element_id)
element_id = element_id or "canvas"
if html5 == nil then return end
local focused_element = html5.run("document.activeElement.id")
if focused_element == element_id then
return true
else
return false
end
end
return M
html5_focus_check.lua (328 Bytes)