Skip to content

Commit 5287752

Browse files
author
linyuan.yang
committed
fix(player): 二次加载 ROM 画面变小并下移
1 parent 9b6b5f1 commit 5287752

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎packages/player/src/emulator/runner.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,36 @@ export class NesRunner {
103103
// 稳定映射成幂等的目标状态,避免重复发命令切反。
104104
private speedMode: SpeedMode = 'normal'
105105
private muted = false
106+
// 保存 canvas 初始状态,以便每次 destroy 后恢复。
107+
// Nostalgist launch 时会:
108+
// 1) 给 canvas 写 inline appearance 样式(backgroundColor / imageRendering);
109+
// 2) 劫持 canvas.style.removeProperty,拦住 width/height 不让删;
110+
// 3) Emscripten runtime 会把 canvas 的 CSS 尺寸写死为内部分辨率(256x240)。
111+
// 二次 launch 时这些遮盖 → canvas 变成 256x240 硬尺寸,跨出 frame 又位移。
112+
private readonly originalCssText: string
113+
private readonly originalRemoveProperty: (property: string) => string
106114

107115
constructor(canvas: HTMLCanvasElement) {
108116
this.canvas = canvas
117+
this.originalCssText = canvas.style.cssText
118+
this.originalRemoveProperty = canvas.style.removeProperty.bind(canvas.style)
109119
// 设定内部分辨率;实际显示尺寸由 NesScreen 的 CSS 盒子控制,Nostalgist size:'auto'
110120
// 会按元素尺寸自适应,不覆盖这里的宽高比语义。
111121
canvas.width = WIDTH
112122
canvas.height = HEIGHT
113123
}
114124

125+
/** 把 canvas 恢复到 launch 前的状态,清理 Nostalgist / Emscripten 遗留的 inline 样式与方法劫持。 */
126+
private restoreCanvas(): void {
127+
// 先把被劫持的 removeProperty 换回原生,下面 cssText 重置才不会丢掉 width/height。
128+
this.canvas.style.removeProperty = this.originalRemoveProperty
129+
// 一句把所有 inline 样式回到初始值(空字符串也能用,相当于清空)。
130+
this.canvas.style.cssText = this.originalCssText
131+
// 恢复内部分辨率属性(不是样式,是 canvas 自己的 width/height 属性)。
132+
this.canvas.width = WIDTH
133+
this.canvas.height = HEIGHT
134+
}
135+
115136
/** 载入并启动一个 ROM。 */
116137
async loadRom(bytes: Uint8Array): Promise<void> {
117138
await this.destroy()
@@ -146,6 +167,9 @@ export class NesRunner {
146167
console.warn('[NES] 退出核心实例出错', err)
147168
}
148169
this.instance = null
170+
// 清理 Nostalgist / Emscripten 写入的 inline 样式与方法劫持,不然二次 launch 时
171+
// canvas 会带着硬尺寸 256x240,导致画面变小并往下堆。
172+
this.restoreCanvas()
149173
}
150174
this.running = false
151175
this.speedMode = 'normal'

0 commit comments

Comments
 (0)