lyb-js 1.6.5 → 1.6.7

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.
@@ -2,7 +2,7 @@
2
2
  * @description 用于监听浏览器窗口尺寸变化的类
3
3
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsResizeWatcher-窗口监听
4
4
  */
5
- type Listener = (w: number, h: number) => void;
5
+ type Listener = (w: number, h: number, s: number) => void;
6
6
  /** @description 监听窗口变化,内部只注册一次resize事件,调用监听自身可取消监听 */
7
7
  export declare class LibJsResizeWatcher {
8
8
  /** 存储所有监听器及其是否需要立即执行的标志 */
@@ -9,7 +9,15 @@ export class LibJsResizeWatcher {
9
9
  this._handleResize = () => {
10
10
  const w = window.innerWidth;
11
11
  const h = window.innerHeight;
12
- this._listeners.forEach(({ cb }) => cb(w, h));
12
+ let s;
13
+ const orientation = w > h ? "h" : "v";
14
+ if (orientation === "h") {
15
+ s = Math.min(w / 1920, h / 1080);
16
+ }
17
+ else {
18
+ s = Math.min(w / 1080, h / 1920);
19
+ }
20
+ this._listeners.forEach(({ cb }) => cb(w, h, s));
13
21
  };
14
22
  this._mode = mode;
15
23
  if (mode === "h" || mode === "v")
@@ -24,18 +32,28 @@ export class LibJsResizeWatcher {
24
32
  * @returns 一个函数,用于移除该监听器
25
33
  */
26
34
  on(cb, immediate = true) {
35
+ const w = window.innerWidth;
36
+ const h = window.innerHeight;
37
+ const orientation = w > h ? "h" : "v";
27
38
  if (this._mode === "h") {
28
- immediate && cb(1920, 1080);
39
+ immediate && cb(1920, 1080, Math.min(w / 1920, h / 1080));
29
40
  return () => { };
30
41
  }
31
42
  else if (this._mode === "v") {
32
- immediate && cb(1080, 1920);
43
+ immediate && cb(1080, 1920, Math.min(w / 1080, h / 1920));
33
44
  return () => { };
34
45
  }
46
+ let s;
47
+ if (orientation === "h") {
48
+ s = Math.min(w / 1920, h / 1080);
49
+ }
50
+ else {
51
+ s = Math.min(w / 1080, h / 1920);
52
+ }
35
53
  const item = { cb, immediate };
36
54
  this._listeners.push(item);
37
55
  if (immediate)
38
- cb(window.innerWidth, window.innerHeight);
56
+ cb(window.innerWidth, window.innerHeight, s);
39
57
  return () => {
40
58
  this._listeners = this._listeners.filter((l) => l !== item);
41
59
  };
@@ -9,4 +9,4 @@ export declare const libJsCoordsAngle: (coord1: {
9
9
  }, coord2: {
10
10
  x: number;
11
11
  y: number;
12
- }) => number;
12
+ }, mode?: "deg" | "rad") => number;
@@ -3,19 +3,22 @@
3
3
  * @param coord2 终点坐标
4
4
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsCoordsAngle-两点角度
5
5
  */
6
- export const libJsCoordsAngle = (coord1, coord2) => {
6
+ export const libJsCoordsAngle = (coord1, coord2, mode = "deg") => {
7
7
  //计算相对于第一个坐标的水平和垂直距离
8
8
  const deltaX = coord2.x - coord1.x;
9
9
  const deltaY = coord2.y - coord1.y;
10
10
  //使用反三角函数计算角度(以弧度为单位)
11
11
  const angleRad = Math.atan2(deltaY, deltaX);
12
- //将弧度转换为角度
13
- let angleDeg = angleRad * (180 / Math.PI);
14
- //将角度转换为顺时针方向为正方向的角度
15
- angleDeg = -angleDeg + 90;
16
- //调整角度使得右边成为 360 度的位置变为 0
17
- if (angleDeg < 0) {
18
- angleDeg += 360;
12
+ if (mode === "deg") {
13
+ //将弧度转换为角度
14
+ let angleDeg = angleRad * (180 / Math.PI);
15
+ //将角度转换为顺时针方向为正方向的角度
16
+ angleDeg = -angleDeg + 90;
17
+ //调整角度使得右边成为 360 度的位置变为 0
18
+ if (angleDeg < 0) {
19
+ angleDeg += 360;
20
+ }
21
+ return angleDeg;
19
22
  }
20
- return angleDeg;
23
+ return angleRad;
21
24
  };
package/libJs.d.ts CHANGED
@@ -198,7 +198,7 @@ export declare const Math: {
198
198
  }, coord2: {
199
199
  x: number;
200
200
  y: number;
201
- }) => number;
201
+ }, mode?: "deg" | "rad") => number;
202
202
  /** @description 计算两点距离
203
203
  * @param coord1 起点坐标
204
204
  * @param coord2 终点坐标
package/lyb.js CHANGED
@@ -284,16 +284,19 @@ ${log3.label}:`, log3.value];
284
284
  throw new Error("请使用正确类型");
285
285
  }
286
286
  };
287
- const libJsCoordsAngle = (coord1, coord2) => {
287
+ const libJsCoordsAngle = (coord1, coord2, mode = "deg") => {
288
288
  const deltaX = coord2.x - coord1.x;
289
289
  const deltaY = coord2.y - coord1.y;
290
290
  const angleRad = Math.atan2(deltaY, deltaX);
291
- let angleDeg = angleRad * (180 / Math.PI);
292
- angleDeg = -angleDeg + 90;
293
- if (angleDeg < 0) {
294
- angleDeg += 360;
291
+ if (mode === "deg") {
292
+ let angleDeg = angleRad * (180 / Math.PI);
293
+ angleDeg = -angleDeg + 90;
294
+ if (angleDeg < 0) {
295
+ angleDeg += 360;
296
+ }
297
+ return angleDeg;
295
298
  }
296
- return angleDeg;
299
+ return angleRad;
297
300
  };
298
301
  const libJsCoordsDistance = (coord1, coord2) => {
299
302
  const deltaX = coord2.x - coord1.x;
@@ -3526,7 +3529,14 @@ ${log3.label}:`, log3.value];
3526
3529
  this._handleResize = () => {
3527
3530
  const w = window.innerWidth;
3528
3531
  const h = window.innerHeight;
3529
- this._listeners.forEach(({ cb }) => cb(w, h));
3532
+ let s;
3533
+ const orientation = w > h ? "h" : "v";
3534
+ if (orientation === "h") {
3535
+ s = Math.min(w / 1920, h / 1080);
3536
+ } else {
3537
+ s = Math.min(w / 1080, h / 1920);
3538
+ }
3539
+ this._listeners.forEach(({ cb }) => cb(w, h, s));
3530
3540
  };
3531
3541
  this._mode = mode;
3532
3542
  if (mode === "h" || mode === "v")
@@ -3540,19 +3550,28 @@ ${log3.label}:`, log3.value];
3540
3550
  * @returns 一个函数,用于移除该监听器
3541
3551
  */
3542
3552
  on(cb, immediate = true) {
3553
+ const w = window.innerWidth;
3554
+ const h = window.innerHeight;
3555
+ const orientation = w > h ? "h" : "v";
3543
3556
  if (this._mode === "h") {
3544
- immediate && cb(1920, 1080);
3557
+ immediate && cb(1920, 1080, Math.min(w / 1920, h / 1080));
3545
3558
  return () => {
3546
3559
  };
3547
3560
  } else if (this._mode === "v") {
3548
- immediate && cb(1080, 1920);
3561
+ immediate && cb(1080, 1920, Math.min(w / 1080, h / 1920));
3549
3562
  return () => {
3550
3563
  };
3551
3564
  }
3565
+ let s;
3566
+ if (orientation === "h") {
3567
+ s = Math.min(w / 1920, h / 1080);
3568
+ } else {
3569
+ s = Math.min(w / 1080, h / 1920);
3570
+ }
3552
3571
  const item = { cb, immediate };
3553
3572
  this._listeners.push(item);
3554
3573
  if (immediate)
3555
- cb(window.innerWidth, window.innerHeight);
3574
+ cb(window.innerWidth, window.innerHeight, s);
3556
3575
  return () => {
3557
3576
  this._listeners = this._listeners.filter((l) => l !== item);
3558
3577
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.5",
3
+ "version": "1.6.7",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",