Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs: add Camera guide (follow / screen-world / shake)

yhh7c9dd3547698
diff --git a/docs/astro/astro.config.mjs b/docs/astro/astro.config.mjs
index f9063341f..ccc4e9de4 100644
--- a/docs/astro/astro.config.mjs
+++ b/docs/astro/astro.config.mjs
@@ -50,6 +50,7 @@ export default defineConfig({
translations: { 'zh-CN': '玩法' },
items: [
{ label: 'Input', translations: { 'zh-CN': '输入' }, slug: 'guides/input' },
+ { label: 'Camera', translations: { 'zh-CN': '相机' }, slug: 'guides/camera' },
{ label: 'Physics', translations: { 'zh-CN': '物理' }, slug: 'guides/physics' },
{ label: 'Animation', translations: { 'zh-CN': '动画' }, slug: 'guides/animation' },
{ label: 'Spine Animation', translations: { 'zh-CN': 'Spine 动画' }, slug: 'guides/spine' },
diff --git a/docs/astro/src/content/docs/guides/camera.mdx b/docs/astro/src/content/docs/guides/camera.mdx
new file mode 100644
index 000000000..3233c81aa
--- /dev/null
+++ b/docs/astro/src/content/docs/guides/camera.mdx
@@ -0,0 +1,60 @@
+---
+title: Camera
+description: Follow a target, convert screen/world coordinates, and shake the view.
+---
+
+Every scene renders through a camera — an entity with a `Camera` component. You
+control how it moves and how it maps the screen to the world.
+
+## Follow a target
+
+Add a `FollowTarget` component to the camera and point it at an entity. The
+camera damps toward the target each frame, with a dead zone and smoothing.
+Usually you add it in the editor and pick the target; in code:
+
+```typescript
+import { defineSystem, Query, Commands, Camera, FollowTarget } from 'esengine';
+
+const followPlayer = defineSystem([Query(Camera), Commands()], (cameras, cmds) => {
+ for (const [cameraEntity] of cameras) {
+ cmds.entity(cameraEntity).insert(FollowTarget, {
+ target: playerEntity, // the Entity to follow (from your player Query)
+ offsetY: 40, // look a little above it
+ deadzone: 24, // the target can roam this far before the camera moves
+ damping: 0.25, // smoothing time-constant (0 = snap)
+ });
+ }
+});
+```
+
+## Screen ↔ world
+
+Read `CameraView` to convert between screen pixels and world coordinates — for
+placing entities under the cursor, or checking what's on screen:
+
+```typescript
+import { defineSystem, Res, CameraView, Input } from 'esengine';
+
+const pick = defineSystem([Res(CameraView), Res(Input)], (view, input) => {
+ const worldPos = view.screenToWorld(input.mouseX, input.mouseY); // { x, y } | null
+ const bounds = view.getWorldBounds(); // visible { left, right, bottom, top }
+});
+```
+
+For the mouse specifically, `Res(UICameraInfo)` already gives you `worldMouseX` /
+`worldMouseY` — see [Input](/docs/guides/input/).
+
+## Shake the camera
+
+`shakeCamera` adds a decaying perturbation to the **rendered** view — it never
+touches the camera's Transform, so it always recovers and never dirties the
+scene:
+
+```typescript
+import { shakeCamera } from 'esengine';
+
+shakeCamera(app, { amplitude: 12, rotation: 0, frequency: 22, duration: 0.4 });
+```
+
+Call it wherever you hold the `app` — for example on a hit or an explosion. All
+four options are optional (they default to a short punchy shake).
diff --git a/docs/astro/src/content/docs/zh-cn/guides/camera.mdx b/docs/astro/src/content/docs/zh-cn/guides/camera.mdx
new file mode 100644
index 000000000..58db2c5d0
--- /dev/null
+++ b/docs/astro/src/content/docs/zh-cn/guides/camera.mdx
@@ -0,0 +1,58 @@
+---
+title: 相机
+description: 跟随目标、屏幕/世界坐标转换,以及震屏。
+---
+
+每个场景都通过一个相机来渲染——一个带 `Camera` 组件的实体。你控制它如何移动,以及它
+如何把屏幕映射到世界。
+
+## 跟随目标
+
+给相机加一个 `FollowTarget` 组件并指向某个实体。相机每帧向目标阻尼移动,带死区和平滑。
+通常你在编辑器里添加它并选择目标;用代码:
+
+```typescript
+import { defineSystem, Query, Commands, Camera, FollowTarget } from 'esengine';
+
+const followPlayer = defineSystem([Query(Camera), Commands()], (cameras, cmds) => {
+ for (const [cameraEntity] of cameras) {
+ cmds.entity(cameraEntity).insert(FollowTarget, {
+ target: playerEntity, // 要跟随的实体(来自你的玩家 Query)
+ offsetY: 40, // 稍微看它上方一点
+ deadzone: 24, // 目标在此范围内游走时相机不动
+ damping: 0.25, // 平滑时间常数(0 = 直接吸附)
+ });
+ }
+});
+```
+
+## 屏幕 ↔ 世界
+
+读 `CameraView` 在屏幕像素与世界坐标之间转换——用于把实体放到光标处,或判断什么在
+屏幕上:
+
+```typescript
+import { defineSystem, Res, CameraView, Input } from 'esengine';
+
+const pick = defineSystem([Res(CameraView), Res(Input)], (view, input) => {
+ const worldPos = view.screenToWorld(input.mouseX, input.mouseY); // { x, y } | null
+ const bounds = view.getWorldBounds(); // 可见范围 { left, right, bottom, top }
+});
+```
+
+若只是鼠标,`Res(UICameraInfo)` 已经直接给你 `worldMouseX` / `worldMouseY`——见
+[输入](/docs/zh-cn/guides/input/)。
+
+## 震屏
+
+`shakeCamera` 给**渲染**视图加一个衰减扰动——它不碰相机的 Transform,所以总能恢复,
+也不会弄脏场景:
+
+```typescript
+import { shakeCamera } from 'esengine';
+
+shakeCamera(app, { amplitude: 12, rotation: 0, frequency: 22, duration: 0.4 });
+```
+
+在你持有 `app` 的地方调用它——例如在受击或爆炸时。四个选项都是可选的(默认是一次短促
+有力的震动)。