ข้ามไปยังเนื้อหา

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.

The package is private and published to GitHub Packages under the chatbotgang organization.

.npmrc
@chatbotgang:registry=https://npm.pkg.github.com
Terminal window
pnpm add @chatbotgang/mini-games

Importing 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.

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).

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.

The event names are lowercase without hyphens, so every framework binds them directly.

React 19
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)}
/>;
Vue
<cl-mini-game game="random" :theme="theme" @gameerror="onError" />
Svelte 5
<cl-mini-game game="random" {theme} ongameerror={onError}></cl-mini-game>
Solid
<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.

A page with a CSP must allow the site in frame-src:

frame-src https://mini-games.cresclab.com

The game site only allows framing from *.cresclab.com, Firebase Hosting preview channels (*.web.app) and local development hosts.

An app WebView can open a game URL directly, without the element. See Protocol for the URL parameters.