@replayablejs/canvas 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +35 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +72 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Replayable contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @replayablejs/canvas
|
|
2
|
+
|
|
3
|
+
Provide a shared canvas host for rendering integrations.
|
|
4
|
+
|
|
5
|
+
Part of [Replayable](https://github.com/replayablejs/replayable) **0.1.0-alpha.0**.
|
|
6
|
+
APIs may change during the alpha series.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pnpm add @replayablejs/canvas@0.1.0-alpha.0
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Public surface
|
|
15
|
+
|
|
16
|
+
`getCanvasHost`, `CanvasHost`, `SharedRenderingContext`.
|
|
17
|
+
|
|
18
|
+
[Usage and reference](https://github.com/replayablejs/replayable/blob/main/docs/reference/canvas.md).
|
|
19
|
+
The package manifest defines supported import paths; internal source files are not public APIs.
|
|
20
|
+
|
|
21
|
+
## Development
|
|
22
|
+
|
|
23
|
+
From the repository root, install with `pnpm install --frozen-lockfile` and build dependencies
|
|
24
|
+
with `pnpm build`. Run `pnpm --filter @replayablejs/canvas test` for this package's tests.
|
|
25
|
+
|
|
26
|
+
## Peer dependencies
|
|
27
|
+
|
|
28
|
+
- `@replayablejs/runtime`: `workspace:*`
|
|
29
|
+
|
|
30
|
+
Workspace ranges are converted to package versions when packed.
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
Original code is [MIT licensed](https://github.com/replayablejs/replayable/blob/main/LICENSE). Bundled third-party resources retain
|
|
35
|
+
their accompanying license terms.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/types/canvas-host.d.ts
|
|
2
|
+
interface CanvasHost {
|
|
3
|
+
/** Returns the canvas mounted inside Replayable's runtime container. */
|
|
4
|
+
getCanvas(): HTMLCanvasElement;
|
|
5
|
+
/** Returns the WebGL context registered by a renderer, or `null` before registration. */
|
|
6
|
+
getSharedContext(): SharedRenderingContext | null;
|
|
7
|
+
/** Registers the first renderer's context; rejects replacement or use after destruction. */
|
|
8
|
+
setSharedContext(context: SharedRenderingContext): void;
|
|
9
|
+
/** Removes the canvas once and lets the next access create a fresh shared host. */
|
|
10
|
+
destroy(): void;
|
|
11
|
+
}
|
|
12
|
+
type SharedRenderingContext = WebGLRenderingContext | WebGL2RenderingContext;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/host/get-canvas-host.d.ts
|
|
15
|
+
/** Returns the canvas host shared by every renderer in the playable. */
|
|
16
|
+
declare function getCanvasHost(): CanvasHost;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { type CanvasHost, type SharedRenderingContext, getCanvasHost };
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { playable } from "@replayablejs/runtime";
|
|
2
|
+
import { registerWebglContext } from "#webgl-stats";
|
|
3
|
+
//#region src/host/canvas-ids.ts
|
|
4
|
+
/** ID of the generated runtime canvas element. */
|
|
5
|
+
const REPLAYABLE_CANVAS_ID = "replayable-canvas";
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/host/canvas.scss?inline
|
|
8
|
+
var canvas_default = "#replayable-container #replayable-canvas{width:100%;height:100%;display:block;position:absolute;inset:0}\n";
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/host/install-canvas-styles.ts
|
|
11
|
+
const CANVAS_STYLE_ID = "replayable-canvas-styles";
|
|
12
|
+
/** Installs canvas host base styles once per document. */
|
|
13
|
+
function installCanvasStyles() {
|
|
14
|
+
if (document.getElementById(CANVAS_STYLE_ID) !== null) return;
|
|
15
|
+
const style = document.createElement("style");
|
|
16
|
+
style.id = CANVAS_STYLE_ID;
|
|
17
|
+
style.textContent = canvas_default.trim();
|
|
18
|
+
document.head.append(style);
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/host/get-canvas-host.ts
|
|
22
|
+
let sharedCanvasHost;
|
|
23
|
+
/** Returns the canvas host shared by every renderer in the playable. */
|
|
24
|
+
function getCanvasHost() {
|
|
25
|
+
sharedCanvasHost ??= createCanvasHost();
|
|
26
|
+
return sharedCanvasHost;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a replayable canvas host and mounts it into the runtime container.
|
|
30
|
+
*
|
|
31
|
+
* The host only owns DOM placement and shared-context handoff; it never creates
|
|
32
|
+
* or configures rendering contexts itself.
|
|
33
|
+
*/
|
|
34
|
+
function createCanvasHost() {
|
|
35
|
+
installCanvasStyles();
|
|
36
|
+
const canvas = document.createElement("canvas");
|
|
37
|
+
let destroyed = false;
|
|
38
|
+
let sharedContext = null;
|
|
39
|
+
let unregisterContext;
|
|
40
|
+
canvas.id = REPLAYABLE_CANVAS_ID;
|
|
41
|
+
playable.container.append(canvas);
|
|
42
|
+
const host = {
|
|
43
|
+
getCanvas: () => canvas,
|
|
44
|
+
getSharedContext: () => sharedContext,
|
|
45
|
+
setSharedContext,
|
|
46
|
+
destroy
|
|
47
|
+
};
|
|
48
|
+
return host;
|
|
49
|
+
/** Commits the renderer's context only after registration succeeds, allowing retries. */
|
|
50
|
+
function setSharedContext(context) {
|
|
51
|
+
if (destroyed) throw new Error("Cannot register a WebGL context on a destroyed Replayable canvas host.");
|
|
52
|
+
if (sharedContext === context) return;
|
|
53
|
+
if (sharedContext !== null) throw new Error("Replayable canvas already has a different WebGL context.");
|
|
54
|
+
unregisterContext = registerWebglContext(context);
|
|
55
|
+
sharedContext = context;
|
|
56
|
+
}
|
|
57
|
+
/** Releases local ownership before notifying observers, whose cleanup may throw. */
|
|
58
|
+
function destroy() {
|
|
59
|
+
if (destroyed) return;
|
|
60
|
+
destroyed = true;
|
|
61
|
+
const unregister = unregisterContext;
|
|
62
|
+
unregisterContext = void 0;
|
|
63
|
+
sharedContext = null;
|
|
64
|
+
canvas.remove();
|
|
65
|
+
if (sharedCanvasHost === host) sharedCanvasHost = void 0;
|
|
66
|
+
unregister?.();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
export { getCanvasHost };
|
|
71
|
+
|
|
72
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["canvasStyles"],"sources":["../src/host/canvas-ids.ts","../src/host/canvas.scss?inline","../src/host/install-canvas-styles.ts","../src/host/get-canvas-host.ts"],"sourcesContent":["/** ID of the generated runtime canvas element. */\nexport const REPLAYABLE_CANVAS_ID = 'replayable-canvas';\n","#replayable-container #replayable-canvas {\n position: absolute;\n inset: 0;\n display: block;\n width: 100%;\n height: 100%;\n}","import canvasStyles from './canvas.scss?inline';\n\nconst CANVAS_STYLE_ID = 'replayable-canvas-styles';\n\n/** Installs canvas host base styles once per document. */\nexport function installCanvasStyles(): void {\n if (document.getElementById(CANVAS_STYLE_ID) !== null) {\n return;\n }\n\n const style = document.createElement('style');\n style.id = CANVAS_STYLE_ID;\n style.textContent = canvasStyles.trim();\n document.head.append(style);\n}\n","import { playable } from '@replayablejs/runtime';\n\nimport type { CanvasHost, SharedRenderingContext } from '#types/canvas-host.js';\nimport { registerWebglContext } from '#webgl-stats';\n\nimport { REPLAYABLE_CANVAS_ID } from './canvas-ids.js';\nimport { installCanvasStyles } from './install-canvas-styles.js';\n\nlet sharedCanvasHost: CanvasHost | undefined;\n\n/** Returns the canvas host shared by every renderer in the playable. */\nexport function getCanvasHost(): CanvasHost {\n sharedCanvasHost ??= createCanvasHost();\n\n return sharedCanvasHost;\n}\n\n/**\n * Creates a replayable canvas host and mounts it into the runtime container.\n *\n * The host only owns DOM placement and shared-context handoff; it never creates\n * or configures rendering contexts itself.\n */\nfunction createCanvasHost(): CanvasHost {\n installCanvasStyles();\n\n const canvas = document.createElement('canvas');\n let destroyed = false;\n let sharedContext: SharedRenderingContext | null = null;\n let unregisterContext: (() => void) | undefined;\n\n canvas.id = REPLAYABLE_CANVAS_ID;\n playable.container.append(canvas);\n\n const host: CanvasHost = {\n getCanvas: () => canvas,\n getSharedContext: (): SharedRenderingContext | null => sharedContext,\n setSharedContext,\n destroy,\n };\n\n return host;\n\n /** Commits the renderer's context only after registration succeeds, allowing retries. */\n function setSharedContext(context: SharedRenderingContext): void {\n if (destroyed) {\n throw new Error('Cannot register a WebGL context on a destroyed Replayable canvas host.');\n }\n\n // Both renderers may report the same shared context; register it only once.\n if (sharedContext === context) {\n return;\n }\n if (sharedContext !== null) {\n throw new Error('Replayable canvas already has a different WebGL context.');\n }\n\n unregisterContext = registerWebglContext(context);\n sharedContext = context;\n }\n\n /** Releases local ownership before notifying observers, whose cleanup may throw. */\n function destroy(): void {\n if (destroyed) {\n return;\n }\n destroyed = true;\n\n const unregister = unregisterContext;\n unregisterContext = undefined;\n sharedContext = null;\n canvas.remove();\n\n if (sharedCanvasHost === host) {\n sharedCanvasHost = undefined;\n }\n\n // Errors still reach the caller, but cannot leave a mounted or reusable dead host.\n unregister?.();\n }\n}\n"],"mappings":";;;;AACA,MAAa,uBAAuB;;;ACDpC,IAAA,iBAAA;;;ACEA,MAAM,kBAAkB;;AAGxB,SAAgB,sBAA4B;CAC1C,IAAI,SAAS,eAAe,eAAe,MAAM,MAC/C;CAGF,MAAM,QAAQ,SAAS,cAAc,OAAO;CAC5C,MAAM,KAAK;CACX,MAAM,cAAcA,eAAa,KAAK;CACtC,SAAS,KAAK,OAAO,KAAK;AAC5B;;;ACNA,IAAI;;AAGJ,SAAgB,gBAA4B;CAC1C,qBAAqB,iBAAiB;CAEtC,OAAO;AACT;;;;;;;AAQA,SAAS,mBAA+B;CACtC,oBAAoB;CAEpB,MAAM,SAAS,SAAS,cAAc,QAAQ;CAC9C,IAAI,YAAY;CAChB,IAAI,gBAA+C;CACnD,IAAI;CAEJ,OAAO,KAAK;CACZ,SAAS,UAAU,OAAO,MAAM;CAEhC,MAAM,OAAmB;EACvB,iBAAiB;EACjB,wBAAuD;EACvD;EACA;CACF;CAEA,OAAO;;CAGP,SAAS,iBAAiB,SAAuC;EAC/D,IAAI,WACF,MAAM,IAAI,MAAM,wEAAwE;EAI1F,IAAI,kBAAkB,SACpB;EAEF,IAAI,kBAAkB,MACpB,MAAM,IAAI,MAAM,0DAA0D;EAG5E,oBAAoB,qBAAqB,OAAO;EAChD,gBAAgB;CAClB;;CAGA,SAAS,UAAgB;EACvB,IAAI,WACF;EAEF,YAAY;EAEZ,MAAM,aAAa;EACnB,oBAAoB,KAAA;EACpB,gBAAgB;EAChB,OAAO,OAAO;EAEd,IAAI,qBAAqB,MACvB,mBAAmB,KAAA;EAIrB,aAAa;CACf;AACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@replayablejs/canvas",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "Renderer-agnostic canvas host utilities for Replayable playables",
|
|
5
|
+
"homepage": "https://github.com/replayablejs/replayable#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/replayablejs/replayable/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/replayablejs/replayable.git",
|
|
13
|
+
"directory": "packages/canvas"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"imports": {
|
|
21
|
+
"#types/*": "./src/types/*"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@tsdown/css": "0.22.14",
|
|
34
|
+
"happy-dom": "20.13.2",
|
|
35
|
+
"sass-embedded": "1.103.1",
|
|
36
|
+
"tsdown": "0.22.14",
|
|
37
|
+
"typescript": "7.0.2",
|
|
38
|
+
"vitest": "4.1.10",
|
|
39
|
+
"@replayablejs/runtime": "0.1.0-alpha.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@replayablejs/runtime": "0.1.0-alpha.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=24.0.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsdown",
|
|
49
|
+
"dev": "tsdown --watch",
|
|
50
|
+
"lint": "oxlint --type-aware --max-warnings 0 .",
|
|
51
|
+
"test": "vitest run",
|
|
52
|
+
"typecheck": "tsc --noEmit"
|
|
53
|
+
}
|
|
54
|
+
}
|