jmgraph 3.2.27 → 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/dist/jmgraph.core.min.js +1 -1
- package/dist/jmgraph.core.min.js.map +1 -1
- package/dist/jmgraph.js +2661 -415
- package/dist/jmgraph.min.js +1 -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 +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
package/src/lib/webgl/base.js
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview WebGL 基础渲染类
|
|
4
|
+
*
|
|
5
|
+
* 本模块提供了 WebGL 渲染的核心功能,包括:
|
|
6
|
+
* - 着色器程序管理
|
|
7
|
+
* - 缓冲区管理
|
|
8
|
+
* - 纹理管理
|
|
9
|
+
* - 变换矩阵操作
|
|
10
|
+
* - 多边形三角化(使用 earcut 算法)
|
|
11
|
+
* - 渐变支持
|
|
12
|
+
*
|
|
13
|
+
* @module lib/webgl/base
|
|
14
|
+
* @author jmGraph Team
|
|
15
|
+
*/
|
|
2
16
|
import earcut from '../earcut.js';
|
|
3
17
|
import webglGradient, { MAX_STOPS } from './gradient.js';
|
|
4
18
|
import {
|
|
@@ -152,7 +166,21 @@ const pathFragmentSource = `
|
|
|
152
166
|
}
|
|
153
167
|
`;
|
|
154
168
|
|
|
169
|
+
/**
|
|
170
|
+
* WebGL 基础渲染类
|
|
171
|
+
* 提供 WebGL 渲染的核心功能,包括着色器、缓冲区、纹理管理等
|
|
172
|
+
*
|
|
173
|
+
* @class WeblBase
|
|
174
|
+
* @example
|
|
175
|
+
* const base = new WeblBase(graph, { mode: 'webgl' });
|
|
176
|
+
* base.setStyle({ fillStyle: '#ff0000' });
|
|
177
|
+
*/
|
|
155
178
|
class WeblBase {
|
|
179
|
+
/**
|
|
180
|
+
* 构造函数
|
|
181
|
+
* @param {jmGraph} graph jmGraph 实例
|
|
182
|
+
* @param {Object} option 配置选项
|
|
183
|
+
*/
|
|
156
184
|
constructor(graph, option) {
|
|
157
185
|
this.graph = graph;
|
|
158
186
|
this.option = option || {};
|
|
@@ -160,14 +188,16 @@ class WeblBase {
|
|
|
160
188
|
globalAlpha: 1
|
|
161
189
|
};
|
|
162
190
|
this.stateStack = [];
|
|
163
|
-
|
|
191
|
+
/** @type {number[]} 2D 变换矩阵 [a, b, c, d, tx, ty] */
|
|
192
|
+
this.transformMatrix = [1, 0, 0, 1, 0, 0];
|
|
164
193
|
}
|
|
165
194
|
|
|
195
|
+
/** @returns {WebGLRenderingContext} WebGL 渲染上下文 */
|
|
166
196
|
get context() {
|
|
167
197
|
if(this.graph) return this.graph.context;
|
|
168
198
|
}
|
|
169
199
|
|
|
170
|
-
|
|
200
|
+
/** 保存当前状态到状态栈 */
|
|
171
201
|
save() {
|
|
172
202
|
this.stateStack.push({
|
|
173
203
|
transformMatrix: [...this.transformMatrix],
|
|
@@ -175,7 +205,7 @@ class WeblBase {
|
|
|
175
205
|
});
|
|
176
206
|
}
|
|
177
207
|
|
|
178
|
-
|
|
208
|
+
/** 从状态栈恢复上一个状态 */
|
|
179
209
|
restore() {
|
|
180
210
|
if (this.stateStack.length > 0) {
|
|
181
211
|
const state = this.stateStack.pop();
|
|
@@ -184,40 +214,53 @@ class WeblBase {
|
|
|
184
214
|
}
|
|
185
215
|
}
|
|
186
216
|
|
|
187
|
-
|
|
217
|
+
/**
|
|
218
|
+
* 平移变换
|
|
219
|
+
* @param {number} x X 轴平移量
|
|
220
|
+
* @param {number} y Y 轴平移量
|
|
221
|
+
*/
|
|
188
222
|
translate(x, y) {
|
|
189
|
-
// 更新变换矩阵
|
|
190
223
|
this.transformMatrix[4] += x * this.transformMatrix[0] + y * this.transformMatrix[2];
|
|
191
224
|
this.transformMatrix[5] += x * this.transformMatrix[1] + y * this.transformMatrix[3];
|
|
192
225
|
}
|
|
193
226
|
|
|
194
|
-
|
|
227
|
+
/**
|
|
228
|
+
* 缩放变换
|
|
229
|
+
* @param {number} sx X 轴缩放比例
|
|
230
|
+
* @param {number} sy Y 轴缩放比例
|
|
231
|
+
*/
|
|
195
232
|
scale(sx, sy) {
|
|
196
|
-
// 更新变换矩阵
|
|
197
233
|
this.transformMatrix[0] *= sx;
|
|
198
234
|
this.transformMatrix[1] *= sx;
|
|
199
235
|
this.transformMatrix[2] *= sy;
|
|
200
236
|
this.transformMatrix[3] *= sy;
|
|
201
237
|
}
|
|
202
238
|
|
|
203
|
-
|
|
239
|
+
/**
|
|
240
|
+
* 旋转变换
|
|
241
|
+
* @param {number} angle 旋转角度(弧度)
|
|
242
|
+
*/
|
|
204
243
|
rotate(angle) {
|
|
205
244
|
const cos = Math.cos(angle);
|
|
206
245
|
const sin = Math.sin(angle);
|
|
207
246
|
const [a, b, c, d] = this.transformMatrix;
|
|
208
|
-
|
|
209
|
-
// 更新变换矩阵
|
|
210
247
|
this.transformMatrix[0] = a * cos - b * sin;
|
|
211
248
|
this.transformMatrix[1] = a * sin + b * cos;
|
|
212
249
|
this.transformMatrix[2] = c * cos - d * sin;
|
|
213
250
|
this.transformMatrix[3] = c * sin + d * cos;
|
|
214
251
|
}
|
|
215
252
|
|
|
216
|
-
|
|
253
|
+
/**
|
|
254
|
+
* 矩阵变换
|
|
255
|
+
* @param {number} a 水平缩放
|
|
256
|
+
* @param {number} b 垂直倾斜
|
|
257
|
+
* @param {number} c 水平倾斜
|
|
258
|
+
* @param {number} d 垂直缩放
|
|
259
|
+
* @param {number} e 水平移动
|
|
260
|
+
* @param {number} f 垂直移动
|
|
261
|
+
*/
|
|
217
262
|
transform(a, b, c, d, e, f) {
|
|
218
263
|
const [currentA, currentB, currentC, currentD, currentE, currentF] = this.transformMatrix;
|
|
219
|
-
|
|
220
|
-
// 矩阵乘法
|
|
221
264
|
this.transformMatrix[0] = a * currentA + b * currentC;
|
|
222
265
|
this.transformMatrix[1] = a * currentB + b * currentD;
|
|
223
266
|
this.transformMatrix[2] = c * currentA + d * currentC;
|
|
@@ -226,7 +269,11 @@ class WeblBase {
|
|
|
226
269
|
this.transformMatrix[5] = e * currentB + f * currentD + currentF;
|
|
227
270
|
}
|
|
228
271
|
|
|
229
|
-
|
|
272
|
+
/**
|
|
273
|
+
* 应用变换到点
|
|
274
|
+
* @param {Object} point 点坐标 {x, y}
|
|
275
|
+
* @returns {Object} 变换后的点坐标 {x, y}
|
|
276
|
+
*/
|
|
230
277
|
applyTransform(point) {
|
|
231
278
|
const [a, b, c, d, tx, ty] = this.transformMatrix;
|
|
232
279
|
return {
|
|
@@ -235,7 +282,11 @@ class WeblBase {
|
|
|
235
282
|
};
|
|
236
283
|
}
|
|
237
284
|
|
|
238
|
-
|
|
285
|
+
/**
|
|
286
|
+
* 文本测量用的离屏 canvas context
|
|
287
|
+
* @private
|
|
288
|
+
* @returns {CanvasRenderingContext2D|null}
|
|
289
|
+
*/
|
|
239
290
|
get _measureCtx() {
|
|
240
291
|
if(!this.__measureCtx) {
|
|
241
292
|
try {
|
|
@@ -251,54 +302,40 @@ class WeblBase {
|
|
|
251
302
|
return this.__measureCtx;
|
|
252
303
|
}
|
|
253
304
|
|
|
254
|
-
|
|
305
|
+
/**
|
|
306
|
+
* 获取当前着色器程序
|
|
307
|
+
* @returns {Object} 着色器程序对象
|
|
308
|
+
*/
|
|
255
309
|
get program() {
|
|
256
|
-
// 默认所有path用同一个编译好的program
|
|
257
310
|
return this.graph.context.pathProgram || (this.graph.context.pathProgram=this.createProgram(pathVertexSource, pathFragmentSource));
|
|
258
311
|
}
|
|
259
312
|
|
|
260
|
-
|
|
313
|
+
/**
|
|
314
|
+
* 设置样式
|
|
315
|
+
* @param {Object|string} style 样式对象或样式属性名
|
|
316
|
+
* @param {string} [value] 样式值(当 style 为字符串时使用)
|
|
317
|
+
*/
|
|
261
318
|
setStyle(style = this.style, value = '') {
|
|
262
|
-
|
|
263
319
|
if(typeof style === 'string') {
|
|
264
320
|
const obj = {};
|
|
265
321
|
obj[style] = value;
|
|
266
322
|
style = obj;
|
|
267
323
|
}
|
|
268
|
-
/*
|
|
269
|
-
// 设置线条颜色或填充色
|
|
270
|
-
if(style.strokeStyle) {
|
|
271
|
-
let color = style.strokeStyle;
|
|
272
|
-
if(typeof color === 'string') color = this.graph.utils.hexToRGBA(color);
|
|
273
|
-
this.style.strokeStyle = this.graph.utils.rgbToDecimal(color);
|
|
274
|
-
delete style.strokeStyle;
|
|
275
|
-
}
|
|
276
|
-
else if(style.fillStyle) {
|
|
277
|
-
let color = style.fillStyle;
|
|
278
|
-
if(this.isGradient(color)) {
|
|
279
|
-
this.style.fillStyle = color;
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
if(typeof color === 'string') color = this.graph.utils.hexToRGBA(color);
|
|
283
|
-
this.style.fillStyle = this.graph.utils.rgbToDecimal(color);
|
|
284
|
-
}
|
|
285
|
-
delete style.fillStyle;
|
|
286
|
-
} */
|
|
287
|
-
|
|
288
324
|
this.style = {
|
|
289
325
|
...this.style,
|
|
290
326
|
...style
|
|
291
327
|
}
|
|
292
328
|
}
|
|
293
329
|
|
|
294
|
-
|
|
330
|
+
/**
|
|
331
|
+
* 将颜色转换为 WebGL 可识别的格式
|
|
332
|
+
* @param {string|Object} color 颜色值
|
|
333
|
+
* @returns {Object} RGBA 对象 {r, g, b, a},范围 0-1
|
|
334
|
+
*/
|
|
295
335
|
convertColor(color) {
|
|
296
336
|
if(this.isGradient(color)) return color;
|
|
297
337
|
if(typeof color === 'string') {
|
|
298
|
-
// 先尝试 hexToRGBA 解析
|
|
299
338
|
color = this.graph.utils.hexToRGBA(color);
|
|
300
|
-
// hexToRGBA 对无法识别的格式(如 hsl)会原样返回字符串
|
|
301
|
-
// 利用离屏 canvas 将任意 CSS 颜色转为 rgba
|
|
302
339
|
if(typeof color === 'string') {
|
|
303
340
|
color = this.__parseCSSColor(color);
|
|
304
341
|
}
|
|
@@ -309,7 +346,12 @@ class WeblBase {
|
|
|
309
346
|
return color;
|
|
310
347
|
}
|
|
311
348
|
|
|
312
|
-
|
|
349
|
+
/**
|
|
350
|
+
* 利用离屏 canvas 解析任意 CSS 颜色
|
|
351
|
+
* @private
|
|
352
|
+
* @param {string} colorStr CSS 颜色字符串
|
|
353
|
+
* @returns {Object} RGBA 对象 {r, g, b, a}
|
|
354
|
+
*/
|
|
313
355
|
__parseCSSColor(colorStr) {
|
|
314
356
|
const ctx = this._measureCtx;
|
|
315
357
|
if(!ctx) return { r: 0, g: 0, b: 0, a: 0 };
|
|
@@ -328,12 +370,21 @@ class WeblBase {
|
|
|
328
370
|
}
|
|
329
371
|
}
|
|
330
372
|
|
|
331
|
-
|
|
373
|
+
/**
|
|
374
|
+
* 创建着色器程序
|
|
375
|
+
* @param {string} vertexSrc 顶点着色器源码
|
|
376
|
+
* @param {string} fragmentSrc 片段着色器源码
|
|
377
|
+
* @returns {Object} 着色器程序对象
|
|
378
|
+
*/
|
|
332
379
|
createProgram(vertexSrc, fragmentSrc) {
|
|
333
380
|
return createProgram(this.context, vertexSrc, fragmentSrc);
|
|
334
381
|
}
|
|
335
382
|
|
|
336
|
-
|
|
383
|
+
/**
|
|
384
|
+
* 使用指定的着色器程序
|
|
385
|
+
* @param {Object} [program] 着色器程序,默认使用当前程序
|
|
386
|
+
* @returns {Object} 着色器程序
|
|
387
|
+
*/
|
|
337
388
|
useProgram(program=this.program) {
|
|
338
389
|
program = program.program || program;
|
|
339
390
|
if(this.context.__curent_program === program) return program;
|
|
@@ -342,26 +393,44 @@ class WeblBase {
|
|
|
342
393
|
return program;
|
|
343
394
|
}
|
|
344
395
|
|
|
396
|
+
/**
|
|
397
|
+
* 获取属性位置
|
|
398
|
+
* @param {string} name 属性名
|
|
399
|
+
* @returns {number} 属性位置
|
|
400
|
+
*/
|
|
345
401
|
getAttribLocation(name) {
|
|
346
402
|
return this.context.getAttribLocation(this.program.program, name);
|
|
347
403
|
}
|
|
348
404
|
|
|
405
|
+
/**
|
|
406
|
+
* 获取 uniform 位置
|
|
407
|
+
* @param {string} name uniform 变量名
|
|
408
|
+
* @returns {WebGLUniformLocation} uniform 位置
|
|
409
|
+
*/
|
|
349
410
|
getUniformLocation(name) {
|
|
350
411
|
return this.context.getUniformLocation(this.program.program, name);
|
|
351
412
|
}
|
|
352
413
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
414
|
+
/**
|
|
415
|
+
* 将缓冲区数据写入顶点属性
|
|
416
|
+
* @param {Object} buffer 缓冲区对象
|
|
417
|
+
* @param {Object} attr 属性对象
|
|
418
|
+
* @param {number} [size=2] 每个顶点的分量数(1-4)
|
|
419
|
+
* @param {number} [strip=0] 步长,0 表示紧密排列
|
|
420
|
+
* @param {number} [offset=0] 字节偏移量
|
|
421
|
+
* @param {number} [dataType=FLOAT] 数据类型
|
|
422
|
+
* @returns {Object} 缓冲区对象
|
|
423
|
+
*/
|
|
359
424
|
writeVertexAttrib(buffer, attr, size=2, strip=0, offset=0, dataType=this.context.FLOAT) {
|
|
360
425
|
buffer.attr = attr;
|
|
361
426
|
return writeVertexAttrib(this.context, buffer, attr, size, strip, offset, dataType);
|
|
362
427
|
}
|
|
363
428
|
|
|
364
|
-
|
|
429
|
+
/**
|
|
430
|
+
* 禁用顶点属性数组
|
|
431
|
+
* @param {Object} attr 属性对象
|
|
432
|
+
* @returns {Object} 属性对象
|
|
433
|
+
*/
|
|
365
434
|
disableVertexAttribArray(attr) {
|
|
366
435
|
try{
|
|
367
436
|
if(!attr) return attr;
|
|
@@ -373,24 +442,35 @@ class WeblBase {
|
|
|
373
442
|
return attr;
|
|
374
443
|
}
|
|
375
444
|
|
|
376
|
-
|
|
445
|
+
/**
|
|
446
|
+
* 创建 Float32 缓冲区
|
|
447
|
+
* @param {Array} data 数据数组
|
|
448
|
+
* @param {number} [type=ARRAY_BUFFER] 缓冲区类型
|
|
449
|
+
* @param {number} [drawType=STATIC_DRAW] 绘制类型
|
|
450
|
+
* @returns {Object} 缓冲区对象
|
|
451
|
+
*/
|
|
377
452
|
createFloat32Buffer(data, type=this.context.ARRAY_BUFFER, drawType=this.context.STATIC_DRAW) {
|
|
378
453
|
const buffer = createFloat32Buffer(this.context, data, type, drawType);
|
|
379
|
-
return {
|
|
380
|
-
data,
|
|
381
|
-
...buffer
|
|
382
|
-
};
|
|
454
|
+
return { data, ...buffer };
|
|
383
455
|
}
|
|
384
456
|
|
|
457
|
+
/**
|
|
458
|
+
* 创建 Uint16 缓冲区
|
|
459
|
+
* @param {Array} data 数据数组
|
|
460
|
+
* @param {number} [type=ARRAY_BUFFER] 缓冲区类型
|
|
461
|
+
* @param {number} [drawType=STATIC_DRAW] 绘制类型
|
|
462
|
+
* @returns {Object} 缓冲区对象
|
|
463
|
+
*/
|
|
385
464
|
createUint16Buffer(data, type=this.context.ARRAY_BUFFER, drawType=this.context.STATIC_DRAW) {
|
|
386
465
|
const buffer = createUint16Buffer(this.context, data, type, drawType);
|
|
387
|
-
return {
|
|
388
|
-
data,
|
|
389
|
-
...buffer
|
|
390
|
-
};
|
|
466
|
+
return { data, ...buffer };
|
|
391
467
|
}
|
|
392
468
|
|
|
393
|
-
|
|
469
|
+
/**
|
|
470
|
+
* 删除缓冲区
|
|
471
|
+
* @param {Object} buffer 缓冲区对象
|
|
472
|
+
* @returns {Object} 缓冲区对象
|
|
473
|
+
*/
|
|
394
474
|
deleteBuffer(buffer) {
|
|
395
475
|
try {
|
|
396
476
|
if(!buffer) return;
|
|
@@ -404,22 +484,34 @@ class WeblBase {
|
|
|
404
484
|
return buffer;
|
|
405
485
|
}
|
|
406
486
|
|
|
407
|
-
|
|
487
|
+
/** @returns {WebGLTexture} 2D 纹理对象 */
|
|
408
488
|
create2DTexture() {
|
|
409
489
|
return create2DTexture(this.context);
|
|
410
490
|
}
|
|
411
491
|
|
|
412
|
-
|
|
492
|
+
/**
|
|
493
|
+
* 创建图片纹理
|
|
494
|
+
* @param {Image|HTMLImageElement} img 图像对象
|
|
495
|
+
* @returns {Object} 纹理对象
|
|
496
|
+
*/
|
|
413
497
|
createImgTexture(img) {
|
|
414
498
|
return createImgTexture(this.context, img);
|
|
415
499
|
}
|
|
416
500
|
|
|
417
|
-
|
|
501
|
+
/**
|
|
502
|
+
* 根据像素数据创建纹理
|
|
503
|
+
* @param {ImageData|Uint8Array} data 像素数据
|
|
504
|
+
* @returns {Object} 纹理对象
|
|
505
|
+
*/
|
|
418
506
|
createDataTexture(data) {
|
|
419
507
|
return createDataTexture(this.context, data);
|
|
420
508
|
}
|
|
421
509
|
|
|
422
|
-
|
|
510
|
+
/**
|
|
511
|
+
* 删除纹理
|
|
512
|
+
* @param {Object} texture 纹理对象
|
|
513
|
+
* @returns {Object} 纹理对象
|
|
514
|
+
*/
|
|
423
515
|
deleteTexture(texture) {
|
|
424
516
|
try {
|
|
425
517
|
return deleteTexture(this.context, texture.texture || texture);
|
|
@@ -430,43 +522,55 @@ class WeblBase {
|
|
|
430
522
|
return texture;
|
|
431
523
|
}
|
|
432
524
|
|
|
433
|
-
|
|
434
|
-
|
|
525
|
+
/**
|
|
526
|
+
* 多边形三角化,得到三角形顶点索引数组
|
|
527
|
+
* @param {Array<Object>} points 多边形顶点数组
|
|
528
|
+
* @returns {Array<number>} 三角形顶点索引数组
|
|
529
|
+
*/
|
|
435
530
|
earCutPoints(points) {
|
|
436
531
|
const arr = this.pointsToArray(points);
|
|
437
|
-
const ps = earcut(arr)
|
|
532
|
+
const ps = earcut(arr);
|
|
438
533
|
return ps;
|
|
439
534
|
}
|
|
440
535
|
|
|
441
|
-
|
|
442
|
-
|
|
536
|
+
/**
|
|
537
|
+
* 多边形三角化,得到三角形顶点数组
|
|
538
|
+
* @param {Array<Object>} points 多边形顶点数组
|
|
539
|
+
* @returns {Array<Array<Object>>} 三角形数组,每个三角形包含3个顶点
|
|
540
|
+
*/
|
|
443
541
|
earCutPointsToTriangles(points) {
|
|
444
542
|
this.earCutCache = this.earCutCache || (this.earCutCache = {});
|
|
445
|
-
// 快速缓存 key:用长度和首尾点坐标
|
|
446
543
|
const len = points.length;
|
|
447
544
|
const key = len + '_' + points[0].x + '_' + points[0].y + '_' + points[len-1].x + '_' + points[len-1].y;
|
|
448
545
|
if (this.earCutCache[key]) return this.earCutCache[key];
|
|
449
546
|
|
|
450
|
-
const ps = this.earCutPoints(points)
|
|
547
|
+
const ps = this.earCutPoints(points);
|
|
451
548
|
const triangles = [];
|
|
452
|
-
// 用顶点索引再组合成坐标数组
|
|
453
549
|
for(let i=0;i<ps.length; i+=3) {
|
|
454
550
|
const p1 = points[ps[i]];
|
|
455
551
|
const p2 = points[ps[i+1]];
|
|
456
552
|
const p3 = points[ps[i+2]];
|
|
457
|
-
|
|
458
|
-
triangles.push([p1, p2, p3]);// 每三个顶点构成一个三角
|
|
553
|
+
triangles.push([p1, p2, p3]);
|
|
459
554
|
}
|
|
460
555
|
|
|
461
556
|
this.earCutCache[key] = triangles;
|
|
462
557
|
return triangles;
|
|
463
558
|
}
|
|
464
559
|
|
|
465
|
-
|
|
560
|
+
/**
|
|
561
|
+
* 点坐标数组转为一维数组
|
|
562
|
+
* @param {Array<Object>} points 点数组 [{x, y}, ...]
|
|
563
|
+
* @returns {Array<number>} 一维数组 [x1, y1, x2, y2, ...]
|
|
564
|
+
*/
|
|
466
565
|
pointsToArray(points) {
|
|
467
|
-
return [].concat(...points.map(p=>[p.x,p.y]))
|
|
566
|
+
return [].concat(...points.map(p=>[p.x,p.y]));
|
|
468
567
|
}
|
|
469
|
-
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* 一维数组转为点坐标数组
|
|
571
|
+
* @param {Array<number>} arr 一维数组 [x1, y1, x2, y2, ...]
|
|
572
|
+
* @returns {Array<Object>} 点数组 [{x, y}, ...]
|
|
573
|
+
*/
|
|
470
574
|
arrayToPoints(arr) {
|
|
471
575
|
const points = [];
|
|
472
576
|
for(let i=0;i<arr.length; i+=2) {
|
|
@@ -478,14 +582,33 @@ class WeblBase {
|
|
|
478
582
|
return points;
|
|
479
583
|
}
|
|
480
584
|
|
|
481
|
-
|
|
585
|
+
/**
|
|
586
|
+
* 创建线性渐变
|
|
587
|
+
* @param {number} x1 起点X坐标
|
|
588
|
+
* @param {number} y1 起点Y坐标
|
|
589
|
+
* @param {number} x2 终点X坐标
|
|
590
|
+
* @param {number} y2 终点Y坐标
|
|
591
|
+
* @param {Object} bounds 渐变边界
|
|
592
|
+
* @returns {WebglGradient} 渐变对象
|
|
593
|
+
*/
|
|
482
594
|
createLinearGradient(x1, y1, x2, y2, bounds) {
|
|
483
595
|
return new webglGradient('linear', {
|
|
484
596
|
x1, y1, x2, y2, bounds,
|
|
485
597
|
control: this
|
|
486
598
|
});
|
|
487
599
|
}
|
|
488
|
-
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* 创建径向渐变
|
|
603
|
+
* @param {number} x1 内圆中心X坐标
|
|
604
|
+
* @param {number} y1 内圆中心Y坐标
|
|
605
|
+
* @param {number} r1 内圆半径
|
|
606
|
+
* @param {number} x2 外圆中心X坐标
|
|
607
|
+
* @param {number} y2 外圆中心Y坐标
|
|
608
|
+
* @param {number} r2 外圆半径
|
|
609
|
+
* @param {Object} bounds 渐变边界
|
|
610
|
+
* @returns {WebglGradient} 渐变对象
|
|
611
|
+
*/
|
|
489
612
|
createRadialGradient(x1, y1, r1, x2, y2, r2, bounds) {
|
|
490
613
|
return new webglGradient('radial', {
|
|
491
614
|
x1, y1, r1,
|
|
@@ -494,7 +617,12 @@ class WeblBase {
|
|
|
494
617
|
control: this
|
|
495
618
|
});
|
|
496
619
|
}
|
|
497
|
-
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* 判断是否为渐变对象
|
|
623
|
+
* @param {Object} obj 待检测对象
|
|
624
|
+
* @returns {boolean} 是否为渐变对象
|
|
625
|
+
*/
|
|
498
626
|
isGradient(obj) {
|
|
499
627
|
return obj && obj instanceof webglGradient;
|
|
500
628
|
}
|
|
@@ -1,46 +1,77 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview WebGL 缓冲区管理模块
|
|
4
|
+
*
|
|
5
|
+
* 本模块提供了 WebGL 缓冲区的创建和管理功能,包括:
|
|
6
|
+
* - 创建通用缓冲区
|
|
7
|
+
* - 创建 Float32 类型缓冲区
|
|
8
|
+
* - 创建 Uint16 类型缓冲区
|
|
9
|
+
* - 删除缓冲区
|
|
10
|
+
*
|
|
11
|
+
* @module lib/webgl/core/buffer
|
|
12
|
+
* @author jmGraph Team
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 创建 WebGL 缓冲区
|
|
17
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
18
|
+
* @param {Array|TypedArray} data 缓冲区数据
|
|
19
|
+
* @param {number} [type=gl.ARRAY_BUFFER] 缓冲区类型
|
|
20
|
+
* @param {number} [drawType=gl.STATIC_DRAW] 绘制类型
|
|
21
|
+
* @returns {Object} 缓冲区对象 {type, drawType, buffer, unitSize}
|
|
22
|
+
*/
|
|
3
23
|
function createBuffer(gl, data, type=gl.ARRAY_BUFFER, drawType=gl.STATIC_DRAW) {
|
|
4
|
-
//先创建一个缓存对象
|
|
5
24
|
const buffer = gl.createBuffer();
|
|
6
25
|
if(!buffer) {
|
|
7
26
|
throw Error('创建缓冲区对象失败');
|
|
8
27
|
}
|
|
9
|
-
//说明缓存对象保存的类型
|
|
10
28
|
gl.bindBuffer(type, buffer);
|
|
11
|
-
|
|
12
|
-
// 因为会将数据发送到 GPU,为了省去数据解析,这里使用 Float32Array 直接传送数据
|
|
13
|
-
// data.buffer这里要使用data.buffer,否则在edge下可能导至数据发生较大的改变
|
|
14
|
-
gl.bufferData(type, data.buffer || data, drawType); // 表示缓冲区的内容不会经常更改
|
|
29
|
+
gl.bufferData(type, data.buffer || data, drawType);
|
|
15
30
|
return {
|
|
16
31
|
type,
|
|
17
32
|
drawType,
|
|
18
33
|
buffer,
|
|
19
|
-
// 获取到数组中单个元素的字节数
|
|
20
34
|
unitSize: data.BYTES_PER_ELEMENT
|
|
21
35
|
};
|
|
22
36
|
}
|
|
23
37
|
|
|
24
|
-
|
|
38
|
+
/**
|
|
39
|
+
* 创建 Float32 类型缓冲区
|
|
40
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
41
|
+
* @param {Array} data 数据数组
|
|
42
|
+
* @param {number} [type=gl.ARRAY_BUFFER] 缓冲区类型
|
|
43
|
+
* @param {number} [drawType=gl.STATIC_DRAW] 绘制类型
|
|
44
|
+
* @returns {Object} 缓冲区对象
|
|
45
|
+
*/
|
|
25
46
|
function createFloat32Buffer(gl, data, type=gl.ARRAY_BUFFER, drawType=gl.STATIC_DRAW) {
|
|
26
47
|
const vertices = new Float32Array(data);
|
|
27
48
|
const buffer = createBuffer(gl, vertices, type, drawType);
|
|
28
49
|
return buffer;
|
|
29
50
|
}
|
|
30
51
|
|
|
31
|
-
|
|
52
|
+
/**
|
|
53
|
+
* 创建 Uint16 类型缓冲区
|
|
54
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
55
|
+
* @param {Array} data 数据数组
|
|
56
|
+
* @param {number} [type=gl.ARRAY_BUFFER] 缓冲区类型
|
|
57
|
+
* @param {number} [drawType=gl.STATIC_DRAW] 绘制类型
|
|
58
|
+
* @returns {Object} 缓冲区对象
|
|
59
|
+
*/
|
|
32
60
|
function createUint16Buffer(gl, data, type=gl.ARRAY_BUFFER, drawType=gl.STATIC_DRAW) {
|
|
33
61
|
const vertices = new Uint16Array(data);
|
|
34
62
|
const buffer = createBuffer(gl, vertices, type, drawType);
|
|
35
63
|
return buffer;
|
|
36
64
|
}
|
|
37
65
|
|
|
38
|
-
|
|
66
|
+
/**
|
|
67
|
+
* 删除缓冲区
|
|
68
|
+
* @param {WebGLRenderingContext} gl WebGL 渲染上下文
|
|
69
|
+
* @param {Object|WebGLBuffer} buffer 缓冲区对象或 WebGL 缓冲区
|
|
70
|
+
*/
|
|
39
71
|
function deleteBuffer(gl, buffer) {
|
|
40
72
|
gl.deleteBuffer(buffer.buffer || buffer);
|
|
41
73
|
}
|
|
42
74
|
|
|
43
|
-
|
|
44
75
|
export {
|
|
45
76
|
createBuffer,
|
|
46
77
|
createUint16Buffer,
|
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview GLSL 类型大小映射模块
|
|
4
|
+
*
|
|
5
|
+
* 本模块提供了 GLSL 类型到其元素数量的映射。
|
|
6
|
+
*
|
|
7
|
+
* @module lib/webgl/core/mapSize
|
|
8
|
+
* @author jmGraph Team
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* GLSL 类型到元素数量的映射表
|
|
13
|
+
* @constant {Object.<string, number>}
|
|
14
|
+
*/
|
|
2
15
|
const GLSL_TO_SIZE = {
|
|
3
16
|
'float': 1,
|
|
4
17
|
'vec2': 2,
|
|
@@ -23,18 +36,14 @@ const GLSL_TO_SIZE = {
|
|
|
23
36
|
};
|
|
24
37
|
|
|
25
38
|
/**
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
28
|
-
* @
|
|
29
|
-
* @return {Number}
|
|
39
|
+
* 根据 GLSL 类型名获取元素数量
|
|
40
|
+
* @param {string} type GLSL 类型名
|
|
41
|
+
* @returns {number} 元素数量
|
|
30
42
|
*/
|
|
31
43
|
const mapSize = function(type) {
|
|
32
44
|
return GLSL_TO_SIZE[type];
|
|
33
45
|
};
|
|
34
46
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
47
|
export {
|
|
39
48
|
mapSize
|
|
40
49
|
}
|