-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox.html
More file actions
95 lines (86 loc) · 2.63 KB
/
Copy pathbox.html
File metadata and controls
95 lines (86 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>WebAssembly Wasm Module Runner</title>
<style>
body {
font-family: sans-serif;
text-align: center;
background: #000;
color: #fff;
}
#canvas {
display: none;
margin: 20px auto;
background-color: black;
}
#loading {
display: block;
margin: 40px auto;
}
</style>
</head>
<body>
<!-- 加载动画 -->
<img id="loading" src="loading.gif" alt="Loading..." />
<!-- Canvas 元素用于渲染 Wasm 内容 -->
<canvas id="canvas" width="800" height="600"></canvas>
<!-- 状态提示 -->
<p id="status" style="display: none;">Loading...</p>
<script>
// 设置 WebAssembly 模块名称
const moduleBaseName = 'powderdev';
// 动态加载 .js 胶水文件
function loadWasmScript() {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = `./${moduleBaseName}.js`; // 注意路径(可改为 ./wasm/powderdev.js)
script.onload = () => {
if (typeof window[`create_${moduleBaseName}`] === 'function') {
resolve(window[`create_${moduleBaseName}`]);
} else {
reject(new Error(`Failed to find 'create_${moduleBaseName}' in loaded script`));
}
};
script.onerror = () => {
reject(new Error('Failed to load Wasm script.'));
};
document.head.appendChild(script);
});
}
// 获取 DOM 元素
const canvas = document.getElementById('canvas');
const status = document.getElementById('status');
const loading = document.getElementById('loading');
// 标记内容可以显示
window.mark_presentable = function () {
loading.style.display = 'none';
canvas.style.display = 'block';
};
// 错误处理
window.onerror = (event) => {
status.innerText = '出现异常,请查看控制台输出。';
status.style.display = 'block';
return true; // 抑制浏览器默认错误提示
};
// 初始化 WebAssembly 模块
loadWasmScript().then(createModule => {
createModule({
canvas,
print: console.log,
printErr: console.error
});
}).catch(err => {
console.error(err);
status.innerText = '加载失败,请检查网络请求和路径。';
status.style.display = 'block';
});
// 监听 WebGL 上下文丢失
canvas.addEventListener("webglcontextlost", (e) => {
alert('WebGL 上下文丢失,请刷新页面。');
e.preventDefault();
}, false);
</script>
</body>
</html>