jmgraph 3.2.26 → 3.2.28
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 +188 -0
- package/dist/jmgraph.core.min.js +1 -1
- package/dist/jmgraph.core.min.js.map +1 -1
- package/dist/jmgraph.js +2713 -425
- package/dist/jmgraph.min.js +1 -1
- package/index.d.ts +142 -1
- package/package.json +1 -1
- package/src/core/jmControl.js +827 -127
- package/src/core/jmEvents.js +154 -0
- package/src/core/jmFilter.js +38 -1
- package/src/core/jmGradient.js +59 -8
- package/src/core/jmGraph.js +51 -7
- package/src/core/jmLayer.js +34 -2
- package/src/core/jmList.js +167 -0
- package/src/core/jmObject.js +128 -8
- package/src/core/jmPath.js +43 -5
- package/src/core/jmProperty.js +181 -2
- package/src/core/jmShadow.js +36 -7
- package/src/core/jmUtils.js +187 -14
- package/src/lib/webgl/base.js +211 -83
- package/src/lib/webgl/core/buffer.js +43 -12
- package/src/lib/webgl/core/mapSize.js +16 -7
- package/src/lib/webgl/core/mapType.js +41 -22
- package/src/lib/webgl/core/program.js +94 -54
- package/src/lib/webgl/core/shader.js +20 -8
- package/src/lib/webgl/core/texture.js +55 -32
- package/src/lib/webgl/gradient.js +49 -17
- package/src/lib/webgl/index.js +173 -24
- package/src/lib/webgl/path.js +61 -12
- package/src/shapes/jmArc.js +48 -2
- package/src/shapes/jmArrow.js +35 -2
- package/src/shapes/jmArrowLine.js +33 -2
- package/src/shapes/jmBezier.js +50 -4
- package/src/shapes/jmCircle.js +35 -2
- package/src/shapes/jmEllipse.js +29 -3
- package/src/shapes/jmHArc.js +39 -2
- package/src/shapes/jmImage.js +49 -3
- package/src/shapes/jmLabel.js +41 -2
- package/src/shapes/jmLine.js +42 -2
- package/src/shapes/jmPolygon.js +42 -3
- package/src/shapes/jmPrismatic.js +34 -2
- package/src/shapes/jmRect.js +45 -3
- package/src/shapes/jmResize.js +42 -4
- package/src/shapes/jmStar.js +38 -4
package/src/core/jmProperty.js
CHANGED
|
@@ -1,45 +1,170 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview jmGraph 属性管理基类
|
|
4
|
+
*
|
|
5
|
+
* jmProperty 是 jmGraph 库中属性管理的核心类,继承自 jmObject。
|
|
6
|
+
* 提供了:
|
|
7
|
+
* - 基于 Symbol 的私有属性存储
|
|
8
|
+
* - 属性变更事件通知机制
|
|
9
|
+
* - 渲染模式管理(2D/WebGL)
|
|
10
|
+
* - 动画帧请求封装
|
|
11
|
+
*
|
|
12
|
+
* @module jmProperty
|
|
13
|
+
* @extends jmObject
|
|
14
|
+
* @author jmGraph Team
|
|
15
|
+
* @license MIT
|
|
16
|
+
*/
|
|
17
|
+
|
|
2
18
|
import {jmUtils} from "./jmUtils.js";
|
|
3
19
|
import { jmObject } from "./jmObject.js";
|
|
4
20
|
|
|
21
|
+
/**
|
|
22
|
+
* 属性存储的 Symbol 键
|
|
23
|
+
* 使用 Symbol 确保属性存储的私有性和唯一性
|
|
24
|
+
* @type {Symbol}
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
5
27
|
const PROPERTY_KEY = Symbol("properties");
|
|
6
28
|
|
|
29
|
+
/**
|
|
30
|
+
* jmGraph 属性管理基类
|
|
31
|
+
*
|
|
32
|
+
* jmProperty 是 jmControl 的父类,为所有图形控件提供属性管理功能。
|
|
33
|
+
* 采用 Symbol + WeakMap 模式实现真正的私有属性存储,避免属性名冲突。
|
|
34
|
+
*
|
|
35
|
+
* 核心特性:
|
|
36
|
+
* 1. **私有属性存储**:使用 Symbol 键确保属性不被外部直接访问
|
|
37
|
+
* 2. **属性变更通知**:设置属性时自动触发 'propertyChange' 事件
|
|
38
|
+
* 3. **渲染模式继承**:子控件自动继承所属 graph 的渲染模式
|
|
39
|
+
* 4. **脏标记传播**:子控件 needUpdate 会自动传播到 graph
|
|
40
|
+
*
|
|
41
|
+
* @class jmProperty
|
|
42
|
+
* @extends jmObject
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // 创建属性对象
|
|
46
|
+
* const prop = new jmProperty({ mode: 'webgl' });
|
|
47
|
+
*
|
|
48
|
+
* // 设置和获取属性
|
|
49
|
+
* prop.property('customValue', 100);
|
|
50
|
+
* console.log(prop.property('customValue')); // 100
|
|
51
|
+
*
|
|
52
|
+
* // 监听属性变更
|
|
53
|
+
* prop.on('propertyChange', (name, args) => {
|
|
54
|
+
* console.log(`${name} changed from ${args.oldValue} to ${args.newValue}`);
|
|
55
|
+
* });
|
|
56
|
+
*/
|
|
7
57
|
export default class jmProperty extends jmObject {
|
|
58
|
+
/**
|
|
59
|
+
* 构造函数
|
|
60
|
+
*
|
|
61
|
+
* 初始化属性存储对象,并设置初始渲染模式。
|
|
62
|
+
*
|
|
63
|
+
* @constructor
|
|
64
|
+
* @param {Object} [params] - 初始化参数
|
|
65
|
+
* @param {'2d'|'webgl'} [params.mode] - 渲染模式,默认 '2d'
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // 创建使用 WebGL 渲染的属性对象
|
|
69
|
+
* const prop = new jmProperty({ mode: 'webgl' });
|
|
70
|
+
*/
|
|
8
71
|
constructor(params) {
|
|
9
72
|
super();
|
|
73
|
+
// 初始化私有属性存储对象
|
|
10
74
|
this[PROPERTY_KEY] = {};
|
|
75
|
+
// 设置渲染模式
|
|
11
76
|
if(params && params.mode) this.mode = params.mode;
|
|
12
77
|
}
|
|
13
78
|
|
|
79
|
+
/**
|
|
80
|
+
* 获取或设置属性值
|
|
81
|
+
*
|
|
82
|
+
* 这是属性系统的核心方法,所有属性的存取都通过此方法。
|
|
83
|
+
* 设置属性时会自动触发 'propertyChange' 事件,便于实现响应式更新。
|
|
84
|
+
*
|
|
85
|
+
* @method property
|
|
86
|
+
* @param {string} name - 属性名称
|
|
87
|
+
* @param {*} [value] - 属性值(设置时提供)
|
|
88
|
+
* @returns {*} 获取时返回属性值,设置时返回设置的值
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* // 获取属性
|
|
92
|
+
* const value = obj.property('myProp');
|
|
93
|
+
*
|
|
94
|
+
* // 设置属性
|
|
95
|
+
* obj.property('myProp', 'newValue');
|
|
96
|
+
*
|
|
97
|
+
* // 链式调用
|
|
98
|
+
* obj.property('a', 1).property('b', 2);
|
|
99
|
+
*/
|
|
14
100
|
property(...pars) {
|
|
15
101
|
if(pars) {
|
|
16
102
|
const pros = this[PROPERTY_KEY];
|
|
17
103
|
const name = pars[0];
|
|
104
|
+
|
|
105
|
+
// 设置属性
|
|
18
106
|
if(pars.length > 1) {
|
|
19
107
|
const value = pars[1];
|
|
20
108
|
const args = {oldValue: pros[name], newValue: value};
|
|
21
109
|
pros[name] = pars[1];
|
|
110
|
+
// 触发属性变更事件(如果对象支持事件)
|
|
22
111
|
if(this.emit) this.emit('propertyChange', name, args);
|
|
23
112
|
return pars[1];
|
|
24
113
|
}
|
|
114
|
+
// 获取属性
|
|
25
115
|
else if(name) {
|
|
26
116
|
return pros[name];
|
|
27
117
|
}
|
|
28
118
|
}
|
|
29
119
|
}
|
|
30
120
|
|
|
121
|
+
/**
|
|
122
|
+
* 是否需要重绘标记
|
|
123
|
+
*
|
|
124
|
+
* 当属性变化导致需要重新渲染时设置此标记。
|
|
125
|
+
* 设置为 true 时,会自动将所属 graph 的 needUpdate 设为 true,
|
|
126
|
+
* 从而触发画布重绘。
|
|
127
|
+
*
|
|
128
|
+
* @type {boolean}
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* // 标记需要重绘
|
|
132
|
+
* control.needUpdate = true;
|
|
133
|
+
*
|
|
134
|
+
* // 检查是否需要重绘
|
|
135
|
+
* if(control.needUpdate) {
|
|
136
|
+
* control.redraw();
|
|
137
|
+
* }
|
|
138
|
+
*/
|
|
31
139
|
get needUpdate() {
|
|
32
140
|
return this.property('needUpdate');
|
|
33
141
|
}
|
|
34
142
|
set needUpdate(v) {
|
|
35
143
|
this.property('needUpdate', v);
|
|
144
|
+
// 传播脏标记到 graph(避免 jmGraph 自身循环)
|
|
36
145
|
if(v && !this.is('jmGraph') && this.graph) {
|
|
37
146
|
this.graph.needUpdate = true;
|
|
38
147
|
}
|
|
39
148
|
}
|
|
40
149
|
|
|
150
|
+
/**
|
|
151
|
+
* 所属的画布实例
|
|
152
|
+
*
|
|
153
|
+
* 获取或设置当前对象所属的 jmGraph 实例。
|
|
154
|
+
* 如果未显式设置,会自动向上查找父级链中的 jmGraph。
|
|
155
|
+
*
|
|
156
|
+
* @type {jmGraph}
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* // 获取所属画布
|
|
160
|
+
* const graph = control.graph;
|
|
161
|
+
*
|
|
162
|
+
* // 设置所属画布(通常由框架内部调用)
|
|
163
|
+
* control.graph = myGraph;
|
|
164
|
+
*/
|
|
41
165
|
get graph() {
|
|
42
166
|
let g = this.property('graph');
|
|
167
|
+
// 如果未设置,尝试从父级链查找
|
|
43
168
|
g = g || (this.property('graph', this.findParent('jmGraph')));
|
|
44
169
|
return g;
|
|
45
170
|
}
|
|
@@ -47,20 +172,74 @@ export default class jmProperty extends jmObject {
|
|
|
47
172
|
return this.property('graph', v);
|
|
48
173
|
}
|
|
49
174
|
|
|
175
|
+
/**
|
|
176
|
+
* 渲染模式
|
|
177
|
+
*
|
|
178
|
+
* 获取当前渲染模式,支持 '2d' 和 'webgl' 两种模式。
|
|
179
|
+
* 渲染模式的查找优先级:
|
|
180
|
+
* 1. 当前对象设置的 mode
|
|
181
|
+
* 2. 如果是 jmGraph,默认 '2d'
|
|
182
|
+
* 3. 从所属 graph 继承 mode
|
|
183
|
+
*
|
|
184
|
+
* @type {'2d'|'webgl'}
|
|
185
|
+
* @readonly
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* // 检查渲染模式
|
|
189
|
+
* if(control.mode === 'webgl') {
|
|
190
|
+
* // 使用 WebGL 特性
|
|
191
|
+
* } else {
|
|
192
|
+
* // 使用 Canvas 2D API
|
|
193
|
+
* }
|
|
194
|
+
*/
|
|
50
195
|
get mode() {
|
|
51
196
|
let m = this.property('mode');
|
|
52
197
|
if(m) return m;
|
|
53
|
-
|
|
54
|
-
return this.
|
|
198
|
+
// 如果当前对象是jmGraph且没有设置mode,返回默认值
|
|
199
|
+
if(this.is('jmGraph')) return this.property('mode') || '2d';
|
|
200
|
+
// 否则从所属的graph获取mode
|
|
201
|
+
return this.graph?.mode || '2d';
|
|
55
202
|
}
|
|
56
203
|
set mode(v) {
|
|
57
204
|
return this.property('mode', v);
|
|
58
205
|
}
|
|
59
206
|
|
|
207
|
+
/**
|
|
208
|
+
* 请求动画帧
|
|
209
|
+
*
|
|
210
|
+
* 封装 requestAnimationFrame,支持在不同环境下工作。
|
|
211
|
+
* 如果当前对象关联了 canvas,会使用 canvas 的 requestAnimationFrame。
|
|
212
|
+
*
|
|
213
|
+
* @method requestAnimationFrame
|
|
214
|
+
* @param {Function} handler - 动画帧回调函数
|
|
215
|
+
* @returns {number} 动画帧请求ID,用于取消
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* // 请求下一帧动画
|
|
219
|
+
* const frameId = control.requestAnimationFrame(() => {
|
|
220
|
+
* // 更新动画状态
|
|
221
|
+
* control.redraw();
|
|
222
|
+
* });
|
|
223
|
+
*
|
|
224
|
+
* // 取消动画帧
|
|
225
|
+
* control.cancelAnimationFrame(frameId);
|
|
226
|
+
*/
|
|
60
227
|
requestAnimationFrame(handler) {
|
|
61
228
|
return jmUtils.requestAnimationFrame(handler, this.graph? this.graph.canvas: null);
|
|
62
229
|
}
|
|
63
230
|
|
|
231
|
+
/**
|
|
232
|
+
* 取消动画帧请求
|
|
233
|
+
*
|
|
234
|
+
* 取消之前通过 requestAnimationFrame 注册的回调。
|
|
235
|
+
*
|
|
236
|
+
* @method cancelAnimationFrame
|
|
237
|
+
* @param {number} handler - 动画帧请求ID
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* // 取消动画帧
|
|
241
|
+
* control.cancelAnimationFrame(frameId);
|
|
242
|
+
*/
|
|
64
243
|
cancelAnimationFrame(handler) {
|
|
65
244
|
return jmUtils.cancelAnimationFrame(handler, this.graph? this.graph.canvas: null);
|
|
66
245
|
}
|
package/src/core/jmShadow.js
CHANGED
|
@@ -1,15 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview jmShadow 阴影类
|
|
3
|
+
*
|
|
4
|
+
* jmShadow 提供了图形阴影效果的创建和管理功能。
|
|
5
|
+
* 支持设置阴影的偏移、模糊程度和颜色。
|
|
6
|
+
*
|
|
7
|
+
* 主要功能:
|
|
8
|
+
* - 阴影偏移设置(x, y)
|
|
9
|
+
* - 模糊程度设置(blur)
|
|
10
|
+
* - 阴影颜色设置(color)
|
|
11
|
+
* - 字符串解析和序列化
|
|
12
|
+
*
|
|
13
|
+
* @module jmShadow
|
|
14
|
+
* @author jmGraph Team
|
|
15
|
+
* @license MIT
|
|
16
|
+
*/
|
|
17
|
+
|
|
1
18
|
import {jmUtils} from "./jmUtils.js";
|
|
2
19
|
|
|
3
20
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
21
|
+
* 阴影类
|
|
22
|
+
*
|
|
23
|
+
* 用于创建图形的阴影效果。阴影可以应用于任何图形控件。
|
|
24
|
+
*
|
|
6
25
|
* @class jmShadow
|
|
7
|
-
*
|
|
8
|
-
* @param {number} y
|
|
9
|
-
* @param {number}
|
|
10
|
-
* @param {
|
|
26
|
+
*
|
|
27
|
+
* @param {number|string} x 横坐标偏移量,或阴影字符串格式 'x,y,blur,color'
|
|
28
|
+
* @param {number} [y] 纵坐标偏移量
|
|
29
|
+
* @param {number} [blur] 模糊值
|
|
30
|
+
* @param {string} [color] 阴影颜色
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // 创建阴影
|
|
34
|
+
* const shadow = new jmShadow(5, 5, 10, 'rgba(0,0,0,0.5)');
|
|
35
|
+
*
|
|
36
|
+
* // 从字符串创建
|
|
37
|
+
* const shadow = new jmShadow('5, 5, 10, rgba(0,0,0,0.5)');
|
|
38
|
+
*
|
|
39
|
+
* // 应用到图形
|
|
40
|
+
* rect.style.shadow = shadow;
|
|
11
41
|
*/
|
|
12
|
-
|
|
13
42
|
export default class jmShadow {
|
|
14
43
|
constructor(x, y, blur, color) {
|
|
15
44
|
if(typeof x == 'string' && !y && !blur && !color) {
|
package/src/core/jmUtils.js
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview jmGraph 工具类
|
|
4
|
+
*
|
|
5
|
+
* jmUtils 是 jmGraph 库的核心工具类,提供了一系列静态工具方法:
|
|
6
|
+
* - 对象克隆与深拷贝
|
|
7
|
+
* - 事件绑定与解绑
|
|
8
|
+
* - DOM 元素位置计算
|
|
9
|
+
* - 几何计算(点在多边形内判断、旋转等)
|
|
10
|
+
* - 颜色解析与转换
|
|
11
|
+
* - 字符串处理
|
|
12
|
+
*
|
|
13
|
+
* 所有方法都是静态的,可以直接通过 jmUtils.methodName() 调用。
|
|
14
|
+
*
|
|
15
|
+
* @module jmUtils
|
|
16
|
+
* @author jmGraph Team
|
|
17
|
+
* @license MIT
|
|
18
|
+
*/
|
|
19
|
+
|
|
2
20
|
import { jmList } from './jmList.js';
|
|
3
21
|
|
|
22
|
+
/**
|
|
23
|
+
* CSS 颜色关键字映射表
|
|
24
|
+
*
|
|
25
|
+
* 包含所有 CSS 标准颜色名称到十六进制值的映射。
|
|
26
|
+
* 支持 147 种命名颜色 + CSS 系统颜色。
|
|
27
|
+
*
|
|
28
|
+
* @constant {Object.<string, string>}
|
|
29
|
+
* @private
|
|
30
|
+
*/
|
|
4
31
|
const colorKeywords = {
|
|
5
32
|
aliceblue: "#f0f8ff",
|
|
6
33
|
antiquewhite: "#faebd7",
|
|
@@ -38,11 +65,13 @@ const colorKeywords = {
|
|
|
38
65
|
darkseagreen: "#8fbc8f",
|
|
39
66
|
darkslateblue: "#483d8b",
|
|
40
67
|
darkslategray: "#2f4f4f",
|
|
68
|
+
darkslategrey: "#2f4f4f",
|
|
41
69
|
darkturquoise: "#00ced1",
|
|
42
70
|
darkviolet: "#9400d3",
|
|
43
71
|
deeppink: "#ff1493",
|
|
44
72
|
deepskyblue: "#00bfff",
|
|
45
73
|
dimgray: "#696969",
|
|
74
|
+
dimgrey: "#696969",
|
|
46
75
|
dodgerblue: "#1e90ff",
|
|
47
76
|
firebrick: "#b22222",
|
|
48
77
|
floralwhite: "#fffaf0",
|
|
@@ -77,6 +106,7 @@ const colorKeywords = {
|
|
|
77
106
|
lightseagreen: "#20b2aa",
|
|
78
107
|
lightskyblue: "#87cefa",
|
|
79
108
|
lightslategray: "#778899",
|
|
109
|
+
lightslategrey: "#778899",
|
|
80
110
|
lightsteelblue: "#b0c4de",
|
|
81
111
|
lightyellow: "#ffffe0",
|
|
82
112
|
lime: "#00ff00",
|
|
@@ -117,6 +147,7 @@ const colorKeywords = {
|
|
|
117
147
|
powderblue: "#b0e0e6",
|
|
118
148
|
purple: "#800080",
|
|
119
149
|
red: "#ff0000",
|
|
150
|
+
rebeccapurple: "#663399",
|
|
120
151
|
rosybrown: "#bc8f8f",
|
|
121
152
|
royalblue: "#4169e1",
|
|
122
153
|
saddlebrown: "#8b4513",
|
|
@@ -129,6 +160,7 @@ const colorKeywords = {
|
|
|
129
160
|
skyblue: "#87ceeb",
|
|
130
161
|
slateblue: "#6a5acd",
|
|
131
162
|
slategray: "#708090",
|
|
163
|
+
slategrey: "#708090",
|
|
132
164
|
snow: "#fffafa",
|
|
133
165
|
springgreen: "#00ff7f",
|
|
134
166
|
steelblue: "#4682b4",
|
|
@@ -143,15 +175,56 @@ const colorKeywords = {
|
|
|
143
175
|
whitesmoke: "#f5f5f5",
|
|
144
176
|
yellow: "#ffff00",
|
|
145
177
|
yellowgreen: "#9acd32",
|
|
146
|
-
transparent: "rgba(0,0,0,0)"
|
|
178
|
+
transparent: "rgba(0,0,0,0)",
|
|
179
|
+
// grey 别名(已有 darkslategrey/lightslategrey/slategrey/dimgrey)
|
|
180
|
+
// 以下为 CSS 系统颜色
|
|
181
|
+
activeborder: "#bfcaca",
|
|
182
|
+
activecaption: "#000080",
|
|
183
|
+
appworkspace: "#ababab",
|
|
184
|
+
background: "#636363",
|
|
185
|
+
buttonface: "#c0c0c0",
|
|
186
|
+
buttonhighlight: "#dedede",
|
|
187
|
+
buttonshadow: "#808080",
|
|
188
|
+
buttontext: "#000000",
|
|
189
|
+
captiontext: "#000000",
|
|
190
|
+
graytext: "#808080",
|
|
191
|
+
highlight: "#b3d4fc",
|
|
192
|
+
highlighttext: "#000000",
|
|
193
|
+
inactiveborder: "#d4d0c8",
|
|
194
|
+
inactivecaption: "#bfbbb0",
|
|
195
|
+
inactivecaptiontext: "#545454",
|
|
196
|
+
infobackground: "#fbfcc5",
|
|
197
|
+
infotext: "#000000",
|
|
198
|
+
menu: "#c0c0c0",
|
|
199
|
+
menutext: "#000000",
|
|
200
|
+
scrollbar: "#c0c0c0",
|
|
201
|
+
threeddarkshadow: "#696969",
|
|
202
|
+
threedface: "#c0c0c0",
|
|
203
|
+
threedhighlight: "#dfdfdf",
|
|
204
|
+
threedlightshadow: "#dcdcdc",
|
|
205
|
+
threedshadow: "#808080",
|
|
206
|
+
window: "#ffffff",
|
|
207
|
+
windowframe: "#646464",
|
|
208
|
+
windowtext: "#000000"
|
|
147
209
|
};
|
|
148
210
|
|
|
149
211
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
212
|
+
* jmGraph 工具类
|
|
213
|
+
*
|
|
214
|
+
* 提供常用的静态工具方法,包括对象操作、事件处理、几何计算、颜色转换等。
|
|
152
215
|
*
|
|
153
216
|
* @class jmUtils
|
|
154
217
|
* @static
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* // 克隆对象
|
|
221
|
+
* const newObj = jmUtils.clone({ a: 1, b: 2 });
|
|
222
|
+
*
|
|
223
|
+
* // 绑定事件
|
|
224
|
+
* jmUtils.bindEvent(element, 'click', handler);
|
|
225
|
+
*
|
|
226
|
+
* // 检查点是否在多边形内
|
|
227
|
+
* const inside = jmUtils.pointInPolygon({x: 10, y: 10}, polygonPoints);
|
|
155
228
|
*/
|
|
156
229
|
export default class jmUtils {
|
|
157
230
|
/**
|
|
@@ -307,12 +380,12 @@ export default class jmUtils {
|
|
|
307
380
|
el = el.offsetParent;
|
|
308
381
|
}
|
|
309
382
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
383
|
+
else if(el.x) {
|
|
384
|
+
pos.left += el.x;
|
|
385
|
+
}
|
|
386
|
+
else if(el.y){
|
|
387
|
+
pos.top += el.y;
|
|
388
|
+
}
|
|
316
389
|
return pos;
|
|
317
390
|
}
|
|
318
391
|
/**
|
|
@@ -436,6 +509,30 @@ export default class jmUtils {
|
|
|
436
509
|
return this.rayCasting(pt, polygon, offset);
|
|
437
510
|
}
|
|
438
511
|
|
|
512
|
+
/**
|
|
513
|
+
* 判断点是否在线段上
|
|
514
|
+
*
|
|
515
|
+
* 通过计算点到线段的垂直距离来判断点是否在线段上。
|
|
516
|
+
* 同时检查点是否在线段的范围内(不仅仅是直线上)。
|
|
517
|
+
*
|
|
518
|
+
* @method pointOnLine
|
|
519
|
+
* @static
|
|
520
|
+
* @private
|
|
521
|
+
* @param {Object} pt 待检测的点 {x, y}
|
|
522
|
+
* @param {Object} p1 线段起点 {x, y}
|
|
523
|
+
* @param {Object} p2 线段终点 {x, y}
|
|
524
|
+
* @param {number} offset 允许的偏差值(像素)
|
|
525
|
+
* @returns {number} 0=不在线段上, 1=在线段上
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
528
|
+
* const onLine = jmUtils.pointOnLine(
|
|
529
|
+
* {x: 5, y: 5}, // 待检测点
|
|
530
|
+
* {x: 0, y: 0}, // 起点
|
|
531
|
+
* {x: 10, y: 10}, // 终点
|
|
532
|
+
* 2 // 允许偏差
|
|
533
|
+
* );
|
|
534
|
+
* // 返回 1,点在对角线上
|
|
535
|
+
*/
|
|
439
536
|
static pointOnLine(pt, p1, p2, offset) {
|
|
440
537
|
const minX = Math.min(p1.x, p2.x);
|
|
441
538
|
const maxX = Math.max(p1.x, p2.x);
|
|
@@ -476,6 +573,25 @@ export default class jmUtils {
|
|
|
476
573
|
return 0;
|
|
477
574
|
}
|
|
478
575
|
|
|
576
|
+
/**
|
|
577
|
+
* 射线法判断点是否在多边形内部
|
|
578
|
+
*
|
|
579
|
+
* 从待测点向右发射一条水平射线,计算与多边形边界的交点数量。
|
|
580
|
+
* - 交点数为奇数:点在多边形内部
|
|
581
|
+
* - 交点数为偶数:点在多边形外部
|
|
582
|
+
*
|
|
583
|
+
* 这是判断点是否在任意多边形内的经典算法,时间复杂度 O(n)。
|
|
584
|
+
*
|
|
585
|
+
* @method rayCasting
|
|
586
|
+
* @static
|
|
587
|
+
* @private
|
|
588
|
+
* @param {Object} pt 待检测的点 {x, y}
|
|
589
|
+
* @param {Array<Object>} polygon 多边形顶点数组 [{x, y}, ...]
|
|
590
|
+
* @param {number} offset 允许的偏差值(未使用)
|
|
591
|
+
* @returns {number} 0=在多边形外部, 2=在多边形内部
|
|
592
|
+
*
|
|
593
|
+
* @see {@link https://en.wikipedia.org/wiki/Point_in_polygon Point in polygon - Wikipedia}
|
|
594
|
+
*/
|
|
479
595
|
static rayCasting(pt, polygon, offset) {
|
|
480
596
|
const n = polygon.length;
|
|
481
597
|
let inside = false;
|
|
@@ -849,8 +965,19 @@ export default class jmUtils {
|
|
|
849
965
|
}
|
|
850
966
|
|
|
851
967
|
/**
|
|
852
|
-
*
|
|
853
|
-
*
|
|
968
|
+
* 将 RGB 颜色值从 0-255 范围转换为 0-1 范围
|
|
969
|
+
*
|
|
970
|
+
* WebGL 中的颜色值通常使用 0-1 的浮点数表示,
|
|
971
|
+
* 此方法用于将标准 RGB 值转换为 WebGL 兼容格式。
|
|
972
|
+
*
|
|
973
|
+
* @method rgbToDecimal
|
|
974
|
+
* @static
|
|
975
|
+
* @param {Object} color 颜色对象 {r, g, b, a?}
|
|
976
|
+
* @returns {Object} 转换后的颜色对象 {r, g, b, a?},其中 r/g/b 为 0-1 范围
|
|
977
|
+
*
|
|
978
|
+
* @example
|
|
979
|
+
* const color = jmUtils.rgbToDecimal({ r: 255, g: 128, b: 64 });
|
|
980
|
+
* // 返回 { r: 1, g: 0.502, b: 0.251 }
|
|
854
981
|
*/
|
|
855
982
|
static rgbToDecimal(color) {
|
|
856
983
|
color = this.clone(color);
|
|
@@ -860,7 +987,15 @@ export default class jmUtils {
|
|
|
860
987
|
return color;
|
|
861
988
|
}
|
|
862
989
|
|
|
863
|
-
|
|
990
|
+
/**
|
|
991
|
+
* 将字节值(0-255)转换为小数(0-1)
|
|
992
|
+
*
|
|
993
|
+
* @method byteToDecimal
|
|
994
|
+
* @static
|
|
995
|
+
* @private
|
|
996
|
+
* @param {number} b 字节值(0-255)
|
|
997
|
+
* @returns {number} 小数值(0-1)
|
|
998
|
+
*/
|
|
864
999
|
static byteToDecimal(b) {
|
|
865
1000
|
return b / 255;
|
|
866
1001
|
}
|
|
@@ -903,14 +1038,52 @@ export default class jmUtils {
|
|
|
903
1038
|
}
|
|
904
1039
|
return r;
|
|
905
1040
|
}
|
|
906
|
-
|
|
1041
|
+
/**
|
|
1042
|
+
* 请求动画帧
|
|
1043
|
+
*
|
|
1044
|
+
* 封装浏览器原生的 requestAnimationFrame 方法,提供跨浏览器兼容性。
|
|
1045
|
+
* 在不支持 requestAnimationFrame 的环境中降级为 setTimeout。
|
|
1046
|
+
*
|
|
1047
|
+
* @method requestAnimationFrame
|
|
1048
|
+
* @static
|
|
1049
|
+
* @param {Function} callback 动画帧回调函数,接收时间戳参数
|
|
1050
|
+
* @param {Window} [win] 可选的窗口对象(用于多窗口环境)
|
|
1051
|
+
* @returns {number} 动画帧请求ID,用于取消
|
|
1052
|
+
*
|
|
1053
|
+
* @example
|
|
1054
|
+
* let animationId;
|
|
1055
|
+
* function animate(timestamp) {
|
|
1056
|
+
* // 更新动画
|
|
1057
|
+
* animationId = jmUtils.requestAnimationFrame(animate);
|
|
1058
|
+
* }
|
|
1059
|
+
* animationId = jmUtils.requestAnimationFrame(animate);
|
|
1060
|
+
*
|
|
1061
|
+
* // 取消动画
|
|
1062
|
+
* jmUtils.cancelAnimationFrame(animationId);
|
|
1063
|
+
*/
|
|
907
1064
|
static requestAnimationFrame(callback, win) {
|
|
908
1065
|
let fun = win && win.requestAnimationFrame? win.requestAnimationFrame: (typeof window !== 'undefined' && window.requestAnimationFrame? window.requestAnimationFrame: setTimeout);
|
|
909
1066
|
return fun(callback, 20);
|
|
910
1067
|
}
|
|
1068
|
+
/**
|
|
1069
|
+
* 取消动画帧请求
|
|
1070
|
+
*
|
|
1071
|
+
* 取消之前通过 requestAnimationFrame 注册的回调。
|
|
1072
|
+
* 在不支持 cancelAnimationFrame 的环境中降级为 clearTimeout。
|
|
1073
|
+
*
|
|
1074
|
+
* @method cancelAnimationFrame
|
|
1075
|
+
* @static
|
|
1076
|
+
* @param {number} handler requestAnimationFrame 返回的请求ID
|
|
1077
|
+
* @param {Window} [win] 可选的窗口对象(用于多窗口环境)
|
|
1078
|
+
*
|
|
1079
|
+
* @example
|
|
1080
|
+
* const animationId = jmUtils.requestAnimationFrame(animate);
|
|
1081
|
+
* jmUtils.cancelAnimationFrame(animationId);
|
|
1082
|
+
*/
|
|
911
1083
|
static cancelAnimationFrame(handler, win) {
|
|
912
1084
|
let fun = win && win.cancelAnimationFrame? win.cancelAnimationFrame: (typeof window !== 'undefined' && window.cancelAnimationFrame? window.cancelAnimationFrame: clearTimeout);
|
|
913
1085
|
return fun(handler);
|
|
914
1086
|
}
|
|
915
1087
|
}
|
|
916
|
-
export { jmUtils };
|
|
1088
|
+
export { jmUtils };
|
|
1089
|
+
export { colorKeywords };
|