castle-web-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +26 -0
- package/castle.js +84 -0
- package/package.json +9 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Castle Web SDK
|
|
2
|
+
|
|
3
|
+
Import from `./castle.js` in your project.
|
|
4
|
+
|
|
5
|
+
## API
|
|
6
|
+
|
|
7
|
+
### `initCard()` -> HTMLElement
|
|
8
|
+
|
|
9
|
+
Creates a card-sized container (5:7 aspect ratio) that fills the viewport. Mount your game inside the returned element.
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const card = initCard();
|
|
13
|
+
card.appendChild(myGameElement);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### `enableHotReload()`
|
|
17
|
+
|
|
18
|
+
Polls the CLI dev server for file changes and reloads automatically. Call once at startup.
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
enableHotReload();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Constants
|
|
25
|
+
|
|
26
|
+
- `CARD_RATIO` — `5 / 7`
|
package/castle.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Castle Web SDK
|
|
2
|
+
|
|
3
|
+
export const CARD_RATIO = 5 / 7;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Initialize card-sized rendering.
|
|
7
|
+
* Creates a container div sized to 5:7 ratio that fills the viewport.
|
|
8
|
+
* Returns the container element — mount your game inside it.
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* const container = castle.initCard();
|
|
12
|
+
* container.innerHTML = '<h1>My Game</h1>';
|
|
13
|
+
*/
|
|
14
|
+
export function initCard() {
|
|
15
|
+
// Reset page styles
|
|
16
|
+
const style = document.createElement('style');
|
|
17
|
+
style.textContent = `
|
|
18
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
19
|
+
html, body {
|
|
20
|
+
width: 100%; height: 100%;
|
|
21
|
+
background: #000;
|
|
22
|
+
overflow: hidden;
|
|
23
|
+
}
|
|
24
|
+
body {
|
|
25
|
+
display: flex;
|
|
26
|
+
align-items: center;
|
|
27
|
+
justify-content: center;
|
|
28
|
+
}
|
|
29
|
+
#castle-card {
|
|
30
|
+
position: relative;
|
|
31
|
+
overflow: hidden;
|
|
32
|
+
background: #121213;
|
|
33
|
+
border-radius: 4% / calc(4% * (5 / 7));
|
|
34
|
+
}
|
|
35
|
+
`;
|
|
36
|
+
document.head.appendChild(style);
|
|
37
|
+
|
|
38
|
+
const card = document.createElement('div');
|
|
39
|
+
card.id = 'castle-card';
|
|
40
|
+
document.body.appendChild(card);
|
|
41
|
+
|
|
42
|
+
function resize() {
|
|
43
|
+
const vw = window.innerWidth;
|
|
44
|
+
const vh = window.innerHeight;
|
|
45
|
+
|
|
46
|
+
let w, h;
|
|
47
|
+
if (vw / vh < CARD_RATIO) {
|
|
48
|
+
// Width-limited
|
|
49
|
+
w = vw;
|
|
50
|
+
h = vw / CARD_RATIO;
|
|
51
|
+
} else {
|
|
52
|
+
// Height-limited
|
|
53
|
+
h = vh;
|
|
54
|
+
w = vh * CARD_RATIO;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
card.style.width = `${w}px`;
|
|
58
|
+
card.style.height = `${h}px`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
resize();
|
|
62
|
+
window.addEventListener('resize', resize);
|
|
63
|
+
|
|
64
|
+
return card;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Enable hot reload. Polls the CLI dev server for file changes.
|
|
69
|
+
*/
|
|
70
|
+
export function enableHotReload() {
|
|
71
|
+
let version = 0;
|
|
72
|
+
async function poll() {
|
|
73
|
+
try {
|
|
74
|
+
const res = await fetch(`/__version?v=${version}`);
|
|
75
|
+
const data = await res.json();
|
|
76
|
+
if (version > 0 && data.version > version) {
|
|
77
|
+
location.reload();
|
|
78
|
+
}
|
|
79
|
+
version = data.version;
|
|
80
|
+
} catch {}
|
|
81
|
+
setTimeout(poll, 100);
|
|
82
|
+
}
|
|
83
|
+
poll();
|
|
84
|
+
}
|