@pretty-chitty/core 1.2.9 → 1.2.15
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/README.md +23 -0
- package/dist/components/ActionLogDisplay.js +1 -1
- package/dist/components/ActionLogDisplay.js.map +1 -1
- package/dist/components/ActionLogHistory.js +1 -1
- package/dist/components/ActionLogHistory.js.map +1 -1
- package/dist/components/BottomBarButton.d.ts.map +1 -1
- package/dist/components/BottomBarButton.js +12 -1
- package/dist/components/BottomBarButton.js.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.d.ts.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.js +5 -1
- package/dist/components/FullScreenGalleryDisplay.js.map +1 -1
- package/dist/components/Gallery/AnimationController.d.ts +1 -0
- package/dist/components/Gallery/AnimationController.d.ts.map +1 -1
- package/dist/components/Gallery/AnimationController.js +9 -0
- package/dist/components/Gallery/AnimationController.js.map +1 -1
- package/dist/components/Gallery/BuiltItem.d.ts +2 -1
- package/dist/components/Gallery/BuiltItem.d.ts.map +1 -1
- package/dist/components/Gallery/BuiltItem.js +15 -3
- package/dist/components/Gallery/BuiltItem.js.map +1 -1
- package/dist/components/Gallery/GalleryController.d.ts +1 -0
- package/dist/components/Gallery/GalleryController.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryController.js +15 -4
- package/dist/components/Gallery/GalleryController.js.map +1 -1
- package/dist/components/Gallery/GalleryViewer.d.ts +2 -1
- package/dist/components/Gallery/GalleryViewer.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryViewer.js +13 -4
- package/dist/components/Gallery/GalleryViewer.js.map +1 -1
- package/dist/components/GameDialog.js +1 -1
- package/dist/components/GameDialog.js.map +1 -1
- package/dist/components/InlineGalleryDisplay.d.ts.map +1 -1
- package/dist/components/InlineGalleryDisplay.js +5 -1
- package/dist/components/InlineGalleryDisplay.js.map +1 -1
- package/dist/components/Playground.d.ts.map +1 -1
- package/dist/components/Playground.js +59 -21
- package/dist/components/Playground.js.map +1 -1
- package/dist/components/ServerTrustMatchViewer.d.ts.map +1 -1
- package/dist/components/ServerTrustMatchViewer.js +10 -2
- package/dist/components/ServerTrustMatchViewer.js.map +1 -1
- package/dist/components/SparkLineChart.d.ts.map +1 -1
- package/dist/components/SparkLineChart.js +62 -22
- package/dist/components/SparkLineChart.js.map +1 -1
- package/dist/components/TopBarDropdown.js +4 -4
- package/dist/components/TopBarDropdown.js.map +1 -1
- package/dist/components/TopBarPlayers.d.ts.map +1 -1
- package/dist/components/TopBarPlayers.js +9 -3
- package/dist/components/TopBarPlayers.js.map +1 -1
- package/dist/components/Viewer.d.ts.map +1 -1
- package/dist/components/Viewer.js +15 -6
- package/dist/components/Viewer.js.map +1 -1
- package/dist/game/Chit.d.ts +10 -1
- package/dist/game/Chit.d.ts.map +1 -1
- package/dist/game/Chit.js +125 -4
- package/dist/game/Chit.js.map +1 -1
- package/dist/game/Connection.d.ts +1 -0
- package/dist/game/Connection.d.ts.map +1 -1
- package/dist/game/Connection.js +27 -8
- package/dist/game/Connection.js.map +1 -1
- package/dist/game/ConnectionTransport.d.ts +5 -0
- package/dist/game/ConnectionTransport.d.ts.map +1 -1
- package/dist/game/ConnectionTransport.js +32 -0
- package/dist/game/ConnectionTransport.js.map +1 -1
- package/dist/game/GalleryItemChitChildrenSource.d.ts.map +1 -1
- package/dist/game/GalleryItemChitChildrenSource.js +9 -1
- package/dist/game/GalleryItemChitChildrenSource.js.map +1 -1
- package/dist/game/GameTheme.d.ts +3 -0
- package/dist/game/GameTheme.d.ts.map +1 -1
- package/dist/game/GameTheme.js +3 -0
- package/dist/game/GameTheme.js.map +1 -1
- package/dist/game/OrderedOutlet.d.ts +4 -1
- package/dist/game/OrderedOutlet.d.ts.map +1 -1
- package/dist/game/OrderedOutlet.js +25 -4
- package/dist/game/OrderedOutlet.js.map +1 -1
- package/dist/game/Pick.d.ts +1 -0
- package/dist/game/Pick.d.ts.map +1 -1
- package/dist/game/Pick.js +40 -1
- package/dist/game/Pick.js.map +1 -1
- package/dist/game/Prompt.d.ts +11 -1
- package/dist/game/Prompt.d.ts.map +1 -1
- package/dist/game/Prompt.js +34 -1
- package/dist/game/Prompt.js.map +1 -1
- package/dist/game/Turn.d.ts +17 -0
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +114 -63
- package/dist/game/Turn.js.map +1 -1
- package/dist/game/badAiTransport/BadAIClientPrompts.d.ts.map +1 -1
- package/dist/game/badAiTransport/BadAIClientPrompts.js +24 -4
- package/dist/game/badAiTransport/BadAIClientPrompts.js.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.d.ts +2 -0
- package/dist/game/clientTransport/ClientPrompts.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.js +48 -16
- package/dist/game/clientTransport/ClientPrompts.js.map +1 -1
- package/dist/game/clientTransport/ClientTime.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientTime.js +58 -52
- package/dist/game/clientTransport/ClientTime.js.map +1 -1
- package/dist/game/serverTransport/ServerPrompts.d.ts +1 -1
- package/dist/game/serverTransport/ServerPrompts.d.ts.map +1 -1
- package/dist/game/serverTransport/ServerPrompts.js +14 -3
- package/dist/game/serverTransport/ServerPrompts.js.map +1 -1
- package/dist/game/serverTransport/ServerStatus.d.ts.map +1 -1
- package/dist/game/serverTransport/ServerStatus.js +6 -2
- package/dist/game/serverTransport/ServerStatus.js.map +1 -1
- package/dist/game/serverTransport/ServerTime.d.ts.map +1 -1
- package/dist/game/serverTransport/ServerTime.js +6 -2
- package/dist/game/serverTransport/ServerTime.js.map +1 -1
- package/dist/hooks/useTimeController.d.ts.map +1 -1
- package/dist/hooks/useTimeController.js +3 -0
- package/dist/hooks/useTimeController.js.map +1 -1
- package/dist/hooks/useWebGlRenderer.d.ts +27 -1
- package/dist/hooks/useWebGlRenderer.d.ts.map +1 -1
- package/dist/hooks/useWebGlRenderer.js +179 -6
- package/dist/hooks/useWebGlRenderer.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/rendering/ChitGalleryItemInstance.js +1 -1
- package/dist/rendering/ChitGalleryItemInstance.js.map +1 -1
- package/dist/rendering/ChitRenderInstance.d.ts.map +1 -1
- package/dist/rendering/ChitRenderInstance.js +30 -9
- package/dist/rendering/ChitRenderInstance.js.map +1 -1
- package/dist/rendering/ChitRenderSpec.js +1 -1
- package/dist/rendering/ChitRenderSpec.js.map +1 -1
- package/dist/rendering/outline/IDBasedOutlinePass.d.ts.map +1 -1
- package/dist/rendering/outline/IDBasedOutlinePass.js +6 -2
- package/dist/rendering/outline/IDBasedOutlinePass.js.map +1 -1
- package/dist/rendering/outline/passes/InterMeshEdgeDetectionPass.d.ts.map +1 -1
- package/dist/rendering/outline/passes/InterMeshEdgeDetectionPass.js +7 -3
- package/dist/rendering/outline/passes/InterMeshEdgeDetectionPass.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.js +34 -13
- package/dist/utilities/CanvasStack/CanvasOperations.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts +9 -0
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +37 -2
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/CanvasStack/RichTextRenderer.d.ts +2 -0
- package/dist/utilities/CanvasStack/RichTextRenderer.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/RichTextRenderer.js +47 -4
- package/dist/utilities/CanvasStack/RichTextRenderer.js.map +1 -1
- package/package.json +1 -1
- package/src/library/components/ActionLogDisplay.tsx +1 -1
- package/src/library/components/ActionLogHistory.tsx +2 -2
- package/src/library/components/BottomBarButton.tsx +19 -2
- package/src/library/components/FullScreenGalleryDisplay.tsx +5 -0
- package/src/library/components/Gallery/AnimationController.ts +10 -0
- package/src/library/components/Gallery/BuiltItem.ts +16 -3
- package/src/library/components/Gallery/GalleryController.ts +15 -4
- package/src/library/components/Gallery/GalleryViewer.tsx +17 -3
- package/src/library/components/GameDialog.tsx +1 -1
- package/src/library/components/InlineGalleryDisplay.tsx +5 -0
- package/src/library/components/Playground.tsx +76 -20
- package/src/library/components/ServerTrustMatchViewer.tsx +10 -2
- package/src/library/components/SparkLineChart.tsx +85 -24
- package/src/library/components/TopBarDropdown.tsx +4 -4
- package/src/library/components/TopBarPlayers.tsx +9 -3
- package/src/library/components/Viewer.tsx +15 -6
- package/src/library/game/Chit.test.ts +91 -0
- package/src/library/game/Chit.ts +122 -9
- package/src/library/game/Connection.ts +27 -8
- package/src/library/game/ConnectionTransport.ts +38 -0
- package/src/library/game/GalleryItemChitChildrenSource.ts +9 -1
- package/src/library/game/GameTheme.ts +4 -0
- package/src/library/game/OrderedOutlet.ts +27 -4
- package/src/library/game/Pick.ts +46 -4
- package/src/library/game/Prompt.ts +43 -3
- package/src/library/game/Turn.ts +127 -78
- package/src/library/game/badAiTransport/BadAIClientPrompts.ts +21 -4
- package/src/library/game/clientTransport/ClientPrompts.ts +50 -16
- package/src/library/game/clientTransport/ClientTime.ts +61 -56
- package/src/library/game/serverTransport/ServerPrompts.ts +25 -7
- package/src/library/game/serverTransport/ServerStatus.ts +6 -2
- package/src/library/game/serverTransport/ServerTime.ts +6 -2
- package/src/library/hooks/useTimeController.tsx +3 -0
- package/src/library/hooks/useWebGlRenderer.tsx +212 -6
- package/src/library/index.ts +1 -1
- package/src/library/rendering/ChitGalleryItemInstance.ts +1 -1
- package/src/library/rendering/ChitRenderInstance.ts +32 -9
- package/src/library/rendering/ChitRenderSpec.ts +1 -1
- package/src/library/rendering/outline/IDBasedOutlinePass.ts +6 -2
- package/src/library/rendering/outline/passes/InterMeshEdgeDetectionPass.ts +7 -3
- package/src/library/utilities/CanvasStack/CanvasOperations.tsx +44 -13
- package/src/library/utilities/CanvasStack/CanvasStack.ts +38 -1
- package/src/library/utilities/CanvasStack/RichTextRenderer.ts +66 -5
|
@@ -29,11 +29,13 @@ export class ServerPrompts<P extends PlayerChit, R extends RootChit<P>> extends
|
|
|
29
29
|
if (newTurn) {
|
|
30
30
|
unsubs = newTurn.rootChit.players.map((player) =>
|
|
31
31
|
player.promptStatus.latestPrompt.on((latestPrompt: any) =>
|
|
32
|
-
this.clientPrompts
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
this.clientPrompts
|
|
33
|
+
.setPromptForPlayer(
|
|
34
|
+
player.playerId,
|
|
35
|
+
latestPrompt?.serialize(),
|
|
36
|
+
this.match.turn.value?.clockDetails(playerId),
|
|
37
|
+
)
|
|
38
|
+
.catch((e: any) => console.error("Failed to send prompt to client:", e)),
|
|
37
39
|
),
|
|
38
40
|
);
|
|
39
41
|
}
|
|
@@ -51,14 +53,30 @@ export class ServerPrompts<P extends PlayerChit, R extends RootChit<P>> extends
|
|
|
51
53
|
return this.match.turn.value?.rootChit.players;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
async resolvePrompt(response: any): Promise<void | PromptSerialization> {
|
|
56
|
+
async resolvePrompt(promptId: string, response: any): Promise<void | PromptSerialization> {
|
|
55
57
|
const player = this.playerChits?.find((p) => p.playerId === this.playerId);
|
|
56
58
|
if (player && player.promptStatus.latestPrompt.value) {
|
|
59
|
+
const currentPrompt = player.promptStatus.latestPrompt.value;
|
|
60
|
+
|
|
61
|
+
if (currentPrompt.id !== promptId) {
|
|
62
|
+
console.warn(
|
|
63
|
+
`[ServerPrompts] Prompt ID mismatch for player ${this.playerId}: received "${promptId}", expected "${currentPrompt.id}". Ignoring resolution.`,
|
|
64
|
+
);
|
|
65
|
+
return currentPrompt.serialize();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!currentPrompt.canResolveResponse(response)) {
|
|
69
|
+
console.warn(
|
|
70
|
+
`[ServerPrompts] Prompt "${currentPrompt.id}" for player ${this.playerId} cannot resolve response (type=${currentPrompt.type}). Ignoring resolution.`,
|
|
71
|
+
);
|
|
72
|
+
return currentPrompt.serialize();
|
|
73
|
+
}
|
|
74
|
+
|
|
57
75
|
let cb: (() => void) | undefined;
|
|
58
76
|
const p = new Promise((resolve) => {
|
|
59
77
|
cb = this.match.onChange(() => resolve(0), false);
|
|
60
78
|
});
|
|
61
|
-
|
|
79
|
+
currentPrompt.resolve(response);
|
|
62
80
|
await p;
|
|
63
81
|
if (cb) {
|
|
64
82
|
cb();
|
|
@@ -18,13 +18,17 @@ export class ServerStatus<P extends PlayerChit, R extends RootChit<P>> extends C
|
|
|
18
18
|
|
|
19
19
|
this.register(
|
|
20
20
|
this.match.errorState.on((errorMessage) => {
|
|
21
|
-
this.clientStatus
|
|
21
|
+
this.clientStatus
|
|
22
|
+
.setErrorMessage(errorMessage)
|
|
23
|
+
.catch((e: any) => console.error("Failed to send error message to client:", e));
|
|
22
24
|
}),
|
|
23
25
|
);
|
|
24
26
|
|
|
25
27
|
this.register(
|
|
26
28
|
this.match.result.on((result) => {
|
|
27
|
-
this.clientStatus
|
|
29
|
+
this.clientStatus
|
|
30
|
+
.setMatchResult(result?.winners?.map((winner) => winner.id ?? ""))
|
|
31
|
+
.catch((e: any) => console.error("Failed to send match result to client:", e));
|
|
28
32
|
}),
|
|
29
33
|
);
|
|
30
34
|
}
|
|
@@ -20,7 +20,9 @@ export class ServerTime<P extends PlayerChit, R extends RootChit<P>> extends Con
|
|
|
20
20
|
this.register(
|
|
21
21
|
match.onChange(() => {
|
|
22
22
|
if (match.turn.value) {
|
|
23
|
-
this.clientTime
|
|
23
|
+
this.clientTime
|
|
24
|
+
.newMaxClock(match.turn.value.clockDetails(this.playerId))
|
|
25
|
+
.catch((e: any) => console.error("Failed to send newMaxClock to client:", e));
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
if (!this.hasSentLastActionTime) {
|
|
@@ -28,7 +30,9 @@ export class ServerTime<P extends PlayerChit, R extends RootChit<P>> extends Con
|
|
|
28
30
|
|
|
29
31
|
const player = match.turn.value?.rootChit.players.find((p) => p.id === playerId);
|
|
30
32
|
if (player) {
|
|
31
|
-
this.clientTime
|
|
33
|
+
this.clientTime
|
|
34
|
+
.setStartTime(player.promptStatus.latestPromptResponseTime)
|
|
35
|
+
.catch((e: any) => console.error("Failed to send setStartTime to client:", e));
|
|
32
36
|
}
|
|
33
37
|
}
|
|
34
38
|
}),
|
|
@@ -76,6 +76,9 @@ export function TimeControllerProvider({ children }: { children: ReactNode }) {
|
|
|
76
76
|
|
|
77
77
|
if (!clientTime || clientTime.connection !== connection) {
|
|
78
78
|
clientTime?.dispose();
|
|
79
|
+
if (clientTime?.clientPrompt) {
|
|
80
|
+
clientTime.clientPrompt.disconnect();
|
|
81
|
+
}
|
|
79
82
|
|
|
80
83
|
const newClientTime = new ClientTime(connection, game, clientTimeState);
|
|
81
84
|
connection.register(newClientTime);
|
|
@@ -26,10 +26,18 @@ class WebGLRendererWrapper {
|
|
|
26
26
|
private renderer: WebGLRenderer;
|
|
27
27
|
private composerPool = new Map<string, ComposerEntry>();
|
|
28
28
|
private memoryExtension: any;
|
|
29
|
+
private loseContextExt: WEBGL_lose_context | null = null;
|
|
29
30
|
private currentWidth = 0;
|
|
30
31
|
private currentHeight = 0;
|
|
31
32
|
public pixelRatio = Math.max(1.5, typeof window !== "undefined" ? window.devicePixelRatio : 1.5);
|
|
32
33
|
private maxComposers = 8;
|
|
34
|
+
private _contextLost = false;
|
|
35
|
+
private _restoreScheduled = false;
|
|
36
|
+
private _lastLossAt = 0;
|
|
37
|
+
private _lastRestoreAttemptAt = 0;
|
|
38
|
+
private static RESTORE_BACKOFF_MS = 250;
|
|
39
|
+
private _dirtyCallbacks = new Set<() => void>();
|
|
40
|
+
public readonly isWebGL2: boolean;
|
|
33
41
|
|
|
34
42
|
constructor() {
|
|
35
43
|
this.renderer = new WebGLRenderer({ alpha: true, antialias: true });
|
|
@@ -42,6 +50,10 @@ class WebGLRendererWrapper {
|
|
|
42
50
|
// Get memory extension for GPU memory monitoring
|
|
43
51
|
const gl = this.renderer.getContext();
|
|
44
52
|
this.memoryExtension = gl.getExtension("WEBGL_debug_renderer_info");
|
|
53
|
+
this.loseContextExt = gl.getExtension("WEBGL_lose_context");
|
|
54
|
+
this.isWebGL2 =
|
|
55
|
+
typeof WebGL2RenderingContext !== "undefined" &&
|
|
56
|
+
gl instanceof WebGL2RenderingContext;
|
|
45
57
|
|
|
46
58
|
this.renderer.setPixelRatio(this.pixelRatio);
|
|
47
59
|
this.renderer.shadowMap.enabled = true;
|
|
@@ -49,6 +61,134 @@ class WebGLRendererWrapper {
|
|
|
49
61
|
|
|
50
62
|
// Enable scissor test for partial rendering
|
|
51
63
|
this.renderer.setScissorTest(true);
|
|
64
|
+
|
|
65
|
+
// Handle WebGL context loss and restoration
|
|
66
|
+
const canvas = this.renderer.domElement;
|
|
67
|
+
canvas.addEventListener(
|
|
68
|
+
"webglcontextlost",
|
|
69
|
+
(e) => {
|
|
70
|
+
e.preventDefault(); // allows context to be restored
|
|
71
|
+
this.handleContextLost("event");
|
|
72
|
+
},
|
|
73
|
+
false,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
canvas.addEventListener(
|
|
77
|
+
"webglcontextrestored",
|
|
78
|
+
() => {
|
|
79
|
+
this.handleContextRestored();
|
|
80
|
+
},
|
|
81
|
+
false,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private handleContextLost(source: "event" | "silent") {
|
|
86
|
+
const wasLost = this._contextLost;
|
|
87
|
+
this._contextLost = true;
|
|
88
|
+
this._lastLossAt = Date.now();
|
|
89
|
+
console.warn(
|
|
90
|
+
`[WebGLRenderer] Context lost (source=${source}, alreadyLost=${wasLost}, size=${this.currentWidth}x${this.currentHeight}, composers=${this.composerPool.size})`,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// All render targets are invalid — drop them so we don't keep using stale ones
|
|
94
|
+
for (const entry of this.composerPool.values()) {
|
|
95
|
+
entry.composer.dispose();
|
|
96
|
+
entry.outlinePass.dispose();
|
|
97
|
+
}
|
|
98
|
+
this.composerPool.clear();
|
|
99
|
+
|
|
100
|
+
// If the loss was silent (no event from the browser), the matching `restored`
|
|
101
|
+
// event also won't fire on its own. Schedule a forced restore so rendering
|
|
102
|
+
// resumes without requiring the user to switch panels. The "event" path is
|
|
103
|
+
// expected to recover on its own; if it doesn't, render() will reschedule.
|
|
104
|
+
if (source === "silent") {
|
|
105
|
+
this.maybeScheduleRestore();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private handleContextRestored() {
|
|
110
|
+
const downMs = this._lastLossAt ? Date.now() - this._lastLossAt : -1;
|
|
111
|
+
console.warn(
|
|
112
|
+
`[WebGLRenderer] Context restored (downMs=${downMs}, size=${this.currentWidth}x${this.currentHeight})`,
|
|
113
|
+
);
|
|
114
|
+
this._contextLost = false;
|
|
115
|
+
this._restoreScheduled = false;
|
|
116
|
+
|
|
117
|
+
// Re-apply renderer state
|
|
118
|
+
this.renderer.setPixelRatio(this.pixelRatio);
|
|
119
|
+
this.renderer.shadowMap.enabled = true;
|
|
120
|
+
this.renderer.setClearColor(0xffffff, 1.0);
|
|
121
|
+
this.renderer.setScissorTest(true);
|
|
122
|
+
|
|
123
|
+
// Composer pool was cleared on loss; reset size tracking so the next
|
|
124
|
+
// render call rebuilds the backing framebuffer at the right dimensions.
|
|
125
|
+
this.currentWidth = 0;
|
|
126
|
+
this.currentHeight = 0;
|
|
127
|
+
|
|
128
|
+
// Notify all active viewers to re-render
|
|
129
|
+
for (const cb of this._dirtyCallbacks) {
|
|
130
|
+
cb();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Throttled scheduler for forceRestore(). Call this any time we observe the
|
|
136
|
+
* context is still lost and want to try recovery — it dedupes against an
|
|
137
|
+
* already-pending attempt and enforces a backoff so we don't spin.
|
|
138
|
+
*/
|
|
139
|
+
private maybeScheduleRestore() {
|
|
140
|
+
if (!this._contextLost) return;
|
|
141
|
+
if (this._restoreScheduled) return;
|
|
142
|
+
const sinceLastAttempt = Date.now() - this._lastRestoreAttemptAt;
|
|
143
|
+
if (this._lastRestoreAttemptAt > 0 && sinceLastAttempt < WebGLRendererWrapper.RESTORE_BACKOFF_MS) {
|
|
144
|
+
// Defer until backoff elapses
|
|
145
|
+
const wait = WebGLRendererWrapper.RESTORE_BACKOFF_MS - sinceLastAttempt;
|
|
146
|
+
this._restoreScheduled = true;
|
|
147
|
+
setTimeout(() => {
|
|
148
|
+
this._restoreScheduled = false;
|
|
149
|
+
this.forceRestore();
|
|
150
|
+
}, wait);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
this._restoreScheduled = true;
|
|
154
|
+
queueMicrotask(() => {
|
|
155
|
+
this._restoreScheduled = false;
|
|
156
|
+
this.forceRestore();
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private forceRestore() {
|
|
161
|
+
if (!this._contextLost) return;
|
|
162
|
+
this._lastRestoreAttemptAt = Date.now();
|
|
163
|
+
|
|
164
|
+
if (this.loseContextExt) {
|
|
165
|
+
try {
|
|
166
|
+
this.loseContextExt.restoreContext();
|
|
167
|
+
// If the browser honors this, `webglcontextrestored` fires asynchronously
|
|
168
|
+
// and clears _contextLost. If it doesn't fire, the next render() call
|
|
169
|
+
// will hit the _contextLost early-return and schedule another attempt.
|
|
170
|
+
return;
|
|
171
|
+
} catch (e) {
|
|
172
|
+
console.warn("[WebGLRenderer] restoreContext() threw, falling back to setSize", e);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Fallback: nudge the canvas size to encourage the driver to rebuild the context.
|
|
176
|
+
// Some Android drivers fire `webglcontextrestored` in response to setSize on a
|
|
177
|
+
// lost context. If neither path triggers a restore, render() reschedules us.
|
|
178
|
+
try {
|
|
179
|
+
this.renderer.setSize(Math.max(1, this.currentWidth), Math.max(1, this.currentHeight));
|
|
180
|
+
} catch (e) {
|
|
181
|
+
console.warn("[WebGLRenderer] setSize fallback threw", e);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
get contextLost() {
|
|
186
|
+
return this._contextLost;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
onDirty(cb: () => void): () => void {
|
|
190
|
+
this._dirtyCallbacks.add(cb);
|
|
191
|
+
return () => this._dirtyCallbacks.delete(cb);
|
|
52
192
|
}
|
|
53
193
|
|
|
54
194
|
private evictLeastRecentlyUsedComposer() {
|
|
@@ -124,15 +264,79 @@ class WebGLRendererWrapper {
|
|
|
124
264
|
return entry;
|
|
125
265
|
}
|
|
126
266
|
|
|
127
|
-
private
|
|
267
|
+
private _shrinkTimer?: ReturnType<typeof setTimeout>;
|
|
268
|
+
private _activeRenderSizes = new Map<string, { width: number; height: number }>();
|
|
269
|
+
|
|
270
|
+
private ensureRendererSize(targetWidth: number, targetHeight: number, viewerId?: string) {
|
|
271
|
+
if (viewerId) {
|
|
272
|
+
this._activeRenderSizes.set(viewerId, { width: targetWidth, height: targetHeight });
|
|
273
|
+
}
|
|
274
|
+
|
|
128
275
|
if (targetWidth > this.currentWidth || targetHeight > this.currentHeight) {
|
|
276
|
+
const prevWidth = this.currentWidth;
|
|
277
|
+
const prevHeight = this.currentHeight;
|
|
129
278
|
this.currentWidth = Math.max(this.currentWidth, targetWidth);
|
|
130
279
|
this.currentHeight = Math.max(this.currentHeight, targetHeight);
|
|
131
280
|
this.renderer.setSize(this.currentWidth, this.currentHeight);
|
|
281
|
+
console.log(
|
|
282
|
+
`[WebGLRenderer] Renderer size grew: ${prevWidth}x${prevHeight} -> ${this.currentWidth}x${this.currentHeight} (requested ${targetWidth}x${targetHeight})`,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Remove a viewer from the active set and schedule a shrink check. */
|
|
288
|
+
unregisterViewerSize(viewerId: string) {
|
|
289
|
+
this._activeRenderSizes.delete(viewerId);
|
|
290
|
+
this.scheduleShrink();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private scheduleShrink() {
|
|
294
|
+
if (this._shrinkTimer) return;
|
|
295
|
+
this._shrinkTimer = setTimeout(() => {
|
|
296
|
+
this._shrinkTimer = undefined;
|
|
297
|
+
this.shrinkToFit();
|
|
298
|
+
}, 2000);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private shrinkToFit() {
|
|
302
|
+
let maxW = 0;
|
|
303
|
+
let maxH = 0;
|
|
304
|
+
for (const size of this._activeRenderSizes.values()) {
|
|
305
|
+
maxW = Math.max(maxW, size.width);
|
|
306
|
+
maxH = Math.max(maxH, size.height);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Only shrink if we can save at least 25% in one dimension
|
|
310
|
+
if (maxW > 0 && maxH > 0 && (maxW < this.currentWidth * 0.75 || maxH < this.currentHeight * 0.75)) {
|
|
311
|
+
const prevWidth = this.currentWidth;
|
|
312
|
+
const prevHeight = this.currentHeight;
|
|
313
|
+
this.currentWidth = maxW;
|
|
314
|
+
this.currentHeight = maxH;
|
|
315
|
+
this.renderer.setSize(this.currentWidth, this.currentHeight);
|
|
316
|
+
console.log(
|
|
317
|
+
`[WebGLRenderer] Renderer size shrunk: ${prevWidth}x${prevHeight} -> ${this.currentWidth}x${this.currentHeight}`,
|
|
318
|
+
);
|
|
132
319
|
}
|
|
133
320
|
}
|
|
134
321
|
|
|
135
|
-
render(sceneWrapper: SceneWrapper, camera: Camera, context2d: CanvasRenderingContext2D, theme: GameTheme) {
|
|
322
|
+
render(sceneWrapper: SceneWrapper, camera: Camera, context2d: CanvasRenderingContext2D, theme: GameTheme, viewerId?: string): boolean {
|
|
323
|
+
if (this._contextLost) {
|
|
324
|
+
// Still lost — keep trying to restore. The previous attempt may have used
|
|
325
|
+
// the setSize fallback that doesn't always trigger `webglcontextrestored`.
|
|
326
|
+
this.maybeScheduleRestore();
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Detect silent context loss — on some Android WebViews the `webglcontextlost`
|
|
331
|
+
// event isn't fired reliably for GPU process kills. Without this check we'd
|
|
332
|
+
// happily call composer.render() on a dead context, produce no pixels, and
|
|
333
|
+
// strip the snapshot overlay → blank canvas until the user switches panels.
|
|
334
|
+
const gl = this.renderer.getContext();
|
|
335
|
+
if (gl && gl.isContextLost()) {
|
|
336
|
+
this.handleContextLost("silent");
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
|
|
136
340
|
const canvas = context2d.canvas;
|
|
137
341
|
const width = Math.floor(canvas.width / this.pixelRatio);
|
|
138
342
|
const height = Math.floor(canvas.height / this.pixelRatio);
|
|
@@ -140,7 +344,7 @@ class WebGLRendererWrapper {
|
|
|
140
344
|
const targetHeight = Math.ceil(height * this.pixelRatio);
|
|
141
345
|
|
|
142
346
|
// Ensure renderer can accommodate this size
|
|
143
|
-
this.ensureRendererSize(width, height);
|
|
347
|
+
this.ensureRendererSize(width, height, viewerId);
|
|
144
348
|
|
|
145
349
|
// Get or create appropriate composer
|
|
146
350
|
const transparent = true; // Always use transparent for composition
|
|
@@ -158,8 +362,6 @@ class WebGLRendererWrapper {
|
|
|
158
362
|
|
|
159
363
|
// Copy the rendered result to the 2D canvas
|
|
160
364
|
const webglCanvas = this.renderer.domElement;
|
|
161
|
-
// context2d.save();
|
|
162
|
-
// context2d.scale(1 / this.pixelRatio, 1 / this.pixelRatio);
|
|
163
365
|
context2d.drawImage(
|
|
164
366
|
webglCanvas,
|
|
165
367
|
0,
|
|
@@ -171,7 +373,7 @@ class WebGLRendererWrapper {
|
|
|
171
373
|
targetWidth,
|
|
172
374
|
targetHeight, // Dest width, height
|
|
173
375
|
);
|
|
174
|
-
|
|
376
|
+
return true;
|
|
175
377
|
} finally {
|
|
176
378
|
entry.referenceCount--;
|
|
177
379
|
}
|
|
@@ -225,3 +427,7 @@ export function useWebGlRenderer(): WebGLRendererWrapper {
|
|
|
225
427
|
}
|
|
226
428
|
return _wrapper;
|
|
227
429
|
}
|
|
430
|
+
|
|
431
|
+
export function getWebGlRendererInstance(): WebGLRendererWrapper | undefined {
|
|
432
|
+
return _wrapper;
|
|
433
|
+
}
|
package/src/library/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ export { Chit } from "./game/Chit";
|
|
|
10
10
|
/** @group Core Game Elements */
|
|
11
11
|
export { Turn } from "./game/Turn";
|
|
12
12
|
/** @group Core Game Elements */
|
|
13
|
-
export { Pick, ButtonPick, ChitPick } from "./game/Pick";
|
|
13
|
+
export { Pick, ButtonPick, ChitPick, DragTarget, DragPick } from "./game/Pick";
|
|
14
14
|
/** @group Core Game Elements */
|
|
15
15
|
export { GameTheme } from "./game/GameTheme";
|
|
16
16
|
/** @group Chits */
|
|
@@ -37,7 +37,7 @@ export class ChitGalleryItemInstance implements GalleryItem {
|
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
// handle refreshes.
|
|
40
|
-
this.unsubscribe = chit.onChange("deserialized parent onClick", () => {
|
|
40
|
+
this.unsubscribe = chit.onChange("deserialized parent onClick renderInstance", () => {
|
|
41
41
|
if (chit.renderInstance) {
|
|
42
42
|
chit.renderInstance.createGalleryItem(this);
|
|
43
43
|
}
|
|
@@ -18,7 +18,6 @@ import { Easing, Tween, Group as TweenGroup } from "@tweenjs/tween.js";
|
|
|
18
18
|
import { RootChitRenderInstance } from "./RootChitRenderInstance";
|
|
19
19
|
import { fixBbox } from "../utilities/BboxUtils";
|
|
20
20
|
import { ChitGalleryItemInstance } from "./ChitGalleryItemInstance";
|
|
21
|
-
import Color from "color";
|
|
22
21
|
|
|
23
22
|
const LINE_COLOR = new MeshBasicMaterial({ color: 0xff0000, wireframe: true, wireframeLinewidth: 2 });
|
|
24
23
|
const CLICK_LINE_COLOR = new MeshBasicMaterial({ color: 0xffff00, wireframe: true, wireframeLinewidth: 2 });
|
|
@@ -128,8 +127,10 @@ export class ChitRenderInstance {
|
|
|
128
127
|
let intersection = this.attemptToFindPlaneZ0(this.rootRenderInstance, positionEntranceChit);
|
|
129
128
|
|
|
130
129
|
if (intersection && oldParent) {
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
if (positionEntranceChit && positionEntranceChit !== chit.parentFallback) {
|
|
131
|
+
// Ensure entrance coordinates are outside visible region if they overlap
|
|
132
|
+
intersection = this.ensureCoordinatesOutsideVisibleRegion(intersection);
|
|
133
|
+
}
|
|
133
134
|
|
|
134
135
|
this.group.removeFromParent();
|
|
135
136
|
this.group.position.set(intersection.x, intersection.y, intersection.z);
|
|
@@ -139,6 +140,7 @@ export class ChitRenderInstance {
|
|
|
139
140
|
|
|
140
141
|
this.bboxGroup.add(this.bbox);
|
|
141
142
|
this.bboxGroup.add(this.clickbox);
|
|
143
|
+
this.chit.notifyChange("renderInstance");
|
|
142
144
|
}
|
|
143
145
|
|
|
144
146
|
public get effectiveParent() {
|
|
@@ -370,7 +372,7 @@ export class ChitRenderInstance {
|
|
|
370
372
|
|
|
371
373
|
protected updateGroupPosition(key: OwnerOriginPosition | string, group: Group) {
|
|
372
374
|
const vector = this.targetAnchorPoint(key);
|
|
373
|
-
group.position.set(vector.x, vector.y, vector.z + this.sizeZ / 2 + (this.renderSpec?.childrenOffsetZ ?? 0));
|
|
375
|
+
group.position.set(vector.x, vector.y, vector.z + this.sizeZ / 2 + this.centerZ + (this.renderSpec?.childrenOffsetZ ?? 0));
|
|
374
376
|
}
|
|
375
377
|
|
|
376
378
|
protected setOutletPosition(positionKey: string, position: Vector3) {
|
|
@@ -662,8 +664,7 @@ export class ChitRenderInstance {
|
|
|
662
664
|
|
|
663
665
|
if (outlineContext && this.renderSpec.object && outlineContext.renderSpec?.object && c) {
|
|
664
666
|
const id = outlineContext.renderSpec.object.id % 60000;
|
|
665
|
-
const
|
|
666
|
-
const threeColor = new ThreeColor(color.red() / 256, color.green() / 256, color.blue() / 256);
|
|
667
|
+
const threeColor = new ThreeColor(c);
|
|
667
668
|
this.renderSpec.object.traverseVisible((o) => {
|
|
668
669
|
o.userData.outlineId = id;
|
|
669
670
|
o.userData.outlineColor = threeColor;
|
|
@@ -884,7 +885,7 @@ export class ChitRenderInstance {
|
|
|
884
885
|
const targetOffset = {
|
|
885
886
|
x: this.renderSpec.offsetX,
|
|
886
887
|
y: this.renderSpec.offsetY,
|
|
887
|
-
z: this.renderSpec.offsetZ + this.sizeZ / 2,
|
|
888
|
+
z: this.renderSpec.offsetZ + this.sizeZ / 2 - this.centerZ,
|
|
888
889
|
};
|
|
889
890
|
this.handleOffsetForSplay(targetOffset);
|
|
890
891
|
|
|
@@ -1053,7 +1054,7 @@ export class ChitRenderInstance {
|
|
|
1053
1054
|
const targetOffset = {
|
|
1054
1055
|
x: this.renderSpec.offsetX,
|
|
1055
1056
|
y: this.renderSpec.offsetY,
|
|
1056
|
-
z: this.renderSpec.offsetZ + this.sizeZ / 2,
|
|
1057
|
+
z: this.renderSpec.offsetZ + this.sizeZ / 2 - this.centerZ,
|
|
1057
1058
|
};
|
|
1058
1059
|
const targetRotation = { x: this.renderSpec.rotateX, y: this.renderSpec.rotateY, z: this.renderSpec.rotateZ };
|
|
1059
1060
|
|
|
@@ -1143,7 +1144,29 @@ export class ChitRenderInstance {
|
|
|
1143
1144
|
|
|
1144
1145
|
if (rotationEasing) {
|
|
1145
1146
|
rotation.order = "ZYX";
|
|
1146
|
-
|
|
1147
|
+
|
|
1148
|
+
// Adjust current angles so each axis takes the shortest path to the target.
|
|
1149
|
+
// e.g. 355° → 5° becomes -5° → 5° (10° rotation) instead of 355° → 5° (350° rotation).
|
|
1150
|
+
// Adding/subtracting full turns doesn't change the visual orientation.
|
|
1151
|
+
const adjustForShortestPath = (current: number, target: number) => {
|
|
1152
|
+
let diff = target - current;
|
|
1153
|
+
while (diff > Math.PI) diff -= 2 * Math.PI;
|
|
1154
|
+
while (diff < -Math.PI) diff += 2 * Math.PI;
|
|
1155
|
+
return target - diff;
|
|
1156
|
+
};
|
|
1157
|
+
|
|
1158
|
+
const adjustedStart = {
|
|
1159
|
+
x: adjustForShortestPath(rotation.x, targetRotation.x),
|
|
1160
|
+
y: adjustForShortestPath(rotation.y, targetRotation.y),
|
|
1161
|
+
z: adjustForShortestPath(rotation.z, targetRotation.z),
|
|
1162
|
+
};
|
|
1163
|
+
|
|
1164
|
+
// Snap the group rotation to the adjusted values (visually identical)
|
|
1165
|
+
rotation.x = adjustedStart.x;
|
|
1166
|
+
rotation.y = adjustedStart.y;
|
|
1167
|
+
rotation.z = adjustedStart.z;
|
|
1168
|
+
|
|
1169
|
+
this.rotationTween = this.createTween({ x: adjustedStart.x, y: adjustedStart.y, z: adjustedStart.z }, (tween) =>
|
|
1147
1170
|
tween
|
|
1148
1171
|
.to(targetRotation, duration * this.animationSpeedMultiplier)
|
|
1149
1172
|
.onUpdate((obj) => {
|
|
@@ -210,7 +210,7 @@ export class ChitRenderSpec {
|
|
|
210
210
|
}
|
|
211
211
|
|
|
212
212
|
const p = this.outletPositions[ordered.outletName] ?? new Vector3(0, 0, 0);
|
|
213
|
-
mesh.position.set(p.x + offsetX, p.y + offsetY, box3.max.z
|
|
213
|
+
mesh.position.set(p.x + offsetX, p.y + offsetY, box3.max.z + p.z + 0.001);
|
|
214
214
|
this.ornaments.push(mesh);
|
|
215
215
|
}
|
|
216
216
|
}
|
|
@@ -429,22 +429,26 @@ export class IDBasedOutlinePass extends Pass {
|
|
|
429
429
|
(this.copyUniforms["tDiffuse"].value as any) = this.renderTargetTempBuffer.texture;
|
|
430
430
|
this.fsQuad.render(renderer);
|
|
431
431
|
|
|
432
|
-
// Second:
|
|
432
|
+
// Second: Composite edges using premultiplied alpha blending
|
|
433
|
+
// Edge texture stores premultiplied alpha: (rgb * a, a)
|
|
433
434
|
renderer.autoClear = false;
|
|
434
435
|
|
|
435
436
|
const originalBlending = this.materialCopy.blending;
|
|
436
437
|
const originalTransparent = this.materialCopy.transparent;
|
|
438
|
+
const originalPremultipliedAlpha = this.materialCopy.premultipliedAlpha;
|
|
437
439
|
|
|
438
440
|
this.materialCopy.blending = NormalBlending;
|
|
439
441
|
this.materialCopy.transparent = true;
|
|
442
|
+
this.materialCopy.premultipliedAlpha = true;
|
|
440
443
|
this.materialCopy.needsUpdate = true;
|
|
441
444
|
|
|
442
445
|
(this.copyUniforms["tDiffuse"].value as any) = edgeTexture;
|
|
443
446
|
this.fsQuad.render(renderer);
|
|
444
447
|
|
|
445
|
-
//
|
|
448
|
+
// Restore material blending settings
|
|
446
449
|
this.materialCopy.blending = originalBlending;
|
|
447
450
|
this.materialCopy.transparent = originalTransparent;
|
|
451
|
+
this.materialCopy.premultipliedAlpha = originalPremultipliedAlpha;
|
|
448
452
|
this.materialCopy.needsUpdate = true;
|
|
449
453
|
|
|
450
454
|
this.restoreRenderState(renderer);
|
|
@@ -221,12 +221,16 @@ export class InterMeshEdgeDetectionPass extends Pass {
|
|
|
221
221
|
|
|
222
222
|
// Only draw if we're near an edge (inside the mesh)
|
|
223
223
|
if (edgeDistance < 1000.0) {
|
|
224
|
-
// Create thick outline with
|
|
224
|
+
// Create thick outline with solid core and soft outer edge
|
|
225
225
|
float lineThickness = thickness + 0.5;
|
|
226
|
-
float
|
|
226
|
+
float feather = min(1.5, lineThickness * 0.5);
|
|
227
|
+
float solidEnd = lineThickness - feather;
|
|
228
|
+
float alpha = 1.0 - smoothstep(solidEnd, lineThickness, edgeDistance);
|
|
227
229
|
|
|
228
230
|
if (alpha > 0.01) {
|
|
229
|
-
|
|
231
|
+
// Output premultiplied alpha for correct blending with LinearFilter
|
|
232
|
+
float finalAlpha = alpha * strength;
|
|
233
|
+
gl_FragColor = vec4(centerOutlineColor.rgb * finalAlpha, finalAlpha);
|
|
230
234
|
} else {
|
|
231
235
|
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
|
|
232
236
|
}
|
|
@@ -455,9 +455,14 @@ export class ImageCanvasOperation extends CanvasOperation {
|
|
|
455
455
|
if (maxH && syi + shi > maxH) {
|
|
456
456
|
shi = Math.max(0, maxH - syi);
|
|
457
457
|
}
|
|
458
|
-
//
|
|
459
|
-
|
|
460
|
-
|
|
458
|
+
// Inset by half a texel and disable smoothing so bilinear filtering
|
|
459
|
+
// never samples neighboring sprites in the spritesheet.
|
|
460
|
+
const inset = 0.5;
|
|
461
|
+
if (swi > inset * 2 && shi > inset * 2) {
|
|
462
|
+
const prevSmoothing = context.imageSmoothingEnabled;
|
|
463
|
+
context.imageSmoothingEnabled = false;
|
|
464
|
+
context.drawImage(source as any, sxi + inset, syi + inset, swi - inset * 2, shi - inset * 2, x, y, w, h);
|
|
465
|
+
context.imageSmoothingEnabled = prevSmoothing;
|
|
461
466
|
}
|
|
462
467
|
}
|
|
463
468
|
}
|
|
@@ -517,24 +522,50 @@ export class ImageCanvasOperation extends CanvasOperation {
|
|
|
517
522
|
let syiF = Math.floor(sy);
|
|
518
523
|
let swiF = Math.ceil(sw);
|
|
519
524
|
let shiF = Math.ceil(sh);
|
|
520
|
-
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
+
// Clamp to sprite bounds so rounding never bleeds into neighboring sprites.
|
|
526
|
+
const sbRight = sourceBounds.x + sourceBounds.width;
|
|
527
|
+
const sbBottom = sourceBounds.y + sourceBounds.height;
|
|
528
|
+
if (sxiF < sourceBounds.x) {
|
|
529
|
+
swiF -= sourceBounds.x - sxiF;
|
|
530
|
+
sxiF = sourceBounds.x;
|
|
531
|
+
}
|
|
532
|
+
if (syiF < sourceBounds.y) {
|
|
533
|
+
shiF -= sourceBounds.y - syiF;
|
|
534
|
+
syiF = sourceBounds.y;
|
|
525
535
|
}
|
|
526
|
-
if (
|
|
527
|
-
|
|
528
|
-
syiF = 0;
|
|
536
|
+
if (sxiF + swiF > sbRight) {
|
|
537
|
+
swiF = sbRight - sxiF;
|
|
529
538
|
}
|
|
539
|
+
if (syiF + shiF > sbBottom) {
|
|
540
|
+
shiF = sbBottom - syiF;
|
|
541
|
+
}
|
|
542
|
+
// Also clamp to overall image dimensions.
|
|
543
|
+
const maxWF = (source as any).width ?? (source as any).naturalWidth ?? 0;
|
|
544
|
+
const maxHF = (source as any).height ?? (source as any).naturalHeight ?? 0;
|
|
530
545
|
if (maxWF && sxiF + swiF > maxWF) {
|
|
531
546
|
swiF = Math.max(0, maxWF - sxiF);
|
|
532
547
|
}
|
|
533
548
|
if (maxHF && syiF + shiF > maxHF) {
|
|
534
549
|
shiF = Math.max(0, maxHF - syiF);
|
|
535
550
|
}
|
|
536
|
-
|
|
537
|
-
|
|
551
|
+
// Inset by half a texel and disable smoothing so bilinear filtering
|
|
552
|
+
// never samples neighboring sprites in the spritesheet.
|
|
553
|
+
const inset = 0.5;
|
|
554
|
+
if (swiF > inset * 2 && shiF > inset * 2) {
|
|
555
|
+
const prevSmoothing = context.imageSmoothingEnabled;
|
|
556
|
+
context.imageSmoothingEnabled = false;
|
|
557
|
+
context.drawImage(
|
|
558
|
+
source as any,
|
|
559
|
+
sxiF + inset,
|
|
560
|
+
syiF + inset,
|
|
561
|
+
swiF - inset * 2,
|
|
562
|
+
shiF - inset * 2,
|
|
563
|
+
x,
|
|
564
|
+
y,
|
|
565
|
+
w,
|
|
566
|
+
h,
|
|
567
|
+
);
|
|
568
|
+
context.imageSmoothingEnabled = prevSmoothing;
|
|
538
569
|
}
|
|
539
570
|
}
|
|
540
571
|
}
|