Five 插件
Five 提供了工厂模式的插件机制, 提供了 FivePlugin FivePluginInstance FivePluginInits 接口来帮助创建和使用插件
mkdir a-five-pluginnpm init -ynpm install @realsee/fivenpx five-plugin-init Copy
mkdir a-five-pluginnpm init -ynpm install @realsee/fivenpx five-plugin-init
.├── README.md├── devtools│ ├── external-five.js│ ├── external-three.js│ ├── tsconfig.build.json│ ├── webpack.bundle.js│ └── webpack.example.js├── docs├── examples│ ├── data.json│ ├── index.html│ └── index.ts├── lib│ └── index.ts├── package.json└── tsconfig.json Copy
.├── README.md├── devtools│ ├── external-five.js│ ├── external-three.js│ ├── tsconfig.build.json│ ├── webpack.bundle.js│ └── webpack.example.js├── docs├── examples│ ├── data.json│ ├── index.html│ └── index.ts├── lib│ └── index.ts├── package.json└── tsconfig.json
example
localhost:8080
build:doc
docs
build:typescript
tsc
build
build:bundle
webpack
bundle
build:all
npm run build:doc && npm run build:typescript && && npm run build:example && npm run build:bundle
编译配置均在 devtools 目录下, 可以自行修改配置
devtools/tsconfig.build.json
devtools/webpack.bundle.js
build/index.js
package.json
type FivePlugin<Parameters, Returns>; Copy
type FivePlugin<Parameters, Returns>;
这是一个在当five显示界面被点击时 打印 "tap!" 的一个简单插件, 并且可以拉取历史列表
import { FivePlugin } from "@realsee/five";// 打印的内容type TapRecordPluginParameter = string;// 实例方法type TapRecordPluginReturns = { // 一个返回历史列表的防范 getHistory: () => string[]};const TapRecordPlugin: FivePlugin< TapRecordPluginParameter, TapRecordPluginReturns> = (five, message = "tap!") => { const list: string[] = []; five.on("tapGesture", () => { console.log(message); list.push(`${new Date().toUTCString()} ${message}`); }); return { getHistory: () => list.slice() };}; Copy
import { FivePlugin } from "@realsee/five";// 打印的内容type TapRecordPluginParameter = string;// 实例方法type TapRecordPluginReturns = { // 一个返回历史列表的防范 getHistory: () => string[]};const TapRecordPlugin: FivePlugin< TapRecordPluginParameter, TapRecordPluginReturns> = (five, message = "tap!") => { const list: string[] = []; five.on("tapGesture", () => { console.log(message); list.push(`${new Date().toUTCString()} ${message}`); }); return { getHistory: () => list.slice() };};
type FivePluginInit<FivePlugin> Copy
type FivePluginInit<FivePlugin>
就上面 TapRecordPlugin 的例子来初始化插件
import Five, { FivePluginInits } from "@realsee/five";const five = new Five({ plugins: [ [ // 插件 TapRecordPlugin, // 插件对象在 five.plugins 中的命名实例名称, // 比如这个就是通过 five.plguins.tapRecord 获取到插件实例 "tapRecord", // 插件参数,必须符合 TapRecordPluginParameter 的生命 "tap!" ] as FivePluginInit<typeof TapRecordPlugin> ]});// five.load(...) Copy
import Five, { FivePluginInits } from "@realsee/five";const five = new Five({ plugins: [ [ // 插件 TapRecordPlugin, // 插件对象在 five.plugins 中的命名实例名称, // 比如这个就是通过 five.plguins.tapRecord 获取到插件实例 "tapRecord", // 插件参数,必须符合 TapRecordPluginParameter 的生命 "tap!" ] as FivePluginInit<typeof TapRecordPlugin> ]});// five.load(...)
as FivePluginInit
type FivePluginInstance<FivePlugin>; Copy
type FivePluginInstance<FivePlugin>;
简单例子 就上面 TapRecordPlugin 的例子来获取历时点击列表
import { FivePluginInstance } from "@realsee/five";// five.plugins.tapRecord 可以获取到插件实例 "tapRecord" 是插件初始化声明确定的// 通过 FivePluginInstance 在约束类型,否则均为 anyconst tapRecord: FivePluginInstance<typeof TapRecordPlugin> = five.plugins.tapRecord;// 现在就可以获取到历时列表了const getHistory: string[] = tapRecord.getHistory();console.log(getHistory); Copy
import { FivePluginInstance } from "@realsee/five";// five.plugins.tapRecord 可以获取到插件实例 "tapRecord" 是插件初始化声明确定的// 通过 FivePluginInstance 在约束类型,否则均为 anyconst tapRecord: FivePluginInstance<typeof TapRecordPlugin> = five.plugins.tapRecord;// 现在就可以获取到历时列表了const getHistory: string[] = tapRecord.getHistory();console.log(getHistory);
import Five, { FivePlugin, FivePluginInit, FivePluginInstance } from "@realsee/five";// 打印的内容type TapRecordPluginParameter = string;// 实例方法type TapRecordPluginReturns = { // 一个返回历史列表的防范 getHistory: () => string[]};const TapRecordPlugin: FivePlugin< TapRecordPluginParameter, TapRecordPluginReturns> = (five, message = "tap!") => { const list: string[] = []; five.on("tapGesture", () => { console.log(message); list.push(`${new Date().toUTCString()} ${message}`); }); return { getHistory: () => list.slice() };};const five = new Five({ plugins: [ [ // 插件对象在 five.plugins 中的命名实例名称, // 比如这个就是通过 five.plguins.tapRecord 获取到插件实例 "tapRecord", // 插件 TapRecordPlugin, // 插件参数,必须符合 TapRecordPluginParameter 的生命 "tap!" ] as FivePluginInit<typeof TapRecordPlugin> ]});// five.load(...)// five.plugins.tapRecord 可以获取到插件实例 "tapRecord" 是插件初始化声明确定的// 通过 FivePluginInstance 在约束类型,否则均为 anyconst tapRecord: FivePluginInstance<typeof TapRecordPlugin> = five.plugins.tapRecord;// 现在就可以获取到历时列表了const getHistory: string[] = tapRecord.getHistory();console.log(getHistory); Copy
import Five, { FivePlugin, FivePluginInit, FivePluginInstance } from "@realsee/five";// 打印的内容type TapRecordPluginParameter = string;// 实例方法type TapRecordPluginReturns = { // 一个返回历史列表的防范 getHistory: () => string[]};const TapRecordPlugin: FivePlugin< TapRecordPluginParameter, TapRecordPluginReturns> = (five, message = "tap!") => { const list: string[] = []; five.on("tapGesture", () => { console.log(message); list.push(`${new Date().toUTCString()} ${message}`); }); return { getHistory: () => list.slice() };};const five = new Five({ plugins: [ [ // 插件对象在 five.plugins 中的命名实例名称, // 比如这个就是通过 five.plguins.tapRecord 获取到插件实例 "tapRecord", // 插件 TapRecordPlugin, // 插件参数,必须符合 TapRecordPluginParameter 的生命 "tap!" ] as FivePluginInit<typeof TapRecordPlugin> ]});// five.load(...)// five.plugins.tapRecord 可以获取到插件实例 "tapRecord" 是插件初始化声明确定的// 通过 FivePluginInstance 在约束类型,否则均为 anyconst tapRecord: FivePluginInstance<typeof TapRecordPlugin> = five.plugins.tapRecord;// 现在就可以获取到历时列表了const getHistory: string[] = tapRecord.getHistory();console.log(getHistory);
new Five
load
five.on("dispose")
插件参数, 没有参数则为 void
如果有参数则插件暴露的 api,没有参数则不传
Five 插件
插件 Plugins
插件的作用
创建插件
项目目录
npm script
example: 开发环境,编译 example 下的 index.ts, 监听变化,并且开启端口监听,可以通过localhost:8080访问build:doc: 通过 typedoc 生成项目文档,生成目标在docs目录下build:typescript: 通过tsc编译 ts 代码,生成目标在build目录下build:bundle: 通过webpack编译 unpack 包, 生成目标在bundle目录下build:all:npm run build:doc && npm run build:typescript && && npm run build:example && npm run build:bundle编译配置
编译配置均在 devtools 目录下, 可以自行修改配置
devtools/tsconfig.build.json下devtools/webpack.bundle.js下发布
build/index.js, 即通过 tsc 编译得到的代码。你可以在package.json中修改。build:bundle生成的结果。类型说明
简单例子
插件初始化
类型说明
简单例子
as FivePluginInit的方法声明,否则无法约束参数使用插件实例
简单例子 就上面 TapRecordPlugin 的例子来获取历时点击列表
回顾整体的例子
其他注意的问题
new Five时,Five 并没有调用load来创建VR,请通过监听事件的方式,在合适的时机获取模型。five.on("dispose")事件来处理。