nhanh-pure-function 3.0.6 → 4.0.0
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/LICENSE +1 -1
- package/README.md +20 -6
- package/dist/Canvas/{LayerGroup → Axis/LayerGroup}/index.d.ts +2 -2
- package/dist/Canvas/{LayerGroup → Axis/LayerGroup}/layer.d.ts +9 -6
- package/dist/Canvas/Axis/OverlayCreator/index.d.ts +52 -0
- package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/index.d.ts +2 -2
- package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/public/overlay.d.ts +4 -4
- package/dist/Canvas/{core → Axis/core}/axis.d.ts +1 -1
- package/dist/Canvas/{core → Axis/core}/basedata.d.ts +1 -1
- package/dist/Canvas/{core → Axis/core}/quikmethod.d.ts +1 -1
- package/dist/Canvas/{core → Axis/core}/style.d.ts +1 -1
- package/dist/Canvas/{index.d.ts → Axis/index.d.ts} +11 -8
- package/dist/Canvas/{index.types.d.ts → Axis/index.types.d.ts} +3 -1
- package/dist/Canvas/{public → Axis/public}/basedata.d.ts +5 -5
- package/dist/Canvas/Axis/public/viewFit.d.ts +28 -0
- package/dist/Canvas/TimeAxis/index.d.ts +165 -0
- package/dist/index.cjs.js +3 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.es.js +2382 -1857
- package/package.json +5 -5
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/arc.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/arcTo.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/bezierCurve.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/custom.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/ellipse.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/line.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/point.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/polygon.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/public/geometricBoundary.d.ts +0 -0
- /package/dist/Canvas/{OverlayGroup → Axis/OverlayGroup}/text.d.ts +0 -0
- /package/dist/Canvas/{common.type.d.ts → Axis/common.type.d.ts} +0 -0
- /package/dist/Canvas/{core → Axis/core}/draw.d.ts +0 -0
- /package/dist/Canvas/{core → Axis/core}/event.d.ts +0 -0
- /package/dist/Canvas/{public → Axis/public}/eventController.d.ts +0 -0
- /package/dist/Canvas/{public → Axis/public}/eventControllerBasedata.d.ts +0 -0
- /package/dist/Canvas/{public → Axis/public}/tools.d.ts +0 -0
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,19 +1,33 @@
|
|
|
1
1
|
# nhanh-pure-function
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
JavaScript/TypeScript 工具库,提供纯函数、画布、格式化、校验等常用能力,适用于前端项目的数据处理与可视化场景。
|
|
4
4
|
|
|
5
5
|
## 安装
|
|
6
6
|
|
|
7
|
-
您可以通过 npm 安装该库:
|
|
8
|
-
|
|
9
7
|
```bash
|
|
10
8
|
npm install nhanh-pure-function
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
##
|
|
11
|
+
## 模块概览
|
|
12
|
+
|
|
13
|
+
| 模块 | 说明 |
|
|
14
|
+
|------|------|
|
|
15
|
+
| **Constant** | 常量:扩展名与 MIME 映射、单位标签、纸张尺寸、窗口目标等 |
|
|
16
|
+
| **Animate** | 动画:进度调度、振荡器、数值过渡 |
|
|
17
|
+
| **Blob** | 数据转图像 URL(Base64、ArrayBuffer、File 等) |
|
|
18
|
+
| **Browser** | 浏览器:帧率、剪贴板、可打印区域、同源标签管理 |
|
|
19
|
+
| **Canvas/Axis** | 数学坐标轴画布 `_Canvas_Axis`:图层、覆盖物(点/线/多边形/圆弧)、视图适配、覆盖物创建交互 |
|
|
20
|
+
| **Canvas/TimeAxis** | 时间轴画布 `_Canvas_TimeAxis`:时间-像素换算、拖拽、缩放、时间范围绘制 |
|
|
21
|
+
| **Element** | DOM:滚动结束监听、点击外部关闭、拖拽等 |
|
|
22
|
+
| **File** | 文件:读取、下载(含进度)、创建并下载 |
|
|
23
|
+
| **Format** | 格式化:首字母大写、百分比、千位分隔符、文件大小、时间戳、链接名等 |
|
|
24
|
+
| **Math** | 数学:经纬度↔平面坐标、点到线段距离、圆弧点、中点、边界交点等 |
|
|
25
|
+
| **Types** | 类型工具:`RequiredBy`、`PartialBy`、`DeepPartial`、`Mutable` |
|
|
26
|
+
| **Utility** | 工具:空闲执行、等待条件、合并对象、防抖节流、UUID 等 |
|
|
27
|
+
| **Valid** | 校验:数组校验、误差范围、点在多边形内、线段与矩形相交、数据类型、安全上下文、文件类型等 |
|
|
28
|
+
| **_Tip** | 提示流:`info`/`success`/`warning`/`error` 链式注册与执行 |
|
|
14
29
|
|
|
15
|
-
`nhanh-pure-function` 库旨在提供一系列纯函数,这些函数在处理数据时不会产生副作用,确保函数的输入和输出之间的映射关系是确定的。这使得代码更易于测试、维护和复用。
|
|
16
30
|
|
|
17
31
|
## 许可证
|
|
18
32
|
|
|
19
|
-
[MIT](https://opensource.org/licenses/MIT)
|
|
33
|
+
[MIT](https://opensource.org/licenses/MIT)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { default as
|
|
1
|
+
import { default as _Canvas_Axis } from '..';
|
|
2
2
|
import { OverlayType } from '../OverlayGroup';
|
|
3
3
|
import { default as Layer } from './layer';
|
|
4
4
|
import { EventHandler, default as EventController } from '../public/eventController';
|
|
@@ -14,7 +14,7 @@ export default class LayerGroup extends EventController {
|
|
|
14
14
|
defaultHover: EventHandler<"hover">;
|
|
15
15
|
defaultDragg: EventHandler<"dragg">;
|
|
16
16
|
defaultDown: EventHandler<"down">;
|
|
17
|
-
setMainCanvas(mainCanvas?:
|
|
17
|
+
setMainCanvas(mainCanvas?: _Canvas_Axis): void;
|
|
18
18
|
setNotifyReload(notifyReload?: () => void): void;
|
|
19
19
|
/** 获取图层 */
|
|
20
20
|
getLayer(name: string): Layer | undefined;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { default as
|
|
1
|
+
import { default as _Canvas_Axis } from '..';
|
|
2
2
|
import { default as OverlayGroup, OverlayType } from '../OverlayGroup';
|
|
3
3
|
import { default as EventController } from '../public/eventController';
|
|
4
|
-
type ConstructorOption = ConstructorParameters<typeof EventController>[0]
|
|
4
|
+
type ConstructorOption = ConstructorParameters<typeof EventController>[0] & {
|
|
5
|
+
/** 层级 */
|
|
6
|
+
zIndex?: number;
|
|
7
|
+
};
|
|
5
8
|
/**
|
|
6
9
|
* 图层事件触发机制说明:
|
|
7
10
|
*
|
|
@@ -15,15 +18,17 @@ type ConstructorOption = ConstructorParameters<typeof EventController>[0];
|
|
|
15
18
|
* 事件触发对象就应该是该 图层 中的 覆盖物,这时就不应该再触发其他 图层 的事件了;
|
|
16
19
|
*/
|
|
17
20
|
export default class Layer extends EventController {
|
|
21
|
+
private _zIndex;
|
|
18
22
|
/** 层级 */
|
|
19
|
-
zIndex: number;
|
|
23
|
+
get zIndex(): number;
|
|
24
|
+
set zIndex(zIndex: number);
|
|
20
25
|
protected canvas: HTMLCanvasElement;
|
|
21
26
|
protected ctx: CanvasRenderingContext2D;
|
|
22
27
|
/** 是否需要重新绘制 */
|
|
23
28
|
private isReload;
|
|
24
29
|
groups: Map<string, OverlayGroup>;
|
|
25
30
|
constructor(option: ConstructorOption);
|
|
26
|
-
setMainCanvas(mainCanvas?:
|
|
31
|
+
setMainCanvas(mainCanvas?: _Canvas_Axis): void;
|
|
27
32
|
setNotifyReload(notifyReload?: () => void): void;
|
|
28
33
|
setGroupNotifyReload(group: OverlayGroup): void;
|
|
29
34
|
/** 获取覆盖物组 */
|
|
@@ -34,8 +39,6 @@ export default class Layer extends EventController {
|
|
|
34
39
|
removeGroup(groups: OverlayGroup | OverlayGroup[]): void;
|
|
35
40
|
/** 清空覆盖物 */
|
|
36
41
|
clearGroup(): void;
|
|
37
|
-
/** 设置图层的 zIndex 值 */
|
|
38
|
-
setzIndex(zIndex: number): void;
|
|
39
42
|
/** 本次绘制的覆盖物 */
|
|
40
43
|
private currentDrawOverlays;
|
|
41
44
|
/** 获取画布 */
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { default as _Canvas_Axis } from '..';
|
|
2
|
+
import { default as Line } from '../OverlayGroup/line';
|
|
3
|
+
import { default as Polygon } from '../OverlayGroup/polygon';
|
|
4
|
+
/**
|
|
5
|
+
* 管理单次「创建覆盖物」的交互(从开始绘制到完成/取消)。
|
|
6
|
+
* 覆盖物有两种定位方式:value = 坐标轴上的值 [x,y][],position = 画布上的位置 px;本类通过 overlay.value 定位。
|
|
7
|
+
*/
|
|
8
|
+
export default class OverlayCreator {
|
|
9
|
+
/** 绑定的画布实例 */
|
|
10
|
+
private canvas;
|
|
11
|
+
private layerGroup;
|
|
12
|
+
/** 创建中覆盖物的绘制层(置顶以保证可见) */
|
|
13
|
+
private overlayLayer;
|
|
14
|
+
/** 该层内的覆盖物分组,便于统一增删 */
|
|
15
|
+
private overlayGroup;
|
|
16
|
+
/** 当前正在创建的覆盖物(多边形或线),其顶点通过 overlay.value 即坐标轴上的值表示 */
|
|
17
|
+
private overlay?;
|
|
18
|
+
/** 已确定的顶点在坐标轴上的值列表 [x, y][],对应 overlay.value */
|
|
19
|
+
private axisValueList?;
|
|
20
|
+
/** 创建时暂存的其他图层群组的 isInteractive,编辑完成后用于复原 */
|
|
21
|
+
private savedLayerGroupInteractive;
|
|
22
|
+
/**
|
|
23
|
+
* 用户自定义限位器:传入原始 value [x, y](坐标轴上的值),约束到合法范围后返回。
|
|
24
|
+
*/
|
|
25
|
+
axisValueLimiter?: (value: [number, number]) => [number, number];
|
|
26
|
+
constructor(canvas: _Canvas_Axis);
|
|
27
|
+
/** 按类型开始创建:多边形或线 */
|
|
28
|
+
create(type: "polygon" | "line"): Line | Polygon | undefined;
|
|
29
|
+
/** 禁用除创建层外的其他图层群组交互,并记录原始 isInteractive 以便复原 */
|
|
30
|
+
private disableOtherLayerGroups;
|
|
31
|
+
/** 复原此前禁用的图层群组的 isInteractive */
|
|
32
|
+
private restoreLayerGroupsInteractive;
|
|
33
|
+
createLine(): Line;
|
|
34
|
+
createPolygon(): Polygon;
|
|
35
|
+
finish?: (overlay?: Polygon | Line) => void;
|
|
36
|
+
/** 从创建层中移除指定覆盖物,不传则清空该层全部 */
|
|
37
|
+
removeOverlays(overlay?: Polygon | Line): void;
|
|
38
|
+
/** 清空创建层并重置当前创建状态 */
|
|
39
|
+
clear(): void;
|
|
40
|
+
/** 销毁实例:清空覆盖物并移除所有事件监听 */
|
|
41
|
+
destroy(): void;
|
|
42
|
+
/** 当前类型完成绘制所需的最少顶点数(多边形 3,线 2) */
|
|
43
|
+
private get minPointCount();
|
|
44
|
+
/** 从鼠标事件得到坐标轴上的 [x, y],若设置了限位器则先限位再返回 */
|
|
45
|
+
private getAxisValueFromEvent;
|
|
46
|
+
/** 鼠标移动:用已确定顶点 + 当前鼠标的坐标轴值更新 overlay.value 预览(至少有一个确定点后才生效) */
|
|
47
|
+
private handleMousemove;
|
|
48
|
+
/** 左键点击:将当前坐标轴值追加为顶点并刷新 overlay.value 预览 */
|
|
49
|
+
private handleClick;
|
|
50
|
+
/** 右键:若顶点数达标则写回 overlay.value 并回调完成,否则移除当前覆盖物并取消创建 */
|
|
51
|
+
private handleContextmenu;
|
|
52
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { default as
|
|
1
|
+
import { default as _Canvas_Axis } from '..';
|
|
2
2
|
import { default as EventController } from '../public/eventController';
|
|
3
3
|
import { default as Text } from './text';
|
|
4
4
|
import { default as Point } from './point';
|
|
@@ -14,7 +14,7 @@ export default class OverlayGroup extends EventController {
|
|
|
14
14
|
overlays: Set<OverlayType>;
|
|
15
15
|
constructor(option: ConstructorOption);
|
|
16
16
|
/** 设置主画布 */
|
|
17
|
-
setMainCanvas(mainCanvas?:
|
|
17
|
+
setMainCanvas(mainCanvas?: _Canvas_Axis): void;
|
|
18
18
|
/** 设置覆盖物重新绘制方法 */
|
|
19
19
|
setNotifyReload(notifyReload?: () => void): void;
|
|
20
20
|
/** 添加覆盖物 */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { default as
|
|
1
|
+
import { default as _Canvas_Axis } from '../..';
|
|
2
2
|
import { OverlayType } from '../index';
|
|
3
3
|
import { EventHandler, default as EventController } from '../../public/eventController';
|
|
4
|
-
import { _Type_DeepPartial } from '
|
|
4
|
+
import { _Type_DeepPartial } from '../../../../';
|
|
5
5
|
import { BaseLineStyle, PointStyleType } from '../../common.type';
|
|
6
6
|
type ConstructorOption<T, V> = ConstructorParameters<typeof EventController>[0] & {
|
|
7
7
|
/** 样式 */
|
|
@@ -13,7 +13,7 @@ type ConstructorOption<T, V> = ConstructorParameters<typeof EventController>[0]
|
|
|
13
13
|
/** 动态点位 */
|
|
14
14
|
dynamicPosition?: V;
|
|
15
15
|
/** 坐标轴上的值 */
|
|
16
|
-
value
|
|
16
|
+
value?: V;
|
|
17
17
|
/** 偏移 */
|
|
18
18
|
offset?: {
|
|
19
19
|
x: number;
|
|
@@ -77,7 +77,7 @@ export default abstract class Overlay<T, V extends [number, number] | [number, n
|
|
|
77
77
|
redrawOnIsHoverChange: boolean;
|
|
78
78
|
/** 默认 hover 事件 */
|
|
79
79
|
defaultHover: EventHandler<"hover">;
|
|
80
|
-
setMainCanvas(mainCanvas?:
|
|
80
|
+
setMainCanvas(mainCanvas?: _Canvas_Axis): void;
|
|
81
81
|
setNotifyReload(notifyReload?: () => void): void;
|
|
82
82
|
/** 值范围 */
|
|
83
83
|
private _valueScope?;
|
|
@@ -102,7 +102,7 @@ export default class BaseData extends EventController {
|
|
|
102
102
|
/** 绘制坐标轴 */
|
|
103
103
|
drawAxis: Axis;
|
|
104
104
|
/** 图层群组 集合 */
|
|
105
|
-
|
|
105
|
+
layerGroups: Map<string, LayerGroup>;
|
|
106
106
|
constructor(option: ConstructorOption);
|
|
107
107
|
setNotifyReload(notifyReload?: () => void): void;
|
|
108
108
|
/** 获取默认中心点位置 */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _Type_DeepPartial } from '
|
|
1
|
+
import { _Type_DeepPartial } from '../../../';
|
|
2
2
|
import { KnownStyleKeys, StyleType } from '../common.type';
|
|
3
3
|
import { default as BaseData } from './basedata';
|
|
4
4
|
type ConstructorOption = ConstructorParameters<typeof BaseData>[0] & {
|
|
@@ -9,11 +9,13 @@ import { default as Polygon } from './OverlayGroup/polygon';
|
|
|
9
9
|
import { default as Custom } from './OverlayGroup/custom';
|
|
10
10
|
import { default as Arc } from './OverlayGroup/arc';
|
|
11
11
|
import { default as ArcTo } from './OverlayGroup/arcTo';
|
|
12
|
+
import { default as OverlayCreator } from './OverlayCreator';
|
|
13
|
+
import { default as ViewFit } from './public/viewFit';
|
|
12
14
|
import { DeepArray } from './common.type';
|
|
13
15
|
export type * from './index.types';
|
|
14
16
|
type ConstructorOption = ConstructorParameters<typeof QuickMethod>[0] & {
|
|
15
17
|
/** 轴线显示属性 */
|
|
16
|
-
axisShow?: Parameters<
|
|
18
|
+
axisShow?: Parameters<_Canvas_Axis["toggleAxis"]>[0];
|
|
17
19
|
};
|
|
18
20
|
/**
|
|
19
21
|
* 你好啊你好的画布工具类
|
|
@@ -21,13 +23,12 @@ type ConstructorOption = ConstructorParameters<typeof QuickMethod>[0] & {
|
|
|
21
23
|
*
|
|
22
24
|
* 使用示例:
|
|
23
25
|
* - GitHub演示:
|
|
24
|
-
* - 基础画布: https://
|
|
25
|
-
* - 动态图表(月牙定理): https://
|
|
26
|
-
* - 阿里云演示:
|
|
27
|
-
* - 基础画布: https://nhanh.xin/#/canvas/_Canvas
|
|
28
|
-
* - 动态图表(月牙定理): https://nhanh.xin/#/math/DynamicDiagram/%E6%9C%88%E7%89%99%E5%AE%9A%E7%90%86
|
|
26
|
+
* - 基础画布: https://nha-nh.github.io/canvas/_Canvas_Axis
|
|
27
|
+
* - 动态图表(月牙定理): https://nha-nh.github.io/math/DynamicDiagram/%E6%9C%88%E7%89%99%E5%AE%9A%E7%90%86
|
|
29
28
|
*/
|
|
30
|
-
export declare class
|
|
29
|
+
export declare class _Canvas_Axis extends QuickMethod {
|
|
30
|
+
/** 视图适配工具:默认缩放、居中计算 */
|
|
31
|
+
static ViewFit: typeof ViewFit;
|
|
31
32
|
/** 图层群组 */
|
|
32
33
|
static LayerGroup: typeof LayerGroup;
|
|
33
34
|
/** 图层 */
|
|
@@ -48,6 +49,8 @@ export declare class _Canvas extends QuickMethod {
|
|
|
48
49
|
static Arc: typeof Arc;
|
|
49
50
|
/** 圆角 */
|
|
50
51
|
static ArcTo: typeof ArcTo;
|
|
52
|
+
/** 按坐标轴值创建覆盖物(多边形/线)的交互管理 */
|
|
53
|
+
overlayCreator: OverlayCreator;
|
|
51
54
|
constructor(option: ConstructorOption);
|
|
52
55
|
private initLayerGroups;
|
|
53
56
|
/** 获取图层群组 集合 */
|
|
@@ -67,4 +70,4 @@ export declare class _Canvas extends QuickMethod {
|
|
|
67
70
|
/** 销毁 */
|
|
68
71
|
destroy(): void;
|
|
69
72
|
}
|
|
70
|
-
export default
|
|
73
|
+
export default _Canvas_Axis;
|
|
@@ -9,6 +9,8 @@ import { default as Axis } from './core/axis';
|
|
|
9
9
|
import { default as Custom } from './OverlayGroup/custom';
|
|
10
10
|
import { default as Arc } from './OverlayGroup/arc';
|
|
11
11
|
import { default as ArcTo } from './OverlayGroup/arcTo';
|
|
12
|
+
import { default as OverlayCreatorByAxisValue } from './OverlayCreator';
|
|
13
|
+
import { default as ViewFit } from './public/viewFit';
|
|
12
14
|
import { EventHandler } from './public/eventController';
|
|
13
15
|
import { default as Overlay } from './OverlayGroup/public/overlay';
|
|
14
|
-
export type { LayerGroup, OverlayGroup, Layer, OverlayType, Overlay, Point, Text, Line, Polygon, Axis, Custom, Arc, ArcTo, EventHandler, };
|
|
16
|
+
export type { LayerGroup, OverlayGroup, Layer, OverlayType, Overlay, Point, Text, Line, Polygon, Axis, Custom, Arc, ArcTo, OverlayCreatorByAxisValue, ViewFit, EventHandler, };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { default as
|
|
1
|
+
import { default as _Canvas_Axis } from '..';
|
|
2
2
|
declare abstract class Base<T extends Base<T>> {
|
|
3
3
|
/** 父级 */
|
|
4
4
|
parent?: T;
|
|
@@ -7,15 +7,15 @@ declare abstract class Base<T extends Base<T>> {
|
|
|
7
7
|
/** 自定义扩展数据 */
|
|
8
8
|
extData?: any;
|
|
9
9
|
/** 主画布 */
|
|
10
|
-
mainCanvas?:
|
|
10
|
+
mainCanvas?: _Canvas_Axis;
|
|
11
11
|
private _isRecalculate;
|
|
12
12
|
/** 是否需要重新计算坐标 */
|
|
13
13
|
get isRecalculate(): boolean;
|
|
14
14
|
set isRecalculate(isRecalculate: boolean);
|
|
15
15
|
/** 是否是同一个主画布 */
|
|
16
|
-
equalsMainCanvas(mainCanvas?:
|
|
16
|
+
equalsMainCanvas(mainCanvas?: _Canvas_Axis): boolean;
|
|
17
17
|
/** 设置主画布 */
|
|
18
|
-
setMainCanvas(mainCanvas?:
|
|
18
|
+
setMainCanvas(mainCanvas?: _Canvas_Axis): void;
|
|
19
19
|
/** 通知重新加载 */
|
|
20
20
|
notifyReload?: (needForceExecute?: boolean) => void;
|
|
21
21
|
/** 设置通知重新加载 */
|
|
@@ -47,7 +47,7 @@ interface BaseDataOptions {
|
|
|
47
47
|
/** 自定义扩展数据 */
|
|
48
48
|
extData?: any;
|
|
49
49
|
/** 主画布 */
|
|
50
|
-
mainCanvas?:
|
|
50
|
+
mainCanvas?: _Canvas_Axis;
|
|
51
51
|
/** 通知重新加载 */
|
|
52
52
|
notifyReload?: (needForceExecute?: boolean) => void;
|
|
53
53
|
/** 是否显示 */
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 视图适配工具:根据内容尺寸与容器尺寸计算默认缩放与居中位置,供画布/图像适配使用。
|
|
3
|
+
*/
|
|
4
|
+
export default class ViewFit {
|
|
5
|
+
/**
|
|
6
|
+
* 计算默认缩放比例,使图像适配视图容器
|
|
7
|
+
* @param naturalWidth 图像原始宽度
|
|
8
|
+
* @param naturalHeight 图像原始高度
|
|
9
|
+
* @param viewWidth 容器宽度
|
|
10
|
+
* @param viewHeight 容器高度
|
|
11
|
+
* @param step 缩放步长
|
|
12
|
+
* @param factor 每翻一倍对应的增量(factor / step = 10)
|
|
13
|
+
* @returns 缩放比例(步长 0.02),小于1表示缩小,大于1表示放大
|
|
14
|
+
*/
|
|
15
|
+
static computeDefaultScale(naturalWidth: number, naturalHeight: number, viewWidth: number, viewHeight: number, step?: number, factor?: number): number;
|
|
16
|
+
/**
|
|
17
|
+
* 计算默认居中偏移,使内容在视图中居中
|
|
18
|
+
* @param naturalWidth 图像/内容原始宽度
|
|
19
|
+
* @param naturalHeight 图像/内容原始高度
|
|
20
|
+
* @param viewWidth 容器宽度
|
|
21
|
+
* @param viewHeight 容器高度
|
|
22
|
+
* @returns 居中时的 left、top 偏移
|
|
23
|
+
*/
|
|
24
|
+
static computeDefaultCenter(naturalWidth: number, naturalHeight: number, viewWidth: number, viewHeight: number): {
|
|
25
|
+
left: number;
|
|
26
|
+
top: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
declare class TimeAxisCanvasManager {
|
|
2
|
+
/** Canvas DOM id(用于 `document.getElementById` 定位) */
|
|
3
|
+
private id;
|
|
4
|
+
/** 绑定到的 canvas 元素 */
|
|
5
|
+
private canvas?;
|
|
6
|
+
/** Canvas 2D 绘制上下文(可能为 null) */
|
|
7
|
+
private ctx;
|
|
8
|
+
/** 当前渲染像素宽度(会随尺寸变化更新) */
|
|
9
|
+
private width;
|
|
10
|
+
/** 当前渲染像素高度(会随尺寸变化更新) */
|
|
11
|
+
private height;
|
|
12
|
+
/** 尺寸变化监听器,用于驱动重绘 */
|
|
13
|
+
private resizeObserver?;
|
|
14
|
+
/** @param id canvas DOM id(通过 getElementById 定位) */
|
|
15
|
+
constructor(id: string);
|
|
16
|
+
/**
|
|
17
|
+
* 初始化 canvas 引用/上下文,并同步画布像素尺寸与注册 resize 监听
|
|
18
|
+
* @returns true 表示初始化成功;false 表示找不到 canvas 或上下文
|
|
19
|
+
*/
|
|
20
|
+
init(onResize: () => void): boolean;
|
|
21
|
+
/** 获取当前 CanvasRenderingContext2D */
|
|
22
|
+
getContext(): CanvasRenderingContext2D | null;
|
|
23
|
+
/** 获取当前 canvas DOM 元素 */
|
|
24
|
+
getCanvas(): HTMLCanvasElement | undefined;
|
|
25
|
+
/** 获取当前画布像素尺寸 */
|
|
26
|
+
getSize(): {
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
};
|
|
30
|
+
/** 根据元素在页面中的尺寸同步 canvas.width/canvas.height */
|
|
31
|
+
syncCanvasSize(): void;
|
|
32
|
+
/** 注册 ResizeObserver,当尺寸变化时同步尺寸并触发外部重绘 */
|
|
33
|
+
private setupResizeListener;
|
|
34
|
+
/** 销毁 ResizeObserver,释放资源 */
|
|
35
|
+
destroy(): void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 鼠标时间提示:仅负责计算鼠标位置对应的时间
|
|
39
|
+
* - 监听 canvas 的 mousemove / mouseleave
|
|
40
|
+
* - 通知外部:显示/隐藏、canvasX、time
|
|
41
|
+
*/
|
|
42
|
+
type TimeAxisMouseTimeInfo = {
|
|
43
|
+
/** 是否显示指示线/时间提示 */
|
|
44
|
+
visible: boolean;
|
|
45
|
+
/** 鼠标在 canvas 内的 x(像素) */
|
|
46
|
+
canvasX: number;
|
|
47
|
+
/** 鼠标在 canvas 内的 y(像素) */
|
|
48
|
+
canvasY: number;
|
|
49
|
+
/** 鼠标位置对应的时间戳(ms) */
|
|
50
|
+
time: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* 时间轴动画曲线:入参 `t` 为 [0, 1],返回 eased 后的进度(也应为 [0, 1])
|
|
54
|
+
*/
|
|
55
|
+
type TimeAxisAnimationCurve = (t: number) => number;
|
|
56
|
+
declare class TimeAxisBase {
|
|
57
|
+
/** 负责 canvas 引用、2D context 获取与尺寸同步 */
|
|
58
|
+
protected canvasManager: TimeAxisCanvasManager;
|
|
59
|
+
/** 鼠标拖拽:把横向像素位移映射到 currentTime 偏移 */
|
|
60
|
+
private offsetDrag?;
|
|
61
|
+
/** 鼠标滚轮缩放:调整 timeSpacing,并保持缩放锚点时间不变 */
|
|
62
|
+
private wheelZoom?;
|
|
63
|
+
/** 鼠标指示:把鼠标位置映射到时间并通知外部 UI */
|
|
64
|
+
private mouseTimeReporter?;
|
|
65
|
+
/** 外部回调:用于更新“鼠标时间提示/指示线”状态 */
|
|
66
|
+
onMouseTimeChange?: (info: TimeAxisMouseTimeInfo) => void;
|
|
67
|
+
/** centerTime 动画 runId:用于取消旧动画回调(拖拽开始时立即失效) */
|
|
68
|
+
private centerTimeAnimationRunId;
|
|
69
|
+
/** 当前时间轴的“左移/右移”基准时间(与 centerTime、画布 x 共同决定可视区间) */
|
|
70
|
+
private currentTime;
|
|
71
|
+
/** 时间轴主刻度间隔:单位 ms */
|
|
72
|
+
private timeSpacing;
|
|
73
|
+
/** timeSpacing 对应的像素距离:单位 px */
|
|
74
|
+
private timeSpacingInPixels;
|
|
75
|
+
/** 背景与坐标轴绘制样式 */
|
|
76
|
+
readonly styleConfig: {
|
|
77
|
+
backgroundColor: string;
|
|
78
|
+
axisColor: string;
|
|
79
|
+
textFont: string;
|
|
80
|
+
};
|
|
81
|
+
/** 刻度绘制比例与文本基线偏移 */
|
|
82
|
+
readonly tickConfig: {
|
|
83
|
+
heightMinor: number;
|
|
84
|
+
heightMajor: number;
|
|
85
|
+
labelOffsetY: number;
|
|
86
|
+
minorTicksPerMajor: number;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* 创建时间轴引擎:会在构造后 `requestAnimationFrame` 初始化并开始渲染。
|
|
90
|
+
* @param id canvas DOM id
|
|
91
|
+
* @param onMouseTimeChange 鼠标移动时回调(用于显示/更新鼠标时间提示)
|
|
92
|
+
*/
|
|
93
|
+
constructor(id: string, onMouseTimeChange?: (info: TimeAxisMouseTimeInfo) => void);
|
|
94
|
+
/**
|
|
95
|
+
* 用户自定义绘制回调:在绘制背景之后、绘制刻度之前执行
|
|
96
|
+
* 可用于叠加额外图形(如指示线、事件点等)
|
|
97
|
+
*/
|
|
98
|
+
onDrawOverlay?: (ctx: CanvasRenderingContext2D, width: number, height: number) => void;
|
|
99
|
+
/** 初始化交互:拖拽平移、滚轮缩放、鼠标时间通知,并触发首次绘制 */
|
|
100
|
+
private init;
|
|
101
|
+
/** 重绘:先清理并绘制背景/叠加层,再绘制刻度与文本 */
|
|
102
|
+
private render;
|
|
103
|
+
/** 绘制背景(清屏 + 填充背景色) */
|
|
104
|
+
private renderBackground;
|
|
105
|
+
/** 绘制刻度与时间标签(按当前可视区间计算 start/end) */
|
|
106
|
+
private renderAxis;
|
|
107
|
+
/**
|
|
108
|
+
* 计算当前视口需要绘制的时间范围与对应像素区间。
|
|
109
|
+
* 返回值用于 `renderAxis()` 中的刻度循环。
|
|
110
|
+
*/
|
|
111
|
+
private get axisDrawingRange();
|
|
112
|
+
/** 获取当前画布中心点对应的时间戳(ms) */
|
|
113
|
+
private get centerTime();
|
|
114
|
+
/** 设置中心时间戳,并据此反推 currentTime 以保持时间视图正确 */
|
|
115
|
+
private set centerTime(value);
|
|
116
|
+
/** 把画布内 x 像素换算为时间戳(ms) */
|
|
117
|
+
getTimeAtCanvasX(x: number): number;
|
|
118
|
+
/** 反向把时间与像素 x 绑定,调整 currentTime 使两者一致 */
|
|
119
|
+
private setTimeAtCanvasX;
|
|
120
|
+
/** 把时间戳(ms)换算为画布内 x 像素 */
|
|
121
|
+
getCanvasXAtTime(time: number): number;
|
|
122
|
+
/**
|
|
123
|
+
* 以动画方式把时间轴中心时间移动到目标值。
|
|
124
|
+
* 用户交互(拖拽/滚轮)会通过 runId 机制取消旧动画回调。
|
|
125
|
+
*/
|
|
126
|
+
animateCenterTimeTo(targetCenterTime: number, options?: {
|
|
127
|
+
/** 动画持续的帧数(数值越大越“慢”、越容易感受到快慢) */
|
|
128
|
+
durationFrames?: number;
|
|
129
|
+
/** 动画曲线:入参 t∈[0,1],出参 easedT∈[0,1] */
|
|
130
|
+
curve?: TimeAxisAnimationCurve;
|
|
131
|
+
}): void;
|
|
132
|
+
/** 递增 runId,使正在进行的中心时间动画回调失效 */
|
|
133
|
+
private stopCenterTimeAnimation;
|
|
134
|
+
/** 销毁:清理交互监听器与释放 canvas 资源 */
|
|
135
|
+
destroy(): void;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 给使用方的绘制辅助类(Overlay 绘制)
|
|
139
|
+
* - 封装 time <-> x 的换算
|
|
140
|
+
* - 提供常用图形绘制方法(如按时间范围画圆角矩形)
|
|
141
|
+
*/
|
|
142
|
+
export declare class _Canvas_TimeAxis extends TimeAxisBase {
|
|
143
|
+
/**
|
|
144
|
+
* 按时间范围绘制圆角矩形(常用于高亮某段时间区间)
|
|
145
|
+
*/
|
|
146
|
+
drawTimeRangeRoundedRect(options: {
|
|
147
|
+
/** 区间起点时间戳(ms) */
|
|
148
|
+
startTimeMs: number;
|
|
149
|
+
/** 区间终点时间戳(ms) */
|
|
150
|
+
endTimeMs: number;
|
|
151
|
+
/** 矩形 y 坐标(像素,默认 1) */
|
|
152
|
+
y?: number;
|
|
153
|
+
/** 矩形高度(像素,默认使用画布高度 - 1) */
|
|
154
|
+
height?: number;
|
|
155
|
+
/** 圆角半径:可为单值或 canvas roundRect 支持的数组(默认 10) */
|
|
156
|
+
radius?: number | number[];
|
|
157
|
+
/** 填充色:不传则不填充 */
|
|
158
|
+
fillStyle?: string;
|
|
159
|
+
/** 描边色:不传则不描边 */
|
|
160
|
+
strokeStyle?: string;
|
|
161
|
+
/** 描边线宽(默认 1) */
|
|
162
|
+
lineWidth?: number;
|
|
163
|
+
}): void;
|
|
164
|
+
}
|
|
165
|
+
export default _Canvas_TimeAxis;
|