lyb-pixi-js 1.5.0 → 1.5.2

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.
@@ -1,5 +1,5 @@
1
1
  import { Container } from "pixi.js";
2
- import { LibPixiContainer } from '../Base/LibPixiContainer';
2
+ import { LibPixiContainer } from "../Base/LibPixiContainer";
3
3
  export interface LibPixiScrollNumParams {
4
4
  /** 滚动区域宽度 */
5
5
  width: number;
@@ -1,6 +1,6 @@
1
1
  import { Graphics } from "pixi.js";
2
2
  import gsap from "gsap";
3
- import { LibPixiContainer } from '../Base/LibPixiContainer';
3
+ import { LibPixiContainer } from "../Base/LibPixiContainer";
4
4
  /** @description 通过鼠标或手指拖动数字列选择数字
5
5
  * @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibPixiScrollNum-数字滑动
6
6
  */
@@ -105,8 +105,8 @@ export class LibPixiScrollNum extends LibPixiContainer {
105
105
  */
106
106
  setDepth(containerList, y, startY = 0) {
107
107
  const Y = y - startY;
108
- const idx = Math.floor(Math.abs(Y) / 70);
109
- const t = (Math.abs(Y) % 70) / 70;
108
+ const idx = Math.floor(Math.abs(Y) / this._slideHeight);
109
+ const t = (Math.abs(Y) % this._slideHeight) / this._slideHeight;
110
110
  const prevIdx = idx - 1;
111
111
  const nextIdx = idx + 1;
112
112
  const nextIdx2 = idx + 2;
@@ -1,5 +1,21 @@
1
1
  import { Container } from "pixi.js";
2
- import { LibPixiContainer } from '../Base/LibPixiContainer';
2
+ import { LibPixiContainer } from "../Base/LibPixiContainer";
3
+ interface LibPixiSliderParams {
4
+ /** 滑动区域宽度 */
5
+ width: number;
6
+ /** 滑动区域高度 */
7
+ height: number;
8
+ /** 滑动页列表 */
9
+ slideList: Container[];
10
+ /** 是否启用景深 */
11
+ enableDepth?: boolean;
12
+ /** 景深透明度 */
13
+ depthAlpha?: number;
14
+ /** 景深缩放 */
15
+ depthScale?: number;
16
+ /** 滑动回调 */
17
+ slideCallback: (pageIndex: number, _pageNum: number) => void;
18
+ }
3
19
  /** @description 类似轮播图,但是不会自动轮播
4
20
  * @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibPixiSlider-横向滑动图
5
21
  */
@@ -8,6 +24,8 @@ export declare class LibPixiSlider extends LibPixiContainer {
8
24
  private _currentIndex;
9
25
  /** 滑动区域宽度 */
10
26
  private _slideWidth;
27
+ /** 滑动区域高度 */
28
+ private _slideHeight;
11
29
  /** 记录拖动开始时的X坐标 */
12
30
  private _startX;
13
31
  /** 偏移量 */
@@ -19,6 +37,14 @@ export declare class LibPixiSlider extends LibPixiContainer {
19
37
  private _isDragging;
20
38
  /** 滑动内容 */
21
39
  private _slideArea;
40
+ /** 是否启用景深 */
41
+ private _enableDepth;
42
+ /** 景深透明度 */
43
+ private _depthAlpha;
44
+ /** 景深缩放 */
45
+ private _depthScale;
46
+ /** 滑动容器列表 */
47
+ private _slideList;
22
48
  /** @description 滑动回调 */
23
49
  private slideCallback;
24
50
  /**
@@ -27,7 +53,7 @@ export declare class LibPixiSlider extends LibPixiContainer {
27
53
  * @param content 内容
28
54
  * @param slideCallback 滑动结束回调
29
55
  */
30
- constructor(width: number, height: number, content: Container, slideCallback: (pageIndex: number, _pageNum: number) => void);
56
+ constructor(params: LibPixiSliderParams);
31
57
  /** @description 上一页 */
32
58
  prev(): void;
33
59
  /** @description 下一页 */
@@ -42,4 +68,12 @@ export declare class LibPixiSlider extends LibPixiContainer {
42
68
  private _onDragMove;
43
69
  /** @description 结束拖动 */
44
70
  private _onDragEnd;
71
+ /** @description 设置滚动景深
72
+ * @param containerList 元素列表
73
+ * @param y 拖动Y坐标
74
+ * @param startY 内部将y - startY进行计算
75
+ */
76
+ private _setDepth;
77
+ lerp(a1: number, a2: number, t: number): number;
45
78
  }
79
+ export {};
@@ -1,6 +1,7 @@
1
+ import { Container } from "pixi.js";
1
2
  import gsap from "gsap";
2
3
  import { libPixiOverflowHidden } from "../../Utils/LibPixiOverflowHidden";
3
- import { LibPixiContainer } from '../Base/LibPixiContainer';
4
+ import { LibPixiContainer } from "../Base/LibPixiContainer";
4
5
  /** @description 类似轮播图,但是不会自动轮播
5
6
  * @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibPixiSlider-横向滑动图
6
7
  */
@@ -11,12 +12,15 @@ export class LibPixiSlider extends LibPixiContainer {
11
12
  * @param content 内容
12
13
  * @param slideCallback 滑动结束回调
13
14
  */
14
- constructor(width, height, content, slideCallback) {
15
+ constructor(params) {
16
+ const { width, height, slideList, slideCallback, enableDepth = false, depthAlpha = 0.5, depthScale = 0.5, } = params;
15
17
  super(width, height);
16
18
  /** 当前幻灯片索引 */
17
19
  this._currentIndex = 0;
18
20
  /** 滑动区域宽度 */
19
21
  this._slideWidth = 0;
22
+ /** 滑动区域高度 */
23
+ this._slideHeight = 0;
20
24
  /** 记录拖动开始时的X坐标 */
21
25
  this._startX = 0;
22
26
  /** 偏移量 */
@@ -28,11 +32,22 @@ export class LibPixiSlider extends LibPixiContainer {
28
32
  /* 是否正在拖动 */
29
33
  this._isDragging = false;
30
34
  libPixiOverflowHidden(this);
35
+ this._slideArea = new Container();
31
36
  this._slideWidth = width;
32
- this._slideArea = content;
37
+ this._slideHeight = height;
38
+ this._slideList = slideList;
39
+ this._enableDepth = enableDepth;
40
+ this._depthAlpha = depthAlpha;
41
+ this._depthScale = depthScale;
33
42
  this.slideCallback = slideCallback;
34
- this._pageNum = Math.floor(content.width / this._slideWidth) - 1;
35
- this.addChild(content);
43
+ this._pageNum = slideList.length - 1;
44
+ this._slideList.forEach((item, index) => {
45
+ this._slideArea.addChild(item);
46
+ item.x = index * width + this._slideWidth / 2;
47
+ item.y = this._slideHeight / 2;
48
+ item.pivot.set(this._slideWidth / 2, this._slideHeight / 2);
49
+ });
50
+ this.addChild(this._slideArea);
36
51
  // 监听拖动事件
37
52
  this.eventMode = "static";
38
53
  this.cursor = "pointer";
@@ -54,7 +69,13 @@ export class LibPixiSlider extends LibPixiContainer {
54
69
  _slideTo(index) {
55
70
  if (index < 0) {
56
71
  // 回弹到第一张
57
- gsap.to(this._slideArea, { x: 0, duration: 0.25 });
72
+ gsap.to(this._slideArea, {
73
+ x: 0,
74
+ duration: 0.25,
75
+ onUpdate: () => {
76
+ this._setDepth();
77
+ },
78
+ });
58
79
  this._currentIndex = 0;
59
80
  }
60
81
  else if (index > this._pageNum) {
@@ -62,6 +83,9 @@ export class LibPixiSlider extends LibPixiContainer {
62
83
  gsap.to(this._slideArea, {
63
84
  x: -this._pageNum * this._slideWidth,
64
85
  duration: 0.5,
86
+ onUpdate: () => {
87
+ this._setDepth();
88
+ },
65
89
  });
66
90
  this._currentIndex = this._pageNum;
67
91
  }
@@ -71,6 +95,9 @@ export class LibPixiSlider extends LibPixiContainer {
71
95
  gsap.to(this._slideArea, {
72
96
  x: -this._currentIndex * this._slideWidth,
73
97
  duration: 0.25,
98
+ onUpdate: () => {
99
+ this._setDepth();
100
+ },
74
101
  });
75
102
  }
76
103
  this.slideCallback(this._currentIndex, this._pageNum);
@@ -89,6 +116,7 @@ export class LibPixiSlider extends LibPixiContainer {
89
116
  return;
90
117
  const moveX = event.pageX - this._startX;
91
118
  this._slideArea.x = this._offsetX + moveX;
119
+ this._setDepth();
92
120
  }
93
121
  /** @description 结束拖动 */
94
122
  _onDragEnd(event) {
@@ -108,4 +136,34 @@ export class LibPixiSlider extends LibPixiContainer {
108
136
  }
109
137
  this._slideTo(this._currentIndex);
110
138
  }
139
+ /** @description 设置滚动景深
140
+ * @param containerList 元素列表
141
+ * @param y 拖动Y坐标
142
+ * @param startY 内部将y - startY进行计算
143
+ */
144
+ _setDepth() {
145
+ if (!this._enableDepth)
146
+ return;
147
+ const Y = this._slideArea.x;
148
+ const idx = Math.floor(Math.abs(Y) / this._slideWidth);
149
+ const t = (Math.abs(Y) % this._slideWidth) / this._slideWidth;
150
+ const prevIdx = idx - 1;
151
+ const nextIdx = idx + 1;
152
+ const curItem = this._slideList[idx];
153
+ curItem.alpha = this.lerp(this._depthAlpha, 1, 1 - t);
154
+ curItem.scale.set(this.lerp(this._depthScale, 1, 1 - t));
155
+ if (nextIdx < this._slideList.length) {
156
+ const nextItem = this._slideList[nextIdx];
157
+ nextItem.alpha = this.lerp(this._depthAlpha, 1, t);
158
+ nextItem.scale.set(this.lerp(this._depthScale, 1, t));
159
+ }
160
+ if (prevIdx >= 0) {
161
+ const prevItem = this._slideList[prevIdx];
162
+ prevItem.alpha = this.lerp(this._depthAlpha, 1, 1 - t);
163
+ prevItem.scale.set(this.lerp(this._depthScale, 1, 1 - t));
164
+ }
165
+ }
166
+ lerp(a1, a2, t) {
167
+ return a1 * (1 - t) + a2 * t;
168
+ }
111
169
  }