lyb-pixi-js 1.5.3 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/Utils/LibPixiWatchProperty.d.ts +9 -0
- package/Utils/LibPixiWatchProperty.js +46 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -144,6 +144,8 @@ app.stage.addChild(box);
|
|
|
144
144
|
|
|
145
145
|
\- [LibPixiPolygonDrawTool-多边形绘制](#LibPixiPolygonDrawTool-多边形绘制)
|
|
146
146
|
|
|
147
|
+
\- [LibPixiWatchProperty-对象属性监听](#LibPixiWatchProperty-对象属性监听)
|
|
148
|
+
|
|
147
149
|
## Base-基础
|
|
148
150
|
|
|
149
151
|
### LibPixiText-文本
|
|
@@ -818,3 +820,20 @@ $bus.on("play", () => {
|
|
|
818
820
|
```ts
|
|
819
821
|
new LibPixiPolygonDrawTool(app);
|
|
820
822
|
```
|
|
823
|
+
|
|
824
|
+
### LibPixiWatchProperty-对象属性监听
|
|
825
|
+
|
|
826
|
+
> 内部通过 Ticker 每秒调用10次循环判断被监听的属性是否发生改变,当元素被销毁时自动销毁 Ticker,取决于是否传递了元素
|
|
827
|
+
|
|
828
|
+
```ts
|
|
829
|
+
LibPixiWatchProperty(
|
|
830
|
+
gameStore,
|
|
831
|
+
["a", "b"],
|
|
832
|
+
() => {
|
|
833
|
+
warningContainer.visible = a;
|
|
834
|
+
useBtn.eventMode = a || b ? "none" : "static";
|
|
835
|
+
},
|
|
836
|
+
this
|
|
837
|
+
);
|
|
838
|
+
```
|
|
839
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Container } from "pixi.js";
|
|
2
|
+
/** @description 对象属性监听
|
|
3
|
+
* @param obj 对象
|
|
4
|
+
* @param keys 监听的对象属性
|
|
5
|
+
* @param callback 监听的对象属性发生变化时触发回调
|
|
6
|
+
* @param container 容器对象,用于判断元素是否被销毁
|
|
7
|
+
* @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibPixiWatchProperty-对象属性监听
|
|
8
|
+
*/
|
|
9
|
+
export declare const LibPixiWatchProperty: <T extends object, K extends keyof T>(obj: T, keys: K[], callback: () => void, container?: Container) => void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Ticker } from "pixi.js";
|
|
2
|
+
/** @description 对象属性监听
|
|
3
|
+
* @param obj 对象
|
|
4
|
+
* @param keys 监听的对象属性
|
|
5
|
+
* @param callback 监听的对象属性发生变化时触发回调
|
|
6
|
+
* @param container 容器对象,用于判断元素是否被销毁
|
|
7
|
+
* @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibPixiWatchProperty-对象属性监听
|
|
8
|
+
*/
|
|
9
|
+
export const LibPixiWatchProperty = (obj, keys, callback, container) => {
|
|
10
|
+
const ticker = new Ticker();
|
|
11
|
+
const lastValues = new Map();
|
|
12
|
+
ticker.maxFPS = 10;
|
|
13
|
+
callback();
|
|
14
|
+
//存储上一次的值
|
|
15
|
+
for (let i = 0; i < keys.length; i++) {
|
|
16
|
+
const key = keys[i];
|
|
17
|
+
lastValues.set(key, obj[key]);
|
|
18
|
+
}
|
|
19
|
+
const flag = (key) => {
|
|
20
|
+
if (lastValues.get(key) !== obj[key]) {
|
|
21
|
+
lastValues.set(key, obj[key]);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
};
|
|
26
|
+
ticker.add(() => {
|
|
27
|
+
//如果元素被销毁
|
|
28
|
+
if (container === null || container === void 0 ? void 0 : container.destroyed) {
|
|
29
|
+
ticker.destroy();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
let changed = false;
|
|
33
|
+
if (keys.length === 1) {
|
|
34
|
+
changed = flag(keys[0]);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
for (let i = 0; i < keys.length; i++) {
|
|
38
|
+
const key = keys[i];
|
|
39
|
+
changed = flag(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (changed)
|
|
43
|
+
callback();
|
|
44
|
+
});
|
|
45
|
+
ticker.start();
|
|
46
|
+
};
|