Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

移动功能补全及TypeScript支持 #3

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"picFolder": ["resources/images/", "resources/images1/"],
"canvasElementID": "canvas",
"camera": {
"FOV": 75,
"position": {
Expand All @@ -18,5 +17,6 @@
"maxZ": -0.5
},
"modelURL": "../Deprecated/static/model/model.glb",
"divElementID": "cav"
"divElementID": "cav",
"moveDistance": 0.05
}
7 changes: 1 addition & 6 deletions examples/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
<title>Document</title>
</head>
<body>
<div id="cav" style="width: 100vw; height: 100vh; margin: 0; padding: 0">
<canvas id="canvas" style="width: 100%; height: 100%">
Your browser does not support the HTML5 canvas element.
</canvas>
</div>

<ext id="cav" style="width: 100vw; height: 100vh; margin: 0; padding: 0"></ext>
<script type="module">
import * as bundle from "../build/bundle.js";
var exhition = new bundle.$Exhibition("/examples/config.json");
Expand Down
43 changes: 32 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"scripts": {
"check": "eslint . && prettier --check .",
"fix": "eslint . --fix && prettier --write .",
"build": "rollup -c"
"build": "rollup -c && npx -p typescript tsc",
"build:types": "npx -p typescript tsc",
"build:rollup": "rollup -c"
},
"repository": {
"type": "git",
Expand All @@ -23,16 +25,17 @@
"devDependencies": {
"@eslint/js": "^9.10.0",
"@lopatnov/rollup-plugin-uglify": "^2.1.5",
"@types/three": "^0.169.0",
"eslint": "^9.10.0",
"eslint-plugin-prettier": "^5.2.1",
"globals": "^15.9.0",
"rollup": "^4.21.3",
"rollup-plugin-javascript-obfuscator": "^1.0.4",
"terser": "^5.33.0"
"terser": "^5.33.0",
"typescript": "^5.6.2"
},
"dependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/three": "^0.168.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.3.3",
"rollup-plugin-node-externals": "^7.1.3",
Expand Down
Empty file added src/handlers/pic_handler.js
Empty file.
48 changes: 46 additions & 2 deletions src/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class $Exhibition {
this.stats.update();
requestAnimationFrame(this.$animate);
};
this.keypressed;
}

init() {
Expand All @@ -49,6 +50,7 @@ class $Exhibition {
this.loadScence()
.then(_ => {
this.$startAnimate();
this.$startMotionCheck();
console.log("加载场景成功!");
})
.catch(error => {
Expand Down Expand Up @@ -76,6 +78,43 @@ class $Exhibition {
});
}

$startMotionCheck() {
this.keypressed = { KeyW: 0, KeyS: 0, KeyA: 0, KeyD: 0 };
let that = this;
document.addEventListener("keydown", function (event) {
if (event.code == "KeyW" || event.code == "KeyS" || event.code == "KeyA" || event.code == "KeyD") {
that.keypressed[event.code] = 1;
}
});

document.addEventListener("keyup", event => {
if (event.code == "KeyW" || event.code == "KeyS" || event.code == "KeyA" || event.code == "KeyD") {
that.keypressed[event.code] = 0;
}
});

this.movdis = this.config.getValue("moveDistance");

this.motionCheck = setInterval(() => {
if (this.keypressed.KeyW) {
that.controls.moveForward(this.movdis);
that.$checkBoundaries();
}
if (this.keypressed.KeyA) {
that.controls.moveRight(-this.movdis);
that.$checkBoundaries();
}
if (this.keypressed.KeyS) {
that.controls.moveForward(-this.movdis);
that.$checkBoundaries();
}
if (this.keypressed.KeyD) {
that.controls.moveRight(this.movdis);
that.$checkBoundaries();
}
}, 10);
}

/**
* @name isMobileUserAgent
* @description 检查是否是移动端用户代理
Expand Down Expand Up @@ -165,7 +204,12 @@ class $Exhibition {
return new Promise((resolve, reject) => {
try {
this.scene = new THREE.Scene();
this.canvasElement = document.getElementById(this.config.getValue("canvasElementID"));
this.divEle = document.getElementById(this.config.getValue("divElementID"));
this.canvasElement = document.createElement("canvas");
this.canvasElement.style.width = "100%";
this.canvasElement.style.height = "100%";
// this.canvasElement.style.margin = "0";
this.divEle.appendChild(this.canvasElement);
this.camera = new THREE.PerspectiveCamera(
this.config.getValue("camera")["FOV"],
this.canvasElement.clientWidth / this.canvasElement.clientHeight,
Expand Down Expand Up @@ -221,7 +265,7 @@ class $Exhibition {
directLigt.position.set(0, 100, 0);
this.scene.add(directLigt);
this.stats = new Stats();
document.getElementById("cav").appendChild(this.stats.domElement);
this.divEle.appendChild(this.stats.domElement);
resolve(null);
} catch (error) {
reject(error);
Expand Down
24 changes: 24 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// 将此更改以匹配你的项目
"include": ["src/**/*"],
"compilerOptions": {
// 告诉 TypeScript 读取 JS 文件,因为
// 通常它们被忽略为源文件
"allowJs": true,
// 生成 d.ts 文件
"declaration": true,
// 此编译器运行应
// 只输出 d.ts 文件
"emitDeclarationOnly": true,
// 类型应该进入此目录。
// 删除它会将 .d.ts 文件放置在
// 与 .js 文件相邻的位置
"outDir": "build/types",
// 在使用 IDE 功能时转到 js 文件,如
// VSCode 中的"转到定义"
"declarationMap": true,
"lib": ["es6", "dom", "ESNext"],
"moduleResolution": "Bundler",
"module": "ESNext"
}
}
Loading