状态录制
上一章回顾: 改变视角
- 你了解什么是 State, 以及如何获取和修改。
- 通过 State 完成了自动环视的功能。
本章你可以学习到
- 通过 State 完成用户操作的录制。
- 通过 State 还原用户操作画面。
准备工作
和上一章节一样,我们新建一个目录(src/3.recording-state
)以及对应的 html 文件 以及 jsx 或 tsx 文件。
js 或 ts 文件可以先拷贝上一章节的内容。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>改变视角 | Knowing state</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css"
rel="stylesheet"
crossorigin="anonymous"
/>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css"
rel="stylesheet"
/>
<style>
html,
body,
#app {
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<div id="app"></div>
<!-- 模式切换 -->
<nav class="navbar fixed-bottom navbar-light bg-light">
<div class="container-fluid justify-content-center">
<div class="btn-group">
<button class="btn btn-primary active js-Panorama">全景漫游</button>
<button class="btn btn-primary js-Floorplan">空间总览</button>
</div>
</div>
</nav>
<!-- 环视 -->
<div class="card position-fixed m-2 top-0 end-0">
<button class="btn btn-light js-lookAround-start">
<i class="bi bi-arrow-repeat"></i>
</button>
<button class="btn btn-light js-lookAround-stop d-none">
<i class="bi bi-pause"></i>
</button>
</div>
<script type="module" src="./index"></script>
</body>
</html>
- JavaScript
- TypeScript
import { Five, parseWork } from "@realsee/five";
const workURL =
"https://vr-public.realsee-cdn.cn/release/static/image/release/five/work-sample/07bdc58f413bc5494f05c7cbb5cbdce4/work.json";
const five = new Five();
five.appendTo(document.querySelector("#app"));
fetch(workURL)
.then((res) => res.json())
.then((json) => {
const work = parseWork(json);
five.load(work);
});
window.addEventListener("resize", () => five.refresh(), false);
{
// === 模式切换 ===
const buttons = {
Panorama: document.querySelector(".js-Panorama"),
Floorplan: document.querySelector(".js-Floorplan"),
};
for (const [modeName, element] of Object.entries(buttons)) {
element.addEventListener(
"click",
() => {
five.setState({ mode: modeName });
},
false
);
}
five.on("stateChange", (state) => {
for (const [modeName, element] of Object.entries(buttons)) {
if (modeName === state.mode) {
element.classList.add("active");
} else {
element.classList.remove("active");
}
}
});
}
{
// === 环视 ===
let timer;
const startButton = document.querySelector(".js-lookAround-start");
const stopButton = document.querySelector(".js-lookAround-stop");
startButton.addEventListener(
"click",
() => {
window.clearInterval(timer);
timer = window.setInterval(() => {
five.setState({ longitude: five.state.longitude + Math.PI / 360 });
}, 16);
startButton.classList.add("d-none");
stopButton.classList.remove("d-none");
},
false
);
stopButton.addEventListener(
"click",
() => {
window.clearInterval(timer);
startButton.classList.remove("d-none");
stopButton.classList.add("d-none");
},
false
);
}
export {};
import { Five, Mode, parseWork } from "@realsee/five";
const workURL =
"https://vr-public.realsee-cdn.cn/release/static/image/release/five/work-sample/07bdc58f413bc5494f05c7cbb5cbdce4/work.json";
const five = new Five();
five.appendTo(document.querySelector("#app")!);
fetch(workURL)
.then((res) => res.json())
.then((json) => {
const work = parseWork(json);
five.load(work);
});
window.addEventListener("resize", () => five.refresh(), false);
{
// === 模式切换 ===
const buttons: Partial<Record<Mode, Element>> = {
Panorama: document.querySelector(".js-Panorama")!,
Floorplan: document.querySelector(".js-Floorplan")!,
};
for (const [modeName, element] of Object.entries(buttons)) {
element.addEventListener(
"click",
() => {
five.setState({ mode: modeName as Mode });
},
false
);
}
five.on("stateChange", (state) => {
for (const [modeName, element] of Object.entries(buttons)) {
if (modeName === state.mode) {
element.classList.add("active");
} else {
element.classList.remove("active");
}
}
});
}
{
// === 环视 ===
let timer: number | undefined;
const startButton = document.querySelector(".js-lookAround-start")!;
const stopButton = document.querySelector(".js-lookAround-stop")!;
startButton.addEventListener(
"click",
() => {
window.clearInterval(timer);
timer = window.setInterval(() => {
five.setState({ longitude: five.state.longitude + Math.PI / 360 });
}, 16);
startButton.classList.add("d-none");
stopButton.classList.remove("d-none");
},
false
);
stopButton.addEventListener(
"click",
() => {
window.clearInterval(timer);
startButton.classList.remove("d-none");
stopButton.classList.add("d-none");
},
false
);
}
export {};
启动服务 npm run dev
。 并跳转到当前页面 "http://localhost:3000/src/3.recording-state/index.html"。
提示
请查看你的控制台,端口号会因为你的配置以及当前端口占用情况变更,请已控制台输出的为准。
如果你使用其他开发构建工具,请按照自己的开发构建工具的要求启动服务。
录制 / 回放
这章我们继续通过 State 来完成一个有意思的应用。 本章我们会完成这样的一个应用,记录用户在页面上发生的 State,并且可以回放这些操作。