jmgraph 3.2.27 → 3.2.29
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/dist/jmgraph.core.min.js +1 -1
- package/dist/jmgraph.core.min.js.map +1 -1
- package/dist/jmgraph.js +2657 -415
- package/dist/jmgraph.min.js +1 -1
- package/package.json +1 -1
- package/src/core/jmControl.js +824 -127
- package/src/core/jmEvents.js +154 -0
- package/src/core/jmFilter.js +38 -1
- package/src/core/jmGradient.js +47 -2
- 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 +149 -12
- 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
|
@@ -1,29 +1,49 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview WebGL 类型到 GLSL 类型映射模块
|
|
4
|
+
*
|
|
5
|
+
* 本模块提供了 WebGL 常量类型到 GLSL 类型名的映射。
|
|
6
|
+
*
|
|
7
|
+
* @module lib/webgl/core/mapType
|
|
8
|
+
* @author jmGraph Team
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** @type {Object.<number, string>|null} 缓存的类型映射表 */
|
|
2
12
|
var GL_TABLE = null;
|
|
3
13
|
|
|
14
|
+
/**
|
|
15
|
+
* WebGL 常量到 GLSL 类型的映射表
|
|
16
|
+
* @constant {Object.<string, string>}
|
|
17
|
+
*/
|
|
4
18
|
const GL_TO_GLSL_TYPES = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
19
|
+
'FLOAT': 'float',
|
|
20
|
+
'FLOAT_VEC2': 'vec2',
|
|
21
|
+
'FLOAT_VEC3': 'vec3',
|
|
22
|
+
'FLOAT_VEC4': 'vec4',
|
|
9
23
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
'INT': 'int',
|
|
25
|
+
'INT_VEC2': 'ivec2',
|
|
26
|
+
'INT_VEC3': 'ivec3',
|
|
27
|
+
'INT_VEC4': 'ivec4',
|
|
28
|
+
|
|
29
|
+
'BOOL': 'bool',
|
|
30
|
+
'BOOL_VEC2': 'bvec2',
|
|
31
|
+
'BOOL_VEC3': 'bvec3',
|
|
32
|
+
'BOOL_VEC4': 'bvec4',
|
|
33
|
+
|
|
34
|
+
'FLOAT_MAT2': 'mat2',
|
|
35
|
+
'FLOAT_MAT3': 'mat3',
|
|
36
|
+
'FLOAT_MAT4': 'mat4',
|
|
37
|
+
|
|
38
|
+
'SAMPLER_2D': 'sampler2D'
|
|
25
39
|
};
|
|
26
40
|
|
|
41
|
+
/**
|
|
42
|
+
* 将 WebGL 类型常量映射为 GLSL 类型名
|
|
43
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
44
|
+
* @param {number} type WebGL 类型常量
|
|
45
|
+
* @returns {string} GLSL 类型名
|
|
46
|
+
*/
|
|
27
47
|
const mapType = function(gl, type) {
|
|
28
48
|
if(!GL_TABLE) {
|
|
29
49
|
const typeNames = Object.keys(GL_TO_GLSL_TYPES);
|
|
@@ -34,10 +54,9 @@ const mapType = function(gl, type) {
|
|
|
34
54
|
}
|
|
35
55
|
}
|
|
36
56
|
|
|
37
|
-
|
|
57
|
+
return GL_TABLE[type];
|
|
38
58
|
};
|
|
39
59
|
|
|
40
|
-
|
|
41
60
|
export {
|
|
42
|
-
|
|
61
|
+
mapType
|
|
43
62
|
}
|
|
@@ -1,32 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview WebGL 着色器程序管理模块
|
|
3
|
+
*
|
|
4
|
+
* 本模块提供了 WebGL 着色器程序的创建和管理功能,包括:
|
|
5
|
+
* - 创建着色器程序
|
|
6
|
+
* - 提取属性和 uniform 变量
|
|
7
|
+
* - 顶点属性绑定
|
|
8
|
+
*
|
|
9
|
+
* @module lib/webgl/core/program
|
|
10
|
+
* @author jmGraph Team
|
|
11
|
+
*/
|
|
12
|
+
import { createShader } from './shader.js';
|
|
13
|
+
import { mapSize } from './mapSize.js';
|
|
14
|
+
import { mapType } from './mapType.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 创建着色器程序
|
|
18
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
19
|
+
* @param {string} vertexSrc 顶点着色器源码
|
|
20
|
+
* @param {string} fragmentSrc 片段着色器源码
|
|
21
|
+
* @returns {Object} 程序对象 {program, attrs, uniforms}
|
|
22
|
+
*/
|
|
12
23
|
function createProgram(gl, vertexSrc, fragmentSrc) {
|
|
13
|
-
// 创建顶点着色器
|
|
14
24
|
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSrc);
|
|
15
|
-
// 创建片段着色器
|
|
16
25
|
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSrc);
|
|
17
26
|
|
|
18
|
-
const program = gl.createProgram()
|
|
19
|
-
gl.attachShader(program, vertexShader)
|
|
20
|
-
gl.attachShader(program, fragmentShader)
|
|
21
|
-
gl.linkProgram(program)
|
|
27
|
+
const program = gl.createProgram();
|
|
28
|
+
gl.attachShader(program, vertexShader);
|
|
29
|
+
gl.attachShader(program, fragmentShader);
|
|
30
|
+
gl.linkProgram(program);
|
|
22
31
|
|
|
23
|
-
// 检查程序链接状态
|
|
24
32
|
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
25
33
|
console.error('PError: Could not initialize shader.');
|
|
26
34
|
console.error('gl.VALIDATE_STATUS', gl.getProgramParameter(program, gl.VALIDATE_STATUS));
|
|
27
35
|
console.error('gl.getError()', gl.getError());
|
|
28
36
|
|
|
29
|
-
// if there is a program info log, log it
|
|
30
37
|
if (gl.getProgramInfoLog(program) !== '') {
|
|
31
38
|
console.warn('Warning: gl.getProgramInfoLog()', gl.getProgramInfoLog(program));
|
|
32
39
|
}
|
|
@@ -36,28 +43,32 @@ function createProgram(gl, vertexSrc, fragmentSrc) {
|
|
|
36
43
|
|
|
37
44
|
useProgram(gl, program);
|
|
38
45
|
|
|
39
|
-
// clean up some shaders
|
|
40
46
|
gl.deleteShader(vertexShader);
|
|
41
47
|
gl.deleteShader(fragmentShader);
|
|
42
48
|
|
|
43
49
|
const attrs = extractAttributes(gl, program);
|
|
44
50
|
const uniforms = extractUniforms(gl, program);
|
|
45
51
|
|
|
46
|
-
return {
|
|
47
|
-
program,
|
|
48
|
-
attrs,
|
|
49
|
-
uniforms
|
|
50
|
-
};
|
|
52
|
+
return { program, attrs, uniforms };
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
/**
|
|
56
|
+
* 使用指定的着色器程序
|
|
57
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
58
|
+
* @param {WebGLProgram} program 着色器程序
|
|
59
|
+
*/
|
|
54
60
|
function useProgram(gl, program) {
|
|
55
|
-
return gl.useProgram(program);
|
|
61
|
+
return gl.useProgram(program);
|
|
56
62
|
}
|
|
57
63
|
|
|
64
|
+
/**
|
|
65
|
+
* 提取着色器程序中的所有属性
|
|
66
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
67
|
+
* @param {WebGLProgram} program 着色器程序
|
|
68
|
+
* @returns {Object} 属性对象字典
|
|
69
|
+
*/
|
|
58
70
|
function extractAttributes(gl, program) {
|
|
59
71
|
const attributes = {};
|
|
60
|
-
|
|
61
72
|
const count = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
|
|
62
73
|
|
|
63
74
|
for (let i = 0; i < count; i++){
|
|
@@ -74,55 +85,84 @@ function extractAttributes(gl, program) {
|
|
|
74
85
|
return attributes;
|
|
75
86
|
}
|
|
76
87
|
|
|
88
|
+
/**
|
|
89
|
+
* 提取着色器程序中的所有 uniform 变量
|
|
90
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
91
|
+
* @param {WebGLProgram} program 着色器程序
|
|
92
|
+
* @returns {Object} uniform 变量对象字典
|
|
93
|
+
*/
|
|
77
94
|
function extractUniforms(gl, program) {
|
|
78
|
-
|
|
79
|
-
|
|
95
|
+
const uniforms = {};
|
|
80
96
|
const count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
|
|
81
97
|
|
|
82
98
|
for (let i = 0; i < count; i++) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const type = mapType(gl, uniformData.type
|
|
99
|
+
const uniformData = gl.getActiveUniform(program, i);
|
|
100
|
+
const name = uniformData.name.replace(/\[.*?\]/, "");
|
|
101
|
+
const type = mapType(gl, uniformData.type);
|
|
86
102
|
|
|
87
|
-
|
|
103
|
+
uniforms[name] = {
|
|
88
104
|
uniformData,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
105
|
+
type: type,
|
|
106
|
+
size: uniformData.size,
|
|
107
|
+
location: gl.getUniformLocation(program, name),
|
|
108
|
+
};
|
|
93
109
|
}
|
|
94
110
|
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
111
|
+
return uniforms;
|
|
112
|
+
}
|
|
98
113
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
114
|
+
/**
|
|
115
|
+
* 将缓冲区数据写入顶点属性
|
|
116
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
117
|
+
* @param {Object} buffer 缓冲区对象
|
|
118
|
+
* @param {Object} attr 属性对象
|
|
119
|
+
* @param {number} [size=2] 每个顶点的分量数(1-4)
|
|
120
|
+
* @param {number} [strip=0] 步长,0 表示紧密排列
|
|
121
|
+
* @param {number} [offset=0] 字节偏移量
|
|
122
|
+
* @param {number} [dataType=gl.FLOAT] 数据类型
|
|
123
|
+
* @returns {Object} 缓冲区对象
|
|
124
|
+
*/
|
|
104
125
|
function writeVertexAttrib(gl, buffer, attr, size=2, strip=0, offset=0, dataType=gl.FLOAT) {
|
|
105
126
|
gl.bindBuffer(buffer.type, buffer.buffer);
|
|
106
|
-
gl.vertexAttribPointer(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
127
|
+
gl.vertexAttribPointer(
|
|
128
|
+
attr.location,
|
|
129
|
+
size,
|
|
130
|
+
dataType,
|
|
131
|
+
false,
|
|
132
|
+
strip * buffer.unitSize,
|
|
133
|
+
offset
|
|
134
|
+
);
|
|
114
135
|
gl.enableVertexAttribArray(attr.location);
|
|
115
136
|
return buffer;
|
|
116
137
|
}
|
|
117
138
|
|
|
139
|
+
/**
|
|
140
|
+
* 禁用顶点属性数组
|
|
141
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
142
|
+
* @param {Object} attr 属性对象
|
|
143
|
+
*/
|
|
118
144
|
function disableVertexAttribArray(gl, attr) {
|
|
119
145
|
return gl.disableVertexAttribArray(attr.location);
|
|
120
146
|
}
|
|
121
147
|
|
|
148
|
+
/**
|
|
149
|
+
* 获取属性位置
|
|
150
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
151
|
+
* @param {WebGLProgram} program 着色器程序
|
|
152
|
+
* @param {string} name 属性名
|
|
153
|
+
* @returns {number} 属性位置
|
|
154
|
+
*/
|
|
122
155
|
function getAttribLocation(gl, program, name) {
|
|
123
156
|
return gl.getAttribLocation(program, name);
|
|
124
157
|
}
|
|
125
158
|
|
|
159
|
+
/**
|
|
160
|
+
* 获取 uniform 位置
|
|
161
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
162
|
+
* @param {WebGLProgram} program 着色器程序
|
|
163
|
+
* @param {string} name uniform 变量名
|
|
164
|
+
* @returns {WebGLUniformLocation} uniform 位置
|
|
165
|
+
*/
|
|
126
166
|
function getUniformLocation(gl, program, name) {
|
|
127
167
|
return gl.getUniformLocation(program, name);
|
|
128
168
|
}
|
|
@@ -1,14 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview WebGL 着色器管理模块
|
|
3
|
+
*
|
|
4
|
+
* 本模块提供了 WebGL 着色器的创建功能。
|
|
5
|
+
*
|
|
6
|
+
* @module lib/webgl/core/shader
|
|
7
|
+
* @author jmGraph Team
|
|
8
|
+
*/
|
|
8
9
|
|
|
10
|
+
/**
|
|
11
|
+
* 创建 WebGL 着色器
|
|
12
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
13
|
+
* @param {number} type 着色器类型:gl.VERTEX_SHADER 或 gl.FRAGMENT_SHADER
|
|
14
|
+
* @param {string} src 着色器源码
|
|
15
|
+
* @returns {WebGLShader} 编译后的着色器对象
|
|
16
|
+
*/
|
|
17
|
+
function createShader(gl, type, src) {
|
|
18
|
+
const shader = gl.createShader(type);
|
|
19
|
+
gl.shaderSource(shader, src);
|
|
20
|
+
gl.compileShader(shader);
|
|
9
21
|
return shader;
|
|
10
22
|
}
|
|
11
23
|
|
|
12
|
-
export
|
|
24
|
+
export {
|
|
13
25
|
createShader
|
|
14
26
|
}
|
|
@@ -1,54 +1,77 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview WebGL 纹理管理模块
|
|
4
|
+
*
|
|
5
|
+
* 本模块提供了 WebGL 纹理的创建和管理功能,包括:
|
|
6
|
+
* - 创建 2D 纹理
|
|
7
|
+
* - 创建图片纹理
|
|
8
|
+
* - 创建数据纹理
|
|
9
|
+
* - 删除纹理
|
|
10
|
+
*
|
|
11
|
+
* @module lib/webgl/core/texture
|
|
12
|
+
* @author jmGraph Team
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 创建 2D 纹理
|
|
17
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
18
|
+
* @returns {WebGLTexture} 纹理对象
|
|
19
|
+
*/
|
|
3
20
|
function create2DTexture(gl) {
|
|
4
21
|
const texture = gl.createTexture();
|
|
5
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
|
|
6
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
7
|
-
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
22
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
|
|
23
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
24
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
8
25
|
|
|
9
|
-
|
|
10
|
-
gl.texParameterf(gl.TEXTURE_2D, gl.
|
|
11
|
-
gl.texParameterf(gl.TEXTURE_2D, gl.
|
|
12
|
-
gl.texParameterf(gl.TEXTURE_2D, gl.
|
|
13
|
-
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 竖直平铺方式
|
|
14
|
-
|
|
26
|
+
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
27
|
+
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
28
|
+
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
29
|
+
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
15
30
|
|
|
16
31
|
return texture;
|
|
17
32
|
}
|
|
18
33
|
|
|
19
|
-
|
|
34
|
+
/**
|
|
35
|
+
* 创建图片纹理
|
|
36
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
37
|
+
* @param {Image|HTMLImageElement} img 图像对象
|
|
38
|
+
* @returns {Object} 纹理对象 {texture}
|
|
39
|
+
*/
|
|
20
40
|
function createImgTexture(gl, img) {
|
|
21
41
|
const texture = create2DTexture(gl);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
texture
|
|
26
|
-
};
|
|
42
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
|
|
43
|
+
return { texture };
|
|
27
44
|
}
|
|
28
45
|
|
|
29
|
-
|
|
46
|
+
/**
|
|
47
|
+
* 根据像素数据创建纹理
|
|
48
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
49
|
+
* @param {ImageData|Object} pixels 像素数据 {data, width, height}
|
|
50
|
+
* @returns {Object} 纹理对象 {texture}
|
|
51
|
+
*/
|
|
30
52
|
function createDataTexture(gl, pixels) {
|
|
31
53
|
const data = new Uint8Array(pixels.data || pixels);
|
|
32
|
-
|
|
33
54
|
const texture = create2DTexture(gl);
|
|
34
55
|
|
|
35
56
|
gl.texImage2D(
|
|
36
|
-
gl.TEXTURE_2D,
|
|
37
|
-
0,
|
|
38
|
-
gl.RGBA,
|
|
39
|
-
pixels.width || 1,
|
|
40
|
-
pixels.height || 1,
|
|
41
|
-
0,
|
|
42
|
-
gl.RGBA,
|
|
43
|
-
gl.UNSIGNED_BYTE,
|
|
44
|
-
data
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
texture
|
|
48
|
-
};
|
|
57
|
+
gl.TEXTURE_2D,
|
|
58
|
+
0,
|
|
59
|
+
gl.RGBA,
|
|
60
|
+
pixels.width || 1,
|
|
61
|
+
pixels.height || 1,
|
|
62
|
+
0,
|
|
63
|
+
gl.RGBA,
|
|
64
|
+
gl.UNSIGNED_BYTE,
|
|
65
|
+
data
|
|
66
|
+
);
|
|
67
|
+
return { texture };
|
|
49
68
|
}
|
|
50
69
|
|
|
51
|
-
|
|
70
|
+
/**
|
|
71
|
+
* 删除纹理
|
|
72
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
73
|
+
* @param {WebGLTexture} texture 纹理对象
|
|
74
|
+
*/
|
|
52
75
|
function deleteTexture(gl, texture) {
|
|
53
76
|
return gl.deleteTexture(texture);
|
|
54
77
|
}
|
|
@@ -1,11 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview WebGL 渐变对象
|
|
3
|
+
*
|
|
4
|
+
* 本模块提供了 WebGL 渐变功能,支持 GLSL 着色器直接计算渐变色,
|
|
5
|
+
* 无需 textureCanvas,性能更优。
|
|
6
|
+
*
|
|
7
|
+
* 支持的渐变类型:
|
|
8
|
+
* - 线性渐变 (linear)
|
|
9
|
+
* - 径向渐变 (radial)
|
|
10
|
+
*
|
|
11
|
+
* @module lib/webgl/gradient
|
|
12
|
+
* @author jmGraph Team
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** @constant {number} 最大颜色断点数量 */
|
|
1
16
|
const MAX_STOPS = 16;
|
|
2
17
|
|
|
3
18
|
/**
|
|
4
|
-
* WebGL
|
|
5
|
-
* 支持 GLSL
|
|
19
|
+
* WebGL 渐变类
|
|
20
|
+
* 支持 GLSL 着色器直接计算渐变色
|
|
21
|
+
*
|
|
22
|
+
* @class WebglGradient
|
|
23
|
+
* @example
|
|
24
|
+
* const gradient = new WebglGradient('linear', { x1: 0, y1: 0, x2: 100, y2: 0 });
|
|
25
|
+
* gradient.addColorStop(0, '#ff0000');
|
|
26
|
+
* gradient.addColorStop(1, '#0000ff');
|
|
6
27
|
*/
|
|
7
28
|
class WebglGradient {
|
|
29
|
+
/**
|
|
30
|
+
* 构造函数
|
|
31
|
+
* @param {string} [type='linear'] 渐变类型:'linear' 或 'radial'
|
|
32
|
+
* @param {Object} params 渐变参数
|
|
33
|
+
* @param {number} [params.x1=0] 起点/内圆中心X坐标
|
|
34
|
+
* @param {number} [params.y1=0] 起点/内圆中心Y坐标
|
|
35
|
+
* @param {number} [params.r1=0] 内圆半径(径向渐变)
|
|
36
|
+
* @param {number} [params.x2=0] 终点/外圆中心X坐标
|
|
37
|
+
* @param {number} [params.y2=0] 终点/外圆中心Y坐标
|
|
38
|
+
* @param {number} [params.r2=0] 外圆半径(径向渐变)
|
|
39
|
+
* @param {Object} [params.bounds] 渐变边界
|
|
40
|
+
* @param {Object} [params.control] 控制器对象
|
|
41
|
+
*/
|
|
8
42
|
constructor(type = 'linear', params = {}) {
|
|
43
|
+
/** @type {string} 渐变类型 */
|
|
9
44
|
this.type = type || 'linear';
|
|
10
45
|
|
|
11
46
|
this.x1 = params.x1 || 0;
|
|
@@ -15,15 +50,12 @@ class WebglGradient {
|
|
|
15
50
|
this.y2 = params.y2 || 0;
|
|
16
51
|
this.r2 = params.r2 || 0;
|
|
17
52
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
top: 0,
|
|
21
|
-
width: 0,
|
|
22
|
-
height: 0
|
|
23
|
-
};
|
|
53
|
+
/** @type {Object} 渐变边界 */
|
|
54
|
+
this.bounds = params.bounds || { left: 0, top: 0, width: 0, height: 0 };
|
|
24
55
|
|
|
25
56
|
this.control = params.control;
|
|
26
57
|
|
|
58
|
+
/** @type {Array<{offset: number, color: string}>} 颜色断点数组 */
|
|
27
59
|
this.stops = [];
|
|
28
60
|
this._sortedStops = null;
|
|
29
61
|
this._paramsHash = null;
|
|
@@ -31,6 +63,8 @@ class WebglGradient {
|
|
|
31
63
|
|
|
32
64
|
/**
|
|
33
65
|
* 添加颜色断点
|
|
66
|
+
* @param {number} offset 断点位置 (0-1)
|
|
67
|
+
* @param {string} color 颜色值
|
|
34
68
|
*/
|
|
35
69
|
addColorStop(offset, color) {
|
|
36
70
|
this.stops.push({
|
|
@@ -42,7 +76,9 @@ class WebglGradient {
|
|
|
42
76
|
}
|
|
43
77
|
|
|
44
78
|
/**
|
|
45
|
-
*
|
|
79
|
+
* 获取排序后的断点数组(带解析后的颜色)
|
|
80
|
+
* @private
|
|
81
|
+
* @returns {Array<{offset: number, r: number, g: number, b: number, a: number}>}
|
|
46
82
|
*/
|
|
47
83
|
_getSortedStops() {
|
|
48
84
|
if (this._sortedStops) return this._sortedStops;
|
|
@@ -55,8 +91,6 @@ class WebglGradient {
|
|
|
55
91
|
c = utils.hexToRGBA(c);
|
|
56
92
|
}
|
|
57
93
|
if (typeof c === 'object' && c !== null) {
|
|
58
|
-
// hexToRGBA 返回 r/g/b 为 0~255,a 为 0~1
|
|
59
|
-
// 但如果已经是 0~1 范围(由 rgbToDecimal 处理过),需要检测
|
|
60
94
|
const needNormalize = (c.r > 1 || c.g > 1 || c.b > 1) ? 255 : 1;
|
|
61
95
|
return {
|
|
62
96
|
offset: s.offset,
|
|
@@ -74,14 +108,13 @@ class WebglGradient {
|
|
|
74
108
|
}
|
|
75
109
|
|
|
76
110
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
111
|
+
* 将渐变参数转换为 uniform 格式,传递给着色器
|
|
112
|
+
* @returns {Object} uniform 参数对象
|
|
79
113
|
*/
|
|
80
114
|
toUniformParams() {
|
|
81
115
|
const stops = this._getSortedStops();
|
|
82
116
|
const count = Math.min(stops.length, MAX_STOPS);
|
|
83
117
|
|
|
84
|
-
// 展平为 Float32Array: [offset, r, g, b, a, ...]
|
|
85
118
|
const flatStops = new Float32Array(count * 5);
|
|
86
119
|
for (let i = 0; i < count; i++) {
|
|
87
120
|
const s = stops[i];
|
|
@@ -109,9 +142,7 @@ class WebglGradient {
|
|
|
109
142
|
};
|
|
110
143
|
}
|
|
111
144
|
|
|
112
|
-
/**
|
|
113
|
-
* 使缓存失效
|
|
114
|
-
*/
|
|
145
|
+
/** 使缓存失效 */
|
|
115
146
|
invalidateCache() {
|
|
116
147
|
this._sortedStops = null;
|
|
117
148
|
this._paramsHash = null;
|
|
@@ -119,6 +150,7 @@ class WebglGradient {
|
|
|
119
150
|
|
|
120
151
|
/**
|
|
121
152
|
* 转换为渐变的字符串表达
|
|
153
|
+
* @returns {string} 渐变字符串
|
|
122
154
|
*/
|
|
123
155
|
toString() {
|
|
124
156
|
let str = this.type + '-gradient(';
|