Embed a game
本頁內容尚未翻譯。
<cl-mini-game> loads a game from https://mini-games.cresclab.com in a sandboxed frame. It shows a
progress bar until the game reports that it is ready, and it never breaks the page around it: when
the game cannot load, the element shows your fallback or hides itself.
Install
Section titled “Install”The package is private and published to GitHub Packages under the chatbotgang organization.
@chatbotgang:registry=https://npm.pkg.github.compnpm add @chatbotgang/mini-gamesImporting the package defines the element once; importing it again is harmless.
import "@chatbotgang/mini-games";<cl-mini-game game="random" theme="dark" lang="ja" style="block-size: 480px"> <p slot="fallback">The game is unavailable right now.</p></cl-mini-game>The element is a block that fills its width and is 400px tall by default. Size it with CSS.
Attributes
Section titled “Attributes”| Attribute | Values | Default | Effect |
|---|---|---|---|
game |
a game id, or random |
random |
Which game to load. Changing it loads the new game. |
theme |
light, dark |
the game’s default, light |
Changing it updates the running game. |
lang |
zh-TW, en, ja, th |
the game’s default, en |
Changing it updates the running game. |
paused |
present or absent | absent | Pauses the game, for example while a dialog is minimized. |
timeout |
milliseconds | 10000 |
How long to wait for the game to report ready. |
src |
a site URL, absolute or relative to the page | https://mini-games.cresclab.com |
Where games are served from. Use it for previews and local development. |
Game ids: bounce (Corner Chase) and flappy (Crescendo Bird).
Events
Section titled “Events”| Event | detail |
When |
|---|---|---|
gameready |
none | The game is playable. |
gameerror |
{ reason: "timeout" | "game-error", message: string } |
The game did not report ready in time, or reported an error. |
gameescape |
none | The player pressed Escape inside the game. A focused frame keeps key presses from reaching your page, so the game forwards this one. |
The events bubble and cross shadow roots. The element also has a state property, loading,
ready or error, reflected to a state attribute, for code that starts listening late, and a
reload() method for a “try again” button.
When the game fails, the element shows the children in slot="fallback". Without a fallback it
hides itself.
Frameworks
Section titled “Frameworks”The event names are lowercase without hyphens, so every framework binds them directly.
import "@chatbotgang/mini-games";import type {} from "@chatbotgang/mini-games/react";
<cl-mini-game game="random" theme={theme} ongameerror={(event) => console.warn(event.detail.reason)}/>;<cl-mini-game game="random" :theme="theme" @gameerror="onError" /><cl-mini-game game="random" {theme} ongameerror={onError}></cl-mini-game><cl-mini-game game="random" theme={theme()} on:gameerror={onError} />Vue needs compilerOptions.isCustomElement: (tag) => tag.startsWith("cl-") in its compiler
options. Only React types ship today; the others treat the element as an unknown tag.
Content Security Policy
Section titled “Content Security Policy”A page with a CSP must allow the site in frame-src:
frame-src https://mini-games.cresclab.comThe game site only allows framing from *.cresclab.com, Firebase Hosting preview channels
(*.web.app) and local development hosts.
Native apps
Section titled “Native apps”An app WebView can open a game URL directly, without the element. See Protocol for the URL parameters.