# @geckos\.io/snapshot-interpolation 中文文档

规范地址：https://npmcn.com/package/%40geckos.io/snapshot-interpolation/

版本：1\.1\.1
npm 元数据更新时间：2025-02-22T12:07:54.264Z
中文译文更新时间：2026-11-16T01:32:47.696Z
软件包许可：BSD-3-Clause

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40geckos.io/snapshot-interpolation
原文对照：https://npmcn.com/package/%40geckos.io/snapshot-interpolation/?lang=original
项目主页：http://geckos.io/
源码仓库：https://github.com/geckosio/snapshot-interpolation

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：162 kB，34 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2020-06-02T18:10:33.765Z
- 最新版本发布：2025-02-22T12:07:54.064Z
- 近 30 天下载量：5321（2026-10-16 至 2026-11-14，npm 官方下载统计）
- 已发布版本数：10（近 12 个月 0 个）

## 中文简介

实时多人游戏快照国际化库

## 安装

```sh
npm install @geckos.io/snapshot-interpolation
```

也可以使用：`pnpm add @geckos.io/snapshot-interpolation`、`yarn add @geckos.io/snapshot-interpolation`、`bun add @geckos.io/snapshot-interpolation`

## README

![logo](https://npmcn.com/package/%40geckos.io/snapshot-interpolation/readme/logo.png)

# 快照内插

## 实时多人游戏快照国际化库

#### 在您的游戏中轻松地添加快照IPation(也叫实体IPation或缓冲IPation).

[![NPM version](https://img.shields.io/npm/v/@geckos.io/snapshot-interpolation.svg?style=flat-square)](https://www.npmjs.com/package/@geckos.io/snapshot-interpolation) [![Github Workflow](https://img.shields.io/github/workflow/status/geckosio/snapshot-interpolation/CI/master?label=github%20build&logo=github&style=flat-square)](https://github.com/geckosio/snapshot-interpolation/actions?query=workflow%3ACI) [![GitHub last commit](https://img.shields.io/github/last-commit/geckosio/snapshot-interpolation?style=flat-square)](https://github.com/geckosio/snapshot-interpolation/commits/master) [![Downloads](https://img.shields.io/npm/dm/@geckos.io/snapshot-interpolation.svg?style=flat-square)](https://www.npmjs.com/package/@geckos.io/snapshot-interpolation) [![Codecov](https://img.shields.io/codecov/c/github/geckosio/snapshot-interpolation?logo=codecov&style=flat-square)](https://codecov.io/gh/geckosio/snapshot-interpolation) [![build with TypeScript](https://img.shields.io/badge/built%20with-TypeScript-blue?style=flat-square)](https://www.typescriptlang.org/)

* * *

## 关于

内向缓冲器默认为3个服务器帧长(刑警组织在4个快照之间). 所以,如果 **潜伏是50米** 和一个 **服务器框架为 50ms**内向缓冲器有200多米长.

如果你有兴趣多了解一点"快照"的内幕 [这个视频](https://youtu.be/Z9X4lysFr64?t=800)。 。 。 .

## 特征

-   轻松添加 **客户端预测** 财务报告和审定财务报表 **服务器对齐**。 。 。 .
-   轻松添加 **损失补偿**。 。 。 .
-   轻松 **压缩/编码** 发送/接收前的快照.

## 导言

看看这些YouTube视频:

-   [短版本](https://youtu.be/-9ix6JxpqGo)
-   [长版本](https://youtu.be/ciNR4t-5-WI)

## 游戏示例

这个 [github 仓库](https://github.com/geckosio/snapshot-interpolation) 包含一个很好的例子。 看看这个!

```bash
# clone the repo
$ git clone https://github.com/geckosio/snapshot-interpolation.git

# cd into it
$ cd snapshot-interpolation

# install all dependencies
$ npm install

# start the example
$ npm start

# play in your browser
http://localhost:8080/
```

## 安装

从 npm 安装 .

```console
npm install @geckos.io/snapshot-interpolation
```

或使用捆绑版.

```html
<script src="https://unpkg.com/@geckos.io/snapshot-interpolation@1.0.1/bundle/snapshot-interpolation.js"></script>
<script>
  const { SnapshotInterpolation, Vault } = Snap
</script>
```

## 如何使用

### 服务器.js

```js
// import @geckos.io/snapshot-interpolation
import { SnapshotInterpolation } from '@geckos.io/snapshot-interpolation'

// initialize the library
const SI = new SnapshotInterpolation()

// your server update loop
update() {
  // create a snapshot of the current world
  const snapshot = SI.snapshot.create(worldState)

  // add the snapshot to the vault in case you want to access it later (optional)
  SI.vault.add(snapshot)

  // send the snapshot to the client (using geckos.io or any other library)
  this.emit('update', snapshot)
}
```

### 客户端.js

```js
// import @geckos.io/snapshot-interpolation
import { SnapshotInterpolation } from '@geckos.io/snapshot-interpolation'

// initialize the library (add your server's fps)
const SI = new SnapshotInterpolation(serverFPS)

// when receiving the snapshot on the client
this.on('update', (snapshot) => {
  // read the snapshot
  SI.snapshot.add(snapshot)
}

// your client update loop
update() {
  // calculate the interpolation for the parameters x and y and return the snapshot
  const snapshot = SI.calcInterpolation('x y') // [deep: string] as optional second parameter

  // access your state
  const { state } = snapshot

  // apply the interpolated values to you game objects
  const { id, x, y } = state[0]
  if (hero.id === id) {
    hero.x = x
    hero.y = y
  }
}
```

## 世界状况

世界国必须是一个有非巢穴物体(Quaternion除外)的阵列. 你可以随心所欲地给你取名 度值、弧度值或四角值加 `key(deg)`, (中文(简体) ), `key(rad)` 或者说 `key(quat)`。 。 。 .

### 线性内插

```js
// the worldState on the server
const worldState = [
  { id: 'heroBlue', x: 23, y: 14, z: 47 },
  { id: 'heroRed', x: 23, y: 14, z: 47 },
  { id: 'heroGreen', x: 23, y: 14, z: 47 }
]

// calc interpolation on the client
SI.calcInterpolation('x y z')
```

### 学位、放射性和定性

```js
// the worldState on the server
const worldState = [
  {
    id: 'myHero',
    direction: Math.PI / 2,
    rotationInDeg: 90,
    q: { x: 0, y: 0.707, z: 0, w: 0.707 }
  }
]

// calc interpolation on the client
SI.calcInterpolation('direction(rad) rotationInDeg(deg) q(quat)')
```

## 多个国家

```js
// the players state
const playersState = [
  { id: 0, x: 65, y: -17 },
  { id: 1, x: 32, y: 9 }
]

// the towers state
const towersState = [
  { id: 0, health: 100, type: 'blizzardCannon' },
  { id: 1, health: 89, type: 'flameThrower' }
]

const worldState = {
  players: playersState,
  towers: towersState
}

// create snapshot
SI.snapshot.create(worldState)

// we only want to interpolate x and y on the players object
SI.calcInterpolation('x y', 'players')
```

## 默认

断层挡住你所有的快照 每个快照Poltocation实例都包含一个"断层",但如果需要,您可以轻松创建更多:

```js
import { Vault } from '@geckos.io/snapshot-interpolation'

const customVault = new Vault()
```

## 压缩

您可以使用软件包 [@geckos](https://www.npmjs.com/package/@geckos.io/typed-array-buffer-schema) 在将快照发送给客户端之前先压缩快照.

```js
// schema.js
const playerSchema = BufferSchema.schema('player', {
  id: uint8,
  x: { type: int16, digits: 1 },
  y: { type: int16, digits: 1 }
})

const snapshotSchema = BufferSchema.schema('snapshot', {
  id: { type: string8, length: 6 },
  time: uint64,
  state: { players: [playerSchema] }
})

export const snapshotModel = new Model(snapshotSchema)

// server.js
const snapshot = SI.snapshot.create({
  players: [
    { id: 0, x: 17.5, y: -1.1 },
    { id: 1, x: 5.8, y: 18.9 }
  ]
})

const buffer = snapshotModel.toBuffer(snapshot)
send(buffer)

// client.js
receive(buffer => {
  snapshot = snapshotModel.fromBuffer(buffer)
  SI.addSnapshot(snapshot)
})
```

## API 密码

这看起来非常TypeScriptisch, 但你当然也可以在JavaScript中使用.

```ts
// import
import { SnapshotInterpolation, Vault, Types } from '@geckos.io/snapshot-interpolation'

// types
type Value = number | string | Quat | undefined
type ID = string
type Time = number
type State = Entity[]
type Quat = { x: number; y: number; z: number; w: number }

// interfaces
export interface Entity {
  id: string
  [key: string]: Value
}
export interface Snapshot {
  id: ID
  time: Time
  state: State | { [key: string]: State }
}
export interface InterpolatedSnapshot {
  state: State
  percentage: number
  older: ID
  newer: ID
}

// static methods
/** Create a new Snapshot */
SnapshotInterpolation.CreateSnapshot(state: State): Snapshot

/** Create a new ID */
SnapshotInterpolation.NewId(): string

/** Get the current time in milliseconds. */
SnapshotInterpolation.Now(): number

// class SnapshotInterpolation
const SI = new SnapshotInterpolation(serverFPS?: number)

/** Access the vault. */
SI.vault: Vault

/** Get the Interpolation Buffer time in milliseconds. */
SI.interpolationBuffer.get(): number

/** Set the Interpolation Buffer time in milliseconds. */
SI.interpolationBuffer.set(milliseconds: number): void

/** Create the snapshot on the server. */
SI.snapshot.create(state: State): Snapshot

/** Add the snapshot you received from the server to automatically calculate the interpolation with calcInterpolation() */
SI.snapshot.add(snapshot: Snapshot): void

/** Interpolate between two snapshots give the percentage or time. */
SI.interpolate(snapshotA: Snapshot, snapshotB: Snapshot, timeOrPercentage: number, parameters: string, deep: string): InterpolatedSnapshot

/** Get the calculated interpolation on the client. */
SI.calcInterpolation(parameters: string, deep: string): InterpolatedSnapshot | undefined

// class Vault
const vault = new Vault()

/** Get a Snapshot by its ID. */
vault.getById(id: ID): Snapshot

/** Get the latest snapshot */
vault.get(): Snapshot | undefined;

/** Get the two snapshots around a specific time */
vault.get(time: number): { older: Snapshot; newer: Snapshot; } | undefined

/** Get the closest snapshot to e specific time */
vault.get(time: number, closest: boolean): Snapshot | undefined

/** Add a snapshot to the vault. */
vault.add(snapshot: Snapshot): void

/** Get the current capacity (size) of the vault. */
vault.size(): number

/** Set the max capacity (size) of the vault. */
vault.setMaxSize(size: number): void

/** Get the max capacity (size) of the vault. */
vault.getMaxSize(): number

```

## 常见问题

### @geckos\.io/snapshot-interpolation 是什么？

实时多人游戏快照国际化库

### 如何安装 @geckos\.io/snapshot-interpolation？

在项目目录运行 npm install @geckos\.io/snapshot-interpolation，或使用 pnpm add / yarn add / bun add。

### @geckos\.io/snapshot-interpolation 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @geckos\.io/snapshot-interpolation 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @geckos\.io/snapshot-interpolation 可以商用吗？

@geckos\.io/snapshot-interpolation 使用 BSD-3-Clause 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括，不构成法律意见，请以许可原文为准。

### @geckos\.io/snapshot-interpolation 的下载量有多少？

根据 npm 官方下载统计，@geckos\.io/snapshot-interpolation 在 2026/10/16 – 2026/11/14 这 30 天内被下载 5,321 次，日均约 177 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @geckos\.io/snapshot-interpolation 还在维护吗？

最新版本 1\.1\.1 发布于 2025/02/22（1\.7 年前）；至今共发布 10 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/06/03。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

