castle-web-sdk 0.1.0 → 0.2.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 +9 -8
- package/castle.js +85 -53
- package/package.json +1 -1
package/AGENTS.md
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
# Castle Web SDK
|
|
2
2
|
|
|
3
|
-
Import from `./castle.js`
|
|
3
|
+
Import from `castle-web-sdk` (or `./castle.js` directly).
|
|
4
4
|
|
|
5
5
|
## API
|
|
6
6
|
|
|
7
|
-
### `
|
|
7
|
+
### `setup()`
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Connects to the CLI dev server for console log forwarding, screenshot capture, and live reload. Call once at startup.
|
|
10
10
|
|
|
11
11
|
```js
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
import { setup } from 'castle-web-sdk';
|
|
13
|
+
setup();
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
### `
|
|
16
|
+
### `initCard()` -> HTMLElement
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
Creates a card-sized container (5:7 aspect ratio) that fills the viewport. Mount your game inside the returned element.
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
|
-
|
|
21
|
+
const card = initCard();
|
|
22
|
+
card.appendChild(myGameElement);
|
|
22
23
|
```
|
|
23
24
|
|
|
24
25
|
### Constants
|
package/castle.js
CHANGED
|
@@ -2,35 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
export const CARD_RATIO = 5 / 7;
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
let _ws = null;
|
|
6
|
+
let _logBuffer = [];
|
|
7
|
+
|
|
8
|
+
export function setup() {
|
|
9
|
+
_interceptConsole();
|
|
10
|
+
_connectLocal();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// --- Card shell ---
|
|
14
|
+
|
|
14
15
|
export function initCard() {
|
|
15
|
-
// Reset page styles
|
|
16
16
|
const style = document.createElement('style');
|
|
17
17
|
style.textContent = `
|
|
18
18
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
19
|
-
html, body {
|
|
20
|
-
|
|
21
|
-
background: #000;
|
|
22
|
-
overflow: hidden;
|
|
23
|
-
}
|
|
24
|
-
body {
|
|
25
|
-
display: flex;
|
|
26
|
-
align-items: center;
|
|
27
|
-
justify-content: center;
|
|
28
|
-
}
|
|
19
|
+
html, body { width: 100%; height: 100%; background: #000; overflow: hidden; }
|
|
20
|
+
body { display: flex; align-items: center; justify-content: center; }
|
|
29
21
|
#castle-card {
|
|
30
22
|
position: relative;
|
|
23
|
+
border-radius: 4% / calc(4% * (5 / 7));
|
|
31
24
|
overflow: hidden;
|
|
32
25
|
background: #121213;
|
|
33
|
-
border-radius: 4% / calc(4% * (5 / 7));
|
|
34
26
|
}
|
|
35
27
|
`;
|
|
36
28
|
document.head.appendChild(style);
|
|
@@ -40,45 +32,85 @@ export function initCard() {
|
|
|
40
32
|
document.body.appendChild(card);
|
|
41
33
|
|
|
42
34
|
function resize() {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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;
|
|
35
|
+
if (window.CastleEmbed) {
|
|
36
|
+
card.style.width = '100vw';
|
|
37
|
+
card.style.height = '100vh';
|
|
38
|
+
return;
|
|
55
39
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
40
|
+
const maxW = 450, maxH = 630, pad = 20;
|
|
41
|
+
const aw = window.innerWidth - pad * 2;
|
|
42
|
+
const ah = window.innerHeight - pad * 2;
|
|
43
|
+
let w, h;
|
|
44
|
+
if (aw / ah < 5 / 7) { w = Math.min(aw, maxW); h = w * 7 / 5; }
|
|
45
|
+
else { h = Math.min(ah, maxH); w = h * 5 / 7; }
|
|
46
|
+
card.style.width = w + 'px';
|
|
47
|
+
card.style.height = h + 'px';
|
|
59
48
|
}
|
|
60
|
-
|
|
61
49
|
resize();
|
|
62
50
|
window.addEventListener('resize', resize);
|
|
63
51
|
|
|
64
52
|
return card;
|
|
65
53
|
}
|
|
66
54
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
version = data.version;
|
|
80
|
-
} catch {}
|
|
81
|
-
setTimeout(poll, 100);
|
|
55
|
+
// --- Console log forwarding ---
|
|
56
|
+
|
|
57
|
+
const _origLog = console.log;
|
|
58
|
+
const _origWarn = console.warn;
|
|
59
|
+
const _origError = console.error;
|
|
60
|
+
|
|
61
|
+
function _sendMsg(msg) {
|
|
62
|
+
if (_ws && _ws.readyState === WebSocket.OPEN) {
|
|
63
|
+
_ws.send(JSON.stringify(msg));
|
|
64
|
+
} else {
|
|
65
|
+
_logBuffer.push(msg);
|
|
82
66
|
}
|
|
83
|
-
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function _interceptConsole() {
|
|
70
|
+
console.log = (...args) => { _origLog(...args); _sendMsg({ type: 'log', level: 'log', msg: args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ') }); };
|
|
71
|
+
console.warn = (...args) => { _origWarn(...args); _sendMsg({ type: 'log', level: 'warn', msg: args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ') }); };
|
|
72
|
+
console.error = (...args) => { _origError(...args); _sendMsg({ type: 'log', level: 'error', msg: args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ') }); };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// --- Screenshot ---
|
|
76
|
+
|
|
77
|
+
async function _captureScreenshot() {
|
|
78
|
+
const canvas = document.querySelector('canvas');
|
|
79
|
+
if (canvas) return canvas.toDataURL('image/png');
|
|
80
|
+
try {
|
|
81
|
+
const { default: html2canvas } = await import('https://esm.sh/html2canvas');
|
|
82
|
+
const c = await html2canvas(document.body);
|
|
83
|
+
return c.toDataURL('image/png');
|
|
84
|
+
} catch { return null; }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// --- Local WebSocket ---
|
|
88
|
+
|
|
89
|
+
function _connectLocal() {
|
|
90
|
+
fetch('/__castle/ws-port')
|
|
91
|
+
.then(r => r.json())
|
|
92
|
+
.then(({ port }) => {
|
|
93
|
+
if (!port) return;
|
|
94
|
+
const ws = new WebSocket(`ws://localhost:${port}`);
|
|
95
|
+
ws.onopen = () => {
|
|
96
|
+
_ws = ws;
|
|
97
|
+
for (const msg of _logBuffer) _ws.send(JSON.stringify(msg));
|
|
98
|
+
_logBuffer = [];
|
|
99
|
+
};
|
|
100
|
+
ws.onmessage = (evt) => {
|
|
101
|
+
try {
|
|
102
|
+
const msg = JSON.parse(evt.data);
|
|
103
|
+
if (msg.type === 'screenshot_request') {
|
|
104
|
+
_captureScreenshot().then(data => {
|
|
105
|
+
if (data) _sendMsg({ type: 'screenshot_response', requestId: msg.requestId, data });
|
|
106
|
+
});
|
|
107
|
+
} else if (msg.type === 'restart') {
|
|
108
|
+
location.reload();
|
|
109
|
+
}
|
|
110
|
+
} catch {}
|
|
111
|
+
};
|
|
112
|
+
ws.onclose = () => { _ws = null; setTimeout(_connectLocal, 2000); };
|
|
113
|
+
ws.onerror = () => ws.close();
|
|
114
|
+
})
|
|
115
|
+
.catch(() => {});
|
|
84
116
|
}
|