Skip to content

Commit dfff053

Browse files
authored
feat(runtime): implement focusCanvas option to LoadRuntimeOptions (#236)
1 parent cb3c67f commit dfff053

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

.changeset/fresh-cities-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@jolly-pixel/runtime": minor
3+
---
4+
5+
Add a focusCanvas to disable permanent canvas focus listener

docs/llms/runtime-llms-full.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,21 @@ Bootstraps the runtime by detecting GPU capabilities, displaying a loading scree
108108
Returns a `Promise<void>` that resolves when loading completes, or shows an error on the loading screen if something fails.
109109

110110
```ts
111-
export interface LoadRuntimeOptions {
111+
interface LoadRuntimeOptions {
112112
/**
113113
* @default 850
114114
* Minimum delay (ms) before starting asset loading. Gives the loading UI time to render.
115115
*/
116116
loadingDelay?: number;
117+
/**
118+
* Whether to automatically focus the game canvas when the user clicks anywhere on the page.
119+
* This is important for games that require keyboard input,
120+
* as it ensures that the canvas has focus and can receive keyboard events.
121+
* @default true
122+
*/
123+
focusCanvas?: boolean;
117124
}
125+
118126
```
119127

120128
### 🎨 Custom loader and splash screen

packages/runtime/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,21 @@ Bootstraps the runtime by detecting GPU capabilities, displaying a loading scree
106106
Returns a `Promise<void>` that resolves when loading completes, or shows an error on the loading screen if something fails.
107107

108108
```ts
109-
export interface LoadRuntimeOptions {
109+
interface LoadRuntimeOptions {
110110
/**
111111
* @default 850
112112
* Minimum delay (ms) before starting asset loading. Gives the loading UI time to render.
113113
*/
114114
loadingDelay?: number;
115+
/**
116+
* Whether to automatically focus the game canvas when the user clicks anywhere on the page.
117+
* This is important for games that require keyboard input,
118+
* as it ensures that the canvas has focus and can receive keyboard events.
119+
* @default true
120+
*/
121+
focusCanvas?: boolean;
115122
}
123+
116124
```
117125

118126
### 🎨 Custom loader and splash screen

packages/runtime/src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ export interface LoadRuntimeOptions {
1515
* Minimum delay (ms) before starting asset loading. Gives the loading UI time to render.
1616
*/
1717
loadingDelay?: number;
18+
/**
19+
* Whether to automatically focus the game canvas when the user clicks anywhere on the page.
20+
* This is important for games that require keyboard input,
21+
* as it ensures that the canvas has focus and can receive keyboard events.
22+
* @default true
23+
*/
24+
focusCanvas?: boolean;
1825
}
1926

2027
export async function loadRuntime(
2128
runtime: Runtime<any>,
2229
options: LoadRuntimeOptions = {}
2330
) {
24-
const { loadingDelay = 850 } = options;
31+
const {
32+
loadingDelay = 850,
33+
focusCanvas = true
34+
} = options;
2535

2636
const gpuTierPromise = getGPUTier();
2737

@@ -57,7 +67,12 @@ export async function loadRuntime(
5767
});
5868

5969
// Make sure the focus is always on the game canvas wherever we click on the game window
60-
document.addEventListener("click", () => runtime.canvas.focus());
70+
function focusCanvasHandler() {
71+
if (document.activeElement !== runtime.canvas) {
72+
runtime.canvas.focus();
73+
}
74+
}
75+
document.addEventListener("click", focusCanvasHandler);
6176

6277
let initialized = false;
6378
try {
@@ -113,6 +128,11 @@ export async function loadRuntime(
113128
catch (error: any) {
114129
loadingComponent.error(error);
115130
}
131+
finally {
132+
if (!focusCanvas) {
133+
document.removeEventListener("click", focusCanvasHandler);
134+
}
135+
}
116136

117137
if (initialized) {
118138
runtime.start();

0 commit comments

Comments
 (0)