jmchart 1.2.15 → 1.2.16
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/jmchart.amd.js +506 -258
- package/dist/jmchart.cjs.js +506 -258
- package/dist/jmchart.esm.js +506 -258
- package/dist/jmchart.iife.js +506 -258
- package/dist/jmchart.system.js +506 -258
- package/package.json +2 -2
package/dist/jmchart.cjs.js
CHANGED
|
@@ -1119,7 +1119,8 @@ class jmGradient {
|
|
|
1119
1119
|
let sy2 = Number(y2) + bounds.top;
|
|
1120
1120
|
if(this.type === 'linear') {
|
|
1121
1121
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
1122
|
-
|
|
1122
|
+
// WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
|
|
1123
|
+
gradient = control.webglControl.createLinearGradient(sx1, sy1, sx2, sy2, bounds);
|
|
1123
1124
|
gradient.key = this.toString();
|
|
1124
1125
|
}
|
|
1125
1126
|
else {
|
|
@@ -1138,7 +1139,8 @@ class jmGradient {
|
|
|
1138
1139
|
r2 = d * r2;
|
|
1139
1140
|
}
|
|
1140
1141
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
1141
|
-
|
|
1142
|
+
// WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
|
|
1143
|
+
gradient = control.webglControl.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2, bounds);
|
|
1142
1144
|
gradient.key = this.toString();
|
|
1143
1145
|
}
|
|
1144
1146
|
//offsetLine = Math.abs(r2 - r1);//二圆半径差
|
|
@@ -2285,10 +2287,14 @@ earcut.flatten = function (data) {
|
|
|
2285
2287
|
return result;
|
|
2286
2288
|
};
|
|
2287
2289
|
|
|
2288
|
-
|
|
2290
|
+
const MAX_STOPS = 16;
|
|
2291
|
+
|
|
2292
|
+
/**
|
|
2293
|
+
* WebGL 渐变对象
|
|
2294
|
+
* 支持 GLSL 着色器直接计算渐变色,无需 textureCanvas
|
|
2295
|
+
*/
|
|
2289
2296
|
class WebglGradient {
|
|
2290
|
-
|
|
2291
|
-
constructor(type='linear', params={}) {
|
|
2297
|
+
constructor(type = 'linear', params = {}) {
|
|
2292
2298
|
this.type = type || 'linear';
|
|
2293
2299
|
|
|
2294
2300
|
this.x1 = params.x1 || 0;
|
|
@@ -2308,161 +2314,114 @@ class WebglGradient {
|
|
|
2308
2314
|
this.control = params.control;
|
|
2309
2315
|
|
|
2310
2316
|
this.stops = [];
|
|
2311
|
-
this.
|
|
2317
|
+
this._sortedStops = null;
|
|
2318
|
+
this._paramsHash = null;
|
|
2312
2319
|
}
|
|
2313
2320
|
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
if(this.type === 'radial') {
|
|
2319
|
-
this.length = this.r2 - this.r1;
|
|
2320
|
-
}
|
|
2321
|
-
else if(dx === 0 && dy === 0) {
|
|
2322
|
-
this.length = 0;
|
|
2323
|
-
}
|
|
2324
|
-
else {
|
|
2325
|
-
// 渐变中心的距离
|
|
2326
|
-
this.length = Math.sqrt(Math.pow(dx, 2), Math.pow(dy, 2));
|
|
2327
|
-
this.sin = dy / this.length;
|
|
2328
|
-
this.cos = dx / this.length;
|
|
2329
|
-
}
|
|
2330
|
-
}
|
|
2331
|
-
|
|
2332
|
-
// 渐变颜色
|
|
2321
|
+
/**
|
|
2322
|
+
* 添加颜色断点
|
|
2323
|
+
*/
|
|
2333
2324
|
addColorStop(offset, color) {
|
|
2334
2325
|
this.stops.push({
|
|
2335
|
-
offset,
|
|
2326
|
+
offset: Math.max(0, Math.min(1, offset)),
|
|
2336
2327
|
color
|
|
2337
2328
|
});
|
|
2329
|
+
this._sortedStops = null;
|
|
2330
|
+
this._paramsHash = null;
|
|
2338
2331
|
}
|
|
2339
2332
|
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
if(this.
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2333
|
+
/**
|
|
2334
|
+
* 获取排序后的 stops(带解析后的颜色)
|
|
2335
|
+
*/
|
|
2336
|
+
_getSortedStops() {
|
|
2337
|
+
if (this._sortedStops) return this._sortedStops;
|
|
2338
|
+
|
|
2339
|
+
const utils = this.control && this.control.graph && this.control.graph.utils;
|
|
2340
|
+
this._sortedStops = this.stops
|
|
2341
|
+
.map(s => {
|
|
2342
|
+
let c = s.color;
|
|
2343
|
+
if (utils && typeof c === 'string') {
|
|
2344
|
+
c = utils.hexToRGBA(c);
|
|
2345
|
+
}
|
|
2346
|
+
if (typeof c === 'object' && c !== null) {
|
|
2347
|
+
// hexToRGBA 返回 r/g/b 为 0~255,a 为 0~1
|
|
2348
|
+
// 但如果已经是 0~1 范围(由 rgbToDecimal 处理过),需要检测
|
|
2349
|
+
const needNormalize = (c.r > 1 || c.g > 1 || c.b > 1) ? 255 : 1;
|
|
2350
|
+
return {
|
|
2351
|
+
offset: s.offset,
|
|
2352
|
+
r: (c.r !== undefined ? c.r : 0) / needNormalize,
|
|
2353
|
+
g: (c.g !== undefined ? c.g : 0) / needNormalize,
|
|
2354
|
+
b: (c.b !== undefined ? c.b : 0) / needNormalize,
|
|
2355
|
+
a: c.a !== undefined ? c.a : 1
|
|
2356
|
+
};
|
|
2357
|
+
}
|
|
2358
|
+
return { offset: s.offset, r: 0, g: 0, b: 0, a: 1 };
|
|
2359
|
+
})
|
|
2360
|
+
.sort((a, b) => a.offset - b.offset);
|
|
2349
2361
|
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
}
|
|
2353
|
-
let gradient = null;
|
|
2354
|
-
if(this.type === 'linear') {
|
|
2355
|
-
gradient = control.textureContext.createLinearGradient(this.x1, this.y1, this.x2, this.y2);
|
|
2356
|
-
}
|
|
2357
|
-
else {
|
|
2358
|
-
gradient = control.textureContext.createRadialGradient(this.x1, this.y1, this.r1, this.x2, this.y2, this.r2);
|
|
2359
|
-
}
|
|
2360
|
-
this.stops.forEach(function(s, i) {
|
|
2361
|
-
const c = control.graph.utils.toColor(s.color);
|
|
2362
|
-
gradient && gradient.addColorStop(s.offset, c);
|
|
2363
|
-
});
|
|
2364
|
-
|
|
2365
|
-
const data = control.toFillTexture(gradient, bounds, points);
|
|
2362
|
+
return this._sortedStops;
|
|
2363
|
+
}
|
|
2366
2364
|
|
|
2367
|
-
|
|
2368
|
-
|
|
2365
|
+
/**
|
|
2366
|
+
* 将渐变参数以 uniform 形式传递给着色器
|
|
2367
|
+
* 返回 { type, start, end, stopCount, stops } 供着色器使用
|
|
2368
|
+
*/
|
|
2369
|
+
toUniformParams() {
|
|
2370
|
+
const stops = this._getSortedStops();
|
|
2371
|
+
const count = Math.min(stops.length, MAX_STOPS);
|
|
2372
|
+
|
|
2373
|
+
// 展平为 Float32Array: [offset, r, g, b, a, ...]
|
|
2374
|
+
const flatStops = new Float32Array(count * 5);
|
|
2375
|
+
for (let i = 0; i < count; i++) {
|
|
2376
|
+
const s = stops[i];
|
|
2377
|
+
flatStops[i * 5 + 0] = s.offset;
|
|
2378
|
+
flatStops[i * 5 + 1] = s.r;
|
|
2379
|
+
flatStops[i * 5 + 2] = s.g;
|
|
2380
|
+
flatStops[i * 5 + 3] = s.b;
|
|
2381
|
+
flatStops[i * 5 + 4] = s.a;
|
|
2382
|
+
}
|
|
2369
2383
|
|
|
2370
|
-
return
|
|
2384
|
+
return {
|
|
2385
|
+
gradientType: this.type === 'radial' ? 2 : 1,
|
|
2386
|
+
gradientStart: new Float32Array([
|
|
2387
|
+
this.x1, this.y1,
|
|
2388
|
+
this.type === 'radial' ? Math.max(0, this.r1) : 0,
|
|
2389
|
+
0
|
|
2390
|
+
]),
|
|
2391
|
+
gradientEnd: new Float32Array([
|
|
2392
|
+
this.x2, this.y2,
|
|
2393
|
+
this.type === 'radial' ? Math.max(0, this.r2) : 0,
|
|
2394
|
+
0
|
|
2395
|
+
]),
|
|
2396
|
+
stopCount: count,
|
|
2397
|
+
stops: flatStops
|
|
2398
|
+
};
|
|
2371
2399
|
}
|
|
2372
2400
|
|
|
2373
|
-
|
|
2401
|
+
/**
|
|
2402
|
+
* 使缓存失效
|
|
2403
|
+
*/
|
|
2374
2404
|
invalidateCache() {
|
|
2375
|
-
this.
|
|
2376
|
-
this.
|
|
2377
|
-
}
|
|
2378
|
-
|
|
2379
|
-
// 根据绘制图形的坐标计算出对应点的颜色
|
|
2380
|
-
/*
|
|
2381
|
-
toPointColors(points) {
|
|
2382
|
-
const stops = this.getStops();
|
|
2383
|
-
const colors = [];
|
|
2384
|
-
for(let i=0; i<points.length; i+=2) {
|
|
2385
|
-
const p = {
|
|
2386
|
-
x: points[i],
|
|
2387
|
-
y: points[i+1]
|
|
2388
|
-
}
|
|
2389
|
-
if(this.type === 'radial') {
|
|
2390
|
-
const dx = p.x - this.x1;
|
|
2391
|
-
const dy = p.y - this.y1;
|
|
2392
|
-
const len = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
|
|
2393
|
-
const rang = this.getStopRange(len, stops);
|
|
2394
|
-
if(!rang.start && rang.end) {
|
|
2395
|
-
colors.push(rang.end.color);
|
|
2396
|
-
}
|
|
2397
|
-
else if(!rang.end && rang.start) {
|
|
2398
|
-
colors.push(rang.start.color);
|
|
2399
|
-
}
|
|
2400
|
-
else {
|
|
2401
|
-
const rangLength = rang.end.length - rang.start.length;
|
|
2402
|
-
const offlen = len - rang.start.length;
|
|
2403
|
-
const per = offlen / rangLength;
|
|
2404
|
-
const color = {
|
|
2405
|
-
r: rang.start.color.r + (rang.end.color.r - rang.start.color.r) * per,
|
|
2406
|
-
g: rang.start.color.g + (rang.end.color.g - rang.start.color.g) * per,
|
|
2407
|
-
b: rang.start.color.b + (rang.end.color.b - rang.start.color.b) * per,
|
|
2408
|
-
a: rang.start.color.a + (rang.end.color.a - rang.start.color.a) * per,
|
|
2409
|
-
};
|
|
2410
|
-
colors.push(color);
|
|
2411
|
-
}
|
|
2412
|
-
}
|
|
2413
|
-
}
|
|
2414
|
-
return colors;
|
|
2415
|
-
}
|
|
2416
|
-
*/
|
|
2417
|
-
// 根据起点距离获取边界stop
|
|
2418
|
-
/*
|
|
2419
|
-
getStopRange(len, stops) {
|
|
2420
|
-
const res = {};
|
|
2421
|
-
for(const s of stops) {
|
|
2422
|
-
if(s.length <= len) {
|
|
2423
|
-
res.start = s;
|
|
2424
|
-
}
|
|
2425
|
-
else {
|
|
2426
|
-
res.end = s;
|
|
2427
|
-
}
|
|
2428
|
-
}
|
|
2429
|
-
return res;
|
|
2405
|
+
this._sortedStops = null;
|
|
2406
|
+
this._paramsHash = null;
|
|
2430
2407
|
}
|
|
2431
2408
|
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2409
|
+
/**
|
|
2410
|
+
* 转换为渐变的字符串表达
|
|
2411
|
+
*/
|
|
2412
|
+
toString() {
|
|
2413
|
+
let str = this.type + '-gradient(';
|
|
2414
|
+
if (this.type == 'linear') {
|
|
2415
|
+
str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
|
|
2416
|
+
}
|
|
2417
|
+
else {
|
|
2418
|
+
str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
|
|
2441
2419
|
}
|
|
2442
|
-
|
|
2420
|
+
this.stops.forEach(function(s) {
|
|
2421
|
+
str += ',' + s.color + ' ' + s.offset;
|
|
2422
|
+
});
|
|
2423
|
+
return str + ')';
|
|
2443
2424
|
}
|
|
2444
|
-
*/
|
|
2445
|
-
/**
|
|
2446
|
-
* 转换为渐变的字符串表达
|
|
2447
|
-
*
|
|
2448
|
-
* @method toString
|
|
2449
|
-
* @for jmGradient
|
|
2450
|
-
* @return {string} linear-gradient(x1 y1 x2 y2, color1 step, color2 step, ...); //radial-gradient(x1 y1 r1 x2 y2 r2, color1 step,color2 step, ...);
|
|
2451
|
-
*/
|
|
2452
|
-
toString() {
|
|
2453
|
-
let str = this.type + '-gradient(';
|
|
2454
|
-
if(this.type == 'linear') {
|
|
2455
|
-
str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
|
|
2456
|
-
}
|
|
2457
|
-
else {
|
|
2458
|
-
str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
|
|
2459
|
-
}
|
|
2460
|
-
//颜色渐变
|
|
2461
|
-
this.stops.forEach(function(s) {
|
|
2462
|
-
str += ',' + s.color + ' ' + s.offset;
|
|
2463
|
-
});
|
|
2464
|
-
return str + ')';
|
|
2465
|
-
}
|
|
2466
2425
|
}
|
|
2467
2426
|
|
|
2468
2427
|
// 生成着色器
|
|
@@ -2787,8 +2746,8 @@ const pathVertexSource = `
|
|
|
2787
2746
|
vec4 pos = translatePosition(a_position, a_center_point.x, a_center_point.y);
|
|
2788
2747
|
gl_Position = pos;
|
|
2789
2748
|
v_color = a_color;
|
|
2790
|
-
if(a_type == 2) {
|
|
2791
|
-
v_text_coord =
|
|
2749
|
+
if(a_type == 2 || a_type == 5) {
|
|
2750
|
+
v_text_coord = a_position.xy;
|
|
2792
2751
|
}
|
|
2793
2752
|
}
|
|
2794
2753
|
`;
|
|
@@ -2798,21 +2757,73 @@ const pathFragmentSource = `
|
|
|
2798
2757
|
uniform sampler2D u_sample;
|
|
2799
2758
|
uniform vec4 v_texture_bounds; // 纹理的左上坐标和大小 x,y,z,w
|
|
2800
2759
|
uniform vec4 v_single_color;
|
|
2760
|
+
// GLSL 渐变 uniforms
|
|
2761
|
+
uniform int u_gradient_type; // 0=无 1=线性 2=径向
|
|
2762
|
+
uniform vec4 u_gradient_start; // 线性:{x1,y1,0,0} 径向:{cx,cy,r1,0}
|
|
2763
|
+
uniform vec4 u_gradient_end; // 线性:{x2,y2,0,0} 径向:{cx,cy,r2,0}
|
|
2764
|
+
uniform int u_gradient_stop_count;
|
|
2765
|
+
uniform float u_gradient_offsets[${MAX_STOPS}];
|
|
2766
|
+
uniform vec4 u_gradient_colors[${MAX_STOPS}]; // {r, g, b, a} 0~1 范围
|
|
2801
2767
|
varying float v_type;
|
|
2802
2768
|
varying vec4 v_color;
|
|
2803
2769
|
varying vec2 v_text_coord;
|
|
2804
2770
|
|
|
2805
2771
|
${convertTexturePosition}
|
|
2806
2772
|
|
|
2773
|
+
// 在 sorted stops 中按 t 值采样颜色
|
|
2774
|
+
// 兼容 GLSL ES 1.0:循环仅与常量比较,无 break/continue
|
|
2775
|
+
vec4 sampleGradient(float t) {
|
|
2776
|
+
t = clamp(t, 0.0, 1.0);
|
|
2777
|
+
// 正向扫描:始终遍历 MAX_STOPS-1 次,找到 t 所在段并覆盖结果
|
|
2778
|
+
float localT = 0.0;
|
|
2779
|
+
vec4 c0 = u_gradient_colors[0];
|
|
2780
|
+
vec4 c1 = u_gradient_colors[0];
|
|
2781
|
+
for(int i = 0; i < ${MAX_STOPS - 1}; i++) {
|
|
2782
|
+
float s0 = u_gradient_offsets[i];
|
|
2783
|
+
float s1 = u_gradient_offsets[i + 1];
|
|
2784
|
+
if(t >= s0) {
|
|
2785
|
+
float range = s1 - s0;
|
|
2786
|
+
localT = range > 0.0001 ? clamp((t - s0) / range, 0.0, 1.0) : 0.0;
|
|
2787
|
+
c0 = u_gradient_colors[i];
|
|
2788
|
+
c1 = u_gradient_colors[i + 1];
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
return mix(c0, c1, localT);
|
|
2792
|
+
}
|
|
2793
|
+
|
|
2807
2794
|
void main() {
|
|
2808
2795
|
// 如果是fill,则直接填充颜色
|
|
2809
2796
|
if(v_type == 1.0) {
|
|
2810
2797
|
gl_FragColor = v_single_color;
|
|
2811
2798
|
}
|
|
2812
|
-
// 渐变色
|
|
2799
|
+
// 渐变色 (旧方式,顶点颜色插值)
|
|
2813
2800
|
else if(v_type == 3.0) {
|
|
2814
2801
|
gl_FragColor = v_color;
|
|
2815
2802
|
}
|
|
2803
|
+
// GLSL 渐变填充 (type=5)
|
|
2804
|
+
else if(v_type == 5.0) {
|
|
2805
|
+
float t;
|
|
2806
|
+
if(u_gradient_type == 2) {
|
|
2807
|
+
// 径向渐变
|
|
2808
|
+
vec2 d = v_text_coord - u_gradient_start.xy;
|
|
2809
|
+
float dist = length(d);
|
|
2810
|
+
float r1 = u_gradient_start.z;
|
|
2811
|
+
float r2 = u_gradient_end.z;
|
|
2812
|
+
float range = r2 - r1;
|
|
2813
|
+
t = range > 0.001 ? (dist - r1) / range : 0.0;
|
|
2814
|
+
} else {
|
|
2815
|
+
// 线性渐变
|
|
2816
|
+
vec2 dir = u_gradient_end.xy - u_gradient_start.xy;
|
|
2817
|
+
float lenSq = dot(dir, dir);
|
|
2818
|
+
if(lenSq > 0.001) {
|
|
2819
|
+
vec2 pos = v_text_coord - u_gradient_start.xy;
|
|
2820
|
+
t = dot(pos, dir) / lenSq;
|
|
2821
|
+
} else {
|
|
2822
|
+
t = 0.0;
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
gl_FragColor = sampleGradient(t) * v_single_color.a;
|
|
2826
|
+
}
|
|
2816
2827
|
else if(v_type == 2.0) {
|
|
2817
2828
|
vec2 pos = translateTexturePosition(v_text_coord, v_texture_bounds);
|
|
2818
2829
|
gl_FragColor = texture2D(u_sample, pos);
|
|
@@ -2883,7 +2894,7 @@ class WeblBase {
|
|
|
2883
2894
|
rotate(angle) {
|
|
2884
2895
|
const cos = Math.cos(angle);
|
|
2885
2896
|
const sin = Math.sin(angle);
|
|
2886
|
-
const [a, b, c, d
|
|
2897
|
+
const [a, b, c, d] = this.transformMatrix;
|
|
2887
2898
|
|
|
2888
2899
|
// 更新变换矩阵
|
|
2889
2900
|
this.transformMatrix[0] = a * cos - b * sin;
|
|
@@ -2914,21 +2925,20 @@ class WeblBase {
|
|
|
2914
2925
|
};
|
|
2915
2926
|
}
|
|
2916
2927
|
|
|
2917
|
-
//
|
|
2918
|
-
get
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2928
|
+
// 文本测量用的离屏 canvas context(1x1 单例缓存,不依赖 textureCanvas)
|
|
2929
|
+
get _measureCtx() {
|
|
2930
|
+
if(!this.__measureCtx) {
|
|
2931
|
+
try {
|
|
2932
|
+
if(typeof document !== 'undefined') {
|
|
2933
|
+
const c = document.createElement('canvas');
|
|
2934
|
+
c.width = c.height = 1;
|
|
2935
|
+
this.__measureCtx = c.getContext('2d');
|
|
2936
|
+
}
|
|
2937
|
+
} catch(e) {
|
|
2938
|
+
this.__measureCtx = null;
|
|
2939
|
+
}
|
|
2923
2940
|
}
|
|
2924
|
-
return
|
|
2925
|
-
}
|
|
2926
|
-
// 纹理绘制canvas ctx
|
|
2927
|
-
get textureContext() {
|
|
2928
|
-
const ctx = this.textureCanvas.ctx || (this.textureCanvas.ctx = this.textureCanvas.getContext('2d', {
|
|
2929
|
-
willReadFrequently: true
|
|
2930
|
-
}));
|
|
2931
|
-
return ctx;
|
|
2941
|
+
return this.__measureCtx;
|
|
2932
2942
|
}
|
|
2933
2943
|
|
|
2934
2944
|
// i当前程序
|
|
@@ -2974,23 +2984,37 @@ class WeblBase {
|
|
|
2974
2984
|
// 把传统颜色转为webgl识别的
|
|
2975
2985
|
convertColor(color) {
|
|
2976
2986
|
if(this.isGradient(color)) return color;
|
|
2977
|
-
if(typeof color === 'string')
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
if(['fillStyle', 'strokeStyle', 'shadowColor'].indexOf(style) > -1) {
|
|
2985
|
-
value = this.graph.utils.toColor(value);
|
|
2987
|
+
if(typeof color === 'string') {
|
|
2988
|
+
// 先尝试 hexToRGBA 解析
|
|
2989
|
+
color = this.graph.utils.hexToRGBA(color);
|
|
2990
|
+
// hexToRGBA 对无法识别的格式(如 hsl)会原样返回字符串
|
|
2991
|
+
// 利用离屏 canvas 将任意 CSS 颜色转为 rgba
|
|
2992
|
+
if(typeof color === 'string') {
|
|
2993
|
+
color = this.__parseCSSColor(color);
|
|
2986
2994
|
}
|
|
2987
|
-
this.textureContext[style] = value;
|
|
2988
2995
|
}
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2996
|
+
if(typeof color === 'object' && color.r !== undefined) {
|
|
2997
|
+
return this.graph.utils.rgbToDecimal(color);
|
|
2998
|
+
}
|
|
2999
|
+
return color;
|
|
3000
|
+
}
|
|
3001
|
+
|
|
3002
|
+
// 利用离屏 canvas 解析任意 CSS 颜色(hsl/hsla/命名颜色等)
|
|
3003
|
+
__parseCSSColor(colorStr) {
|
|
3004
|
+
const ctx = this._measureCtx;
|
|
3005
|
+
if(!ctx) return { r: 0, g: 0, b: 0, a: 0 };
|
|
3006
|
+
try {
|
|
3007
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
3008
|
+
ctx.fillStyle = '#000000';
|
|
3009
|
+
ctx.fillStyle = colorStr;
|
|
3010
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
3011
|
+
const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data;
|
|
3012
|
+
if(ctx.fillStyle === '#000000' && colorStr !== '#000000' && colorStr !== 'black') {
|
|
3013
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
2993
3014
|
}
|
|
3015
|
+
return { r, g, b, a: a / 255 };
|
|
3016
|
+
} catch(e) {
|
|
3017
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
2994
3018
|
}
|
|
2995
3019
|
}
|
|
2996
3020
|
|
|
@@ -3165,70 +3189,23 @@ class WeblBase {
|
|
|
3165
3189
|
return obj && obj instanceof WebglGradient;
|
|
3166
3190
|
}
|
|
3167
3191
|
|
|
3168
|
-
|
|
3192
|
+
/**
|
|
3169
3193
|
* 测试获取文本所占大小
|
|
3170
3194
|
*
|
|
3171
3195
|
* @method testSize
|
|
3172
3196
|
* @return {object} 含文本大小的对象
|
|
3173
3197
|
*/
|
|
3174
3198
|
testSize(text, style=this.style) {
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
{width:15};
|
|
3184
|
-
this.textureContext.restore &&this.textureContext.restore();
|
|
3185
|
-
size.height = this.style.fontSize? this.style.fontSize: 15;
|
|
3199
|
+
const ctx = this._measureCtx;
|
|
3200
|
+
if(!ctx) return { width: 15, height: style.fontSize || 15 };
|
|
3201
|
+
|
|
3202
|
+
ctx.save && ctx.save();
|
|
3203
|
+
if(style.font || style.fontSize) ctx.font = style.font || (style.fontSize + 'px ' + style.fontFamily);
|
|
3204
|
+
const size = ctx.measureText ? ctx.measureText(text) : { width: 15 };
|
|
3205
|
+
ctx.restore && ctx.restore();
|
|
3206
|
+
size.height = style.fontSize ? parseInt(style.fontSize) : 15;
|
|
3186
3207
|
return size;
|
|
3187
3208
|
}
|
|
3188
|
-
|
|
3189
|
-
// 使用纹理canvas生成图,
|
|
3190
|
-
// 填充可以是颜色或渐变对象
|
|
3191
|
-
// 如果指定了points,则表明要绘制不规则的图形
|
|
3192
|
-
toFillTexture(fillStyle, bounds, points=null) {
|
|
3193
|
-
const canvas = this.textureCanvas;
|
|
3194
|
-
if(!canvas) {
|
|
3195
|
-
return fillStyle;
|
|
3196
|
-
}
|
|
3197
|
-
canvas.width = bounds.width;
|
|
3198
|
-
canvas.height = bounds.height;
|
|
3199
|
-
|
|
3200
|
-
if(!canvas.width || !canvas.height) {
|
|
3201
|
-
return fillStyle;
|
|
3202
|
-
}
|
|
3203
|
-
|
|
3204
|
-
this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
|
|
3205
|
-
|
|
3206
|
-
this.textureContext.fillStyle = fillStyle;
|
|
3207
|
-
|
|
3208
|
-
// 规则图形用 fillRect,比 beginPath/lineTo/fill 快
|
|
3209
|
-
if(!points || !points.length) {
|
|
3210
|
-
this.textureContext.fillRect(0, 0, bounds.width, bounds.height);
|
|
3211
|
-
} else {
|
|
3212
|
-
this.textureContext.beginPath();
|
|
3213
|
-
for(const p of points) {
|
|
3214
|
-
//移至当前坐标
|
|
3215
|
-
if(p.m) {
|
|
3216
|
-
this.textureContext.moveTo(p.x - bounds.left, p.y - bounds.top);
|
|
3217
|
-
}
|
|
3218
|
-
else {
|
|
3219
|
-
this.textureContext.lineTo(p.x - bounds.left, p.y - bounds.top);
|
|
3220
|
-
}
|
|
3221
|
-
}
|
|
3222
|
-
this.textureContext.closePath();
|
|
3223
|
-
this.textureContext.fill();
|
|
3224
|
-
}
|
|
3225
|
-
|
|
3226
|
-
const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
|
|
3227
|
-
return {
|
|
3228
|
-
data,
|
|
3229
|
-
points
|
|
3230
|
-
};
|
|
3231
|
-
}
|
|
3232
3209
|
}
|
|
3233
3210
|
|
|
3234
3211
|
// path 绘制类
|
|
@@ -3435,6 +3412,112 @@ class WebglPath extends WeblBase {
|
|
|
3435
3412
|
equalPoint(p1, p2) {
|
|
3436
3413
|
return p1.x === p2.x && p1.y === p2.y;
|
|
3437
3414
|
}
|
|
3415
|
+
|
|
3416
|
+
// 将带 moveTo 标记的点集拆分为外轮廓和多个洞
|
|
3417
|
+
splitSubPaths(points) {
|
|
3418
|
+
const subPaths = [];
|
|
3419
|
+
let current = [];
|
|
3420
|
+
for(let i = 0; i < points.length; i++) {
|
|
3421
|
+
const p = points[i];
|
|
3422
|
+
if(p.m && current.length > 0) {
|
|
3423
|
+
subPaths.push(current);
|
|
3424
|
+
current = [];
|
|
3425
|
+
}
|
|
3426
|
+
current.push(p);
|
|
3427
|
+
}
|
|
3428
|
+
if(current.length > 0) subPaths.push(current);
|
|
3429
|
+
|
|
3430
|
+
// 面积最大的作为外轮廓,其余作为洞
|
|
3431
|
+
let maxArea = -1;
|
|
3432
|
+
let outerIdx = 0;
|
|
3433
|
+
for(let i = 0; i < subPaths.length; i++) {
|
|
3434
|
+
const area = Math.abs(this.polygonArea(subPaths[i]));
|
|
3435
|
+
if(area > maxArea) {
|
|
3436
|
+
maxArea = area;
|
|
3437
|
+
outerIdx = i;
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
|
|
3441
|
+
const outerPoints = subPaths[outerIdx];
|
|
3442
|
+
const holes = [];
|
|
3443
|
+
for(let i = 0; i < subPaths.length; i++) {
|
|
3444
|
+
if(i !== outerIdx) holes.push(subPaths[i]);
|
|
3445
|
+
}
|
|
3446
|
+
return { outerPoints, holes };
|
|
3447
|
+
}
|
|
3448
|
+
|
|
3449
|
+
// 计算多边形面积(Shoelace 公式)
|
|
3450
|
+
polygonArea(points) {
|
|
3451
|
+
let area = 0;
|
|
3452
|
+
const n = points.length;
|
|
3453
|
+
for(let i = 0; i < n; i++) {
|
|
3454
|
+
const j = (i + 1) % n;
|
|
3455
|
+
area += points[i].x * points[j].y;
|
|
3456
|
+
area -= points[j].x * points[i].y;
|
|
3457
|
+
}
|
|
3458
|
+
return area / 2;
|
|
3459
|
+
}
|
|
3460
|
+
|
|
3461
|
+
// 使用 earcut 带 holes 填充多边形
|
|
3462
|
+
fillWithHoles(outerPoints, holes, isTexture = false) {
|
|
3463
|
+
// 将所有点合并:外轮廓 + 各个洞,并记录洞的起始索引
|
|
3464
|
+
const allPoints = [...outerPoints];
|
|
3465
|
+
const holeIndices = [];
|
|
3466
|
+
for(const hole of holes) {
|
|
3467
|
+
holeIndices.push(allPoints.length);
|
|
3468
|
+
allPoints.push(...hole);
|
|
3469
|
+
}
|
|
3470
|
+
|
|
3471
|
+
const dim = 2;
|
|
3472
|
+
const vertexData = [];
|
|
3473
|
+
for(const p of allPoints) {
|
|
3474
|
+
vertexData.push(p.x, p.y);
|
|
3475
|
+
}
|
|
3476
|
+
|
|
3477
|
+
// 用 earcut 进行带洞三角化
|
|
3478
|
+
const indices = earcut(vertexData, holeIndices, dim);
|
|
3479
|
+
|
|
3480
|
+
if(!indices || indices.length < 3) return;
|
|
3481
|
+
|
|
3482
|
+
// 构建 GPU 顶点数据
|
|
3483
|
+
const allVertices = [];
|
|
3484
|
+
const allTexCoords = [];
|
|
3485
|
+
for(let i = 0; i < indices.length; i++) {
|
|
3486
|
+
const p = allPoints[indices[i]];
|
|
3487
|
+
allVertices.push(p.x, p.y);
|
|
3488
|
+
if(isTexture) allTexCoords.push(p.x, p.y);
|
|
3489
|
+
}
|
|
3490
|
+
|
|
3491
|
+
const gl = this.context;
|
|
3492
|
+
const vertexArr = new Float32Array(allVertices);
|
|
3493
|
+
|
|
3494
|
+
let posBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_position);
|
|
3495
|
+
if(!posBuffer) {
|
|
3496
|
+
posBuffer = this.createFloat32Buffer(vertexArr, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
|
|
3497
|
+
posBuffer.attr = this.program.attrs.a_position;
|
|
3498
|
+
this.__cachedBuffers.push(posBuffer);
|
|
3499
|
+
} else {
|
|
3500
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer.buffer);
|
|
3501
|
+
gl.bufferData(gl.ARRAY_BUFFER, vertexArr, gl.DYNAMIC_DRAW);
|
|
3502
|
+
}
|
|
3503
|
+
this.writeVertexAttrib(posBuffer, this.program.attrs.a_position, 2, 0, 0);
|
|
3504
|
+
|
|
3505
|
+
if(isTexture && allTexCoords.length) {
|
|
3506
|
+
const texData = new Float32Array(allTexCoords);
|
|
3507
|
+
let texBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_text_coord);
|
|
3508
|
+
if(!texBuffer) {
|
|
3509
|
+
texBuffer = this.createFloat32Buffer(texData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
|
|
3510
|
+
texBuffer.attr = this.program.attrs.a_text_coord;
|
|
3511
|
+
this.__cachedBuffers.push(texBuffer);
|
|
3512
|
+
} else {
|
|
3513
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texBuffer.buffer);
|
|
3514
|
+
gl.bufferData(gl.ARRAY_BUFFER, texData, gl.DYNAMIC_DRAW);
|
|
3515
|
+
}
|
|
3516
|
+
this.writeVertexAttrib(texBuffer, this.program.attrs.a_text_coord, 2, 0, 0);
|
|
3517
|
+
}
|
|
3518
|
+
|
|
3519
|
+
gl.drawArrays(gl.TRIANGLES, 0, allVertices.length / 2);
|
|
3520
|
+
}
|
|
3438
3521
|
// 把path坐标集合转为线段集
|
|
3439
3522
|
pathToLines(points) {
|
|
3440
3523
|
let start = null;
|
|
@@ -3661,9 +3744,46 @@ class WebglPath extends WeblBase {
|
|
|
3661
3744
|
}
|
|
3662
3745
|
if(points && points.length) {
|
|
3663
3746
|
const regular = lineWidth <= 1.2;
|
|
3664
|
-
|
|
3665
|
-
this.
|
|
3666
|
-
|
|
3747
|
+
const hasMoveTo = points.some && points.some(p => p.m);
|
|
3748
|
+
const isRing = !hasMoveTo && this.needCut; // 空心形状(jmHArc close=true 时无 m 标记)
|
|
3749
|
+
if(regular && (hasMoveTo || isRing)) {
|
|
3750
|
+
// 有 moveTo 标记或空心形状时,分段绘制每个子路径的 LINE_LOOP
|
|
3751
|
+
// 避免 LINE_LOOP 把不同子路径的点连起来产生拉扯线
|
|
3752
|
+
if(hasMoveTo) {
|
|
3753
|
+
let subPath = [];
|
|
3754
|
+
for(let i = 0; i < points.length; i++) {
|
|
3755
|
+
if(points[i].m && subPath.length > 0) {
|
|
3756
|
+
this.writePoints(subPath);
|
|
3757
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
|
|
3758
|
+
subPath = [];
|
|
3759
|
+
}
|
|
3760
|
+
subPath.push(points[i]);
|
|
3761
|
+
}
|
|
3762
|
+
if(subPath.length > 1) {
|
|
3763
|
+
this.writePoints(subPath);
|
|
3764
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
|
|
3765
|
+
}
|
|
3766
|
+
}
|
|
3767
|
+
else if(isRing) {
|
|
3768
|
+
// 空心形状:前半段为内弧,后半段为外弧(反向),各自 LINE_LOOP
|
|
3769
|
+
const mid = Math.floor(points.length / 2);
|
|
3770
|
+
const inner = points.slice(0, mid);
|
|
3771
|
+
const outer = points.slice(mid);
|
|
3772
|
+
if(inner.length > 1) {
|
|
3773
|
+
this.writePoints(inner);
|
|
3774
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, inner.length);
|
|
3775
|
+
}
|
|
3776
|
+
if(outer.length > 1) {
|
|
3777
|
+
this.writePoints(outer);
|
|
3778
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, outer.length);
|
|
3779
|
+
}
|
|
3780
|
+
}
|
|
3781
|
+
}
|
|
3782
|
+
else {
|
|
3783
|
+
points = regular? points : this.pathToPoints(points);
|
|
3784
|
+
this.writePoints(points);
|
|
3785
|
+
this.context.drawArrays(regular? this.context.LINE_LOOP: this.context.POINTS, 0, points.length);
|
|
3786
|
+
}
|
|
3667
3787
|
// buffer 由 endDraw 统一清理
|
|
3668
3788
|
}
|
|
3669
3789
|
colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
|
|
@@ -3685,10 +3805,9 @@ class WebglPath extends WeblBase {
|
|
|
3685
3805
|
|
|
3686
3806
|
fillColor(color, points, bounds, type=1) {
|
|
3687
3807
|
|
|
3688
|
-
//
|
|
3808
|
+
// 如果是渐变色,使用 GLSL 着色器直接计算
|
|
3689
3809
|
if(this.isGradient(color)) {
|
|
3690
|
-
|
|
3691
|
-
return this.fillImage(imgData.data, imgData.points, bounds);
|
|
3810
|
+
return this.fillGradient(color, points, bounds);
|
|
3692
3811
|
}
|
|
3693
3812
|
|
|
3694
3813
|
// 标注为fill
|
|
@@ -3701,6 +3820,83 @@ class WebglPath extends WeblBase {
|
|
|
3701
3820
|
|
|
3702
3821
|
}
|
|
3703
3822
|
|
|
3823
|
+
/**
|
|
3824
|
+
* 使用 GLSL 着色器渲染渐变填充
|
|
3825
|
+
* 无需 textureCanvas,直接通过 uniform 传递渐变参数给 GPU
|
|
3826
|
+
*/
|
|
3827
|
+
fillGradient(gradient, points, bounds) {
|
|
3828
|
+
const params = gradient.toUniformParams();
|
|
3829
|
+
if(!params) return;
|
|
3830
|
+
|
|
3831
|
+
// 标注为 GLSL 渐变 (type=5)
|
|
3832
|
+
this.context.uniform1i(this.program.uniforms.a_type.location, 5);
|
|
3833
|
+
|
|
3834
|
+
// 设置 globalAlpha(通过 v_single_color.a 传递给着色器)
|
|
3835
|
+
this.context.uniform4f(this.program.uniforms.v_single_color.location, 1.0, 1.0, 1.0, this.style.globalAlpha);
|
|
3836
|
+
|
|
3837
|
+
// 设置渐变类型
|
|
3838
|
+
if(this.program.uniforms.u_gradient_type) {
|
|
3839
|
+
this.context.uniform1i(this.program.uniforms.u_gradient_type.location, params.gradientType);
|
|
3840
|
+
}
|
|
3841
|
+
|
|
3842
|
+
// 设置渐变起点/终点
|
|
3843
|
+
if(this.program.uniforms.u_gradient_start) {
|
|
3844
|
+
this.context.uniform4fv(this.program.uniforms.u_gradient_start.location, params.gradientStart);
|
|
3845
|
+
}
|
|
3846
|
+
if(this.program.uniforms.u_gradient_end) {
|
|
3847
|
+
this.context.uniform4fv(this.program.uniforms.u_gradient_end.location, params.gradientEnd);
|
|
3848
|
+
}
|
|
3849
|
+
|
|
3850
|
+
// 设置颜色断点数量
|
|
3851
|
+
if(this.program.uniforms.u_gradient_stop_count) {
|
|
3852
|
+
this.context.uniform1i(this.program.uniforms.u_gradient_stop_count.location, params.stopCount);
|
|
3853
|
+
}
|
|
3854
|
+
|
|
3855
|
+
// 设置每个 stop 的 offset
|
|
3856
|
+
// 关键:必须填充完整的 MAX_STOPS 长度数组,否则未初始化元素默认为 0
|
|
3857
|
+
// 会导致着色器循环中 t >= 0 始终为 true,返回黑色
|
|
3858
|
+
if(this.program.uniforms.u_gradient_offsets) {
|
|
3859
|
+
const offsets = new Float32Array(MAX_STOPS);
|
|
3860
|
+
for(let i = 0; i < params.stopCount; i++) {
|
|
3861
|
+
offsets[i] = params.stops[i * 5];
|
|
3862
|
+
}
|
|
3863
|
+
// 用 2.0 填充剩余项,使 t(0~1) >= 2.0 为 false,不会被匹配
|
|
3864
|
+
for(let i = params.stopCount; i < MAX_STOPS; i++) {
|
|
3865
|
+
offsets[i] = 2.0;
|
|
3866
|
+
}
|
|
3867
|
+
this.context.uniform1fv(this.program.uniforms.u_gradient_offsets.location, offsets);
|
|
3868
|
+
}
|
|
3869
|
+
|
|
3870
|
+
// 设置每个 stop 的颜色 (rgba)
|
|
3871
|
+
if(this.program.uniforms.u_gradient_colors) {
|
|
3872
|
+
const colors = new Float32Array(MAX_STOPS * 4);
|
|
3873
|
+
for(let i = 0; i < params.stopCount; i++) {
|
|
3874
|
+
colors[i * 4 + 0] = params.stops[i * 5 + 1]; // r
|
|
3875
|
+
colors[i * 4 + 1] = params.stops[i * 5 + 2]; // g
|
|
3876
|
+
colors[i * 4 + 2] = params.stops[i * 5 + 3]; // b
|
|
3877
|
+
colors[i * 4 + 3] = params.stops[i * 5 + 4]; // a
|
|
3878
|
+
}
|
|
3879
|
+
// 用最后一个 stop 的颜色填充剩余项,确保不会返回黑色
|
|
3880
|
+
if(params.stopCount > 0) {
|
|
3881
|
+
const lastR = params.stops[(params.stopCount - 1) * 5 + 1];
|
|
3882
|
+
const lastG = params.stops[(params.stopCount - 1) * 5 + 2];
|
|
3883
|
+
const lastB = params.stops[(params.stopCount - 1) * 5 + 3];
|
|
3884
|
+
const lastA = params.stops[(params.stopCount - 1) * 5 + 4];
|
|
3885
|
+
for(let i = params.stopCount; i < MAX_STOPS; i++) {
|
|
3886
|
+
colors[i * 4 + 0] = lastR;
|
|
3887
|
+
colors[i * 4 + 1] = lastG;
|
|
3888
|
+
colors[i * 4 + 2] = lastB;
|
|
3889
|
+
colors[i * 4 + 3] = lastA;
|
|
3890
|
+
}
|
|
3891
|
+
}
|
|
3892
|
+
this.context.uniform4fv(this.program.uniforms.u_gradient_colors.location, colors);
|
|
3893
|
+
}
|
|
3894
|
+
|
|
3895
|
+
// 填充多边形(需要纹理坐标来计算渐变位置)
|
|
3896
|
+
this.fillPolygons(points, true);
|
|
3897
|
+
this.disableVertexAttribArray(this.program.attrs.a_text_coord);
|
|
3898
|
+
}
|
|
3899
|
+
|
|
3704
3900
|
// 区域填充图片
|
|
3705
3901
|
// points绘制的图形顶点
|
|
3706
3902
|
// 图片整体绘制区域
|
|
@@ -3766,6 +3962,29 @@ class WebglPath extends WeblBase {
|
|
|
3766
3962
|
|
|
3767
3963
|
// 规则图形(凸多边形,如圆):直接用 TRIANGLE_FAN 一次性绘制,无需 earcut
|
|
3768
3964
|
if(this.isRegular) {
|
|
3965
|
+
// 检查是否有 moveTo 标记,如果有说明路径包含多个子路径(如空心圆弧 jmHArc)
|
|
3966
|
+
const hasMoveTo = points.some && points.some(p => p.m);
|
|
3967
|
+
if(hasMoveTo) {
|
|
3968
|
+
// 有 m 标记:按 m 标记拆分子路径
|
|
3969
|
+
const { outerPoints, holes } = this.splitSubPaths(points);
|
|
3970
|
+
this.fillWithHoles(outerPoints, holes, isTexture);
|
|
3971
|
+
return;
|
|
3972
|
+
}
|
|
3973
|
+
// 无 m 标记但 needCut=true 表示空心形状(如 jmHArc close=true)
|
|
3974
|
+
// 前半段为内弧,后半段为外弧(反向),按中点拆分
|
|
3975
|
+
if(this.needCut && points.length >= 6) {
|
|
3976
|
+
const mid = Math.floor(points.length / 2);
|
|
3977
|
+
const inner = points.slice(0, mid);
|
|
3978
|
+
const outer = points.slice(mid);
|
|
3979
|
+
const innerArea = Math.abs(this.polygonArea(inner));
|
|
3980
|
+
const outerArea = Math.abs(this.polygonArea(outer));
|
|
3981
|
+
if(outerArea >= innerArea) {
|
|
3982
|
+
this.fillWithHoles(outer, [inner], isTexture);
|
|
3983
|
+
} else {
|
|
3984
|
+
this.fillWithHoles(inner, [outer], isTexture);
|
|
3985
|
+
}
|
|
3986
|
+
return;
|
|
3987
|
+
}
|
|
3769
3988
|
this.writePoints(points);
|
|
3770
3989
|
isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
|
|
3771
3990
|
this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
|
|
@@ -3833,46 +4052,73 @@ class WebglPath extends WeblBase {
|
|
|
3833
4052
|
}
|
|
3834
4053
|
|
|
3835
4054
|
drawText(text, x, y, bounds) {
|
|
3836
|
-
|
|
4055
|
+
// 文本渲染仍需要 2D canvas 绘制字形,然后作为纹理上传
|
|
4056
|
+
// 使用临时 canvas,不依赖共享的 textureCanvas
|
|
4057
|
+
if(!bounds.width || !bounds.height) return null;
|
|
4058
|
+
if(typeof document === 'undefined') return null;
|
|
4059
|
+
|
|
4060
|
+
let canvas = this.__textCanvas;
|
|
3837
4061
|
if(!canvas) {
|
|
3838
|
-
|
|
4062
|
+
canvas = document.createElement('canvas');
|
|
4063
|
+
this.__textCanvas = canvas;
|
|
3839
4064
|
}
|
|
3840
4065
|
canvas.width = bounds.width;
|
|
3841
4066
|
canvas.height = bounds.height;
|
|
3842
4067
|
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
}
|
|
4068
|
+
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
|
4069
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
3846
4070
|
|
|
3847
|
-
this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
|
|
3848
4071
|
// 修改字体
|
|
3849
|
-
|
|
4072
|
+
ctx.font = this.style.font || (this.style.fontSize + 'px ' + this.style.fontFamily);
|
|
3850
4073
|
|
|
3851
4074
|
x -= bounds.left;
|
|
3852
4075
|
y -= bounds.top;
|
|
3853
4076
|
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
4077
|
+
// 设置文本样式
|
|
4078
|
+
if(this.style.fillStyle) {
|
|
4079
|
+
ctx.fillStyle = this.graph.utils.toColor(this.style.fillStyle);
|
|
4080
|
+
}
|
|
4081
|
+
if(this.style.strokeStyle) {
|
|
4082
|
+
ctx.strokeStyle = this.graph.utils.toColor(this.style.strokeStyle);
|
|
4083
|
+
}
|
|
4084
|
+
if(this.style.shadowColor) {
|
|
4085
|
+
ctx.shadowColor = this.graph.utils.toColor(this.style.shadowColor);
|
|
4086
|
+
}
|
|
4087
|
+
if(this.style.shadowBlur) {
|
|
4088
|
+
ctx.shadowBlur = this.style.shadowBlur;
|
|
4089
|
+
}
|
|
4090
|
+
if(this.style.shadowOffsetX !== undefined) {
|
|
4091
|
+
ctx.shadowOffsetX = this.style.shadowOffsetX;
|
|
4092
|
+
}
|
|
4093
|
+
if(this.style.shadowOffsetY !== undefined) {
|
|
4094
|
+
ctx.shadowOffsetY = this.style.shadowOffsetY;
|
|
4095
|
+
}
|
|
4096
|
+
if(this.style.textAlign) {
|
|
4097
|
+
ctx.textAlign = this.style.textAlign;
|
|
4098
|
+
}
|
|
4099
|
+
if(this.style.textBaseline) {
|
|
4100
|
+
ctx.textBaseline = this.style.textBaseline;
|
|
4101
|
+
}
|
|
3857
4102
|
|
|
4103
|
+
if(this.style.fillStyle && ctx.fillText) {
|
|
3858
4104
|
if(this.style.maxWidth) {
|
|
3859
|
-
|
|
4105
|
+
ctx.fillText(text, x, y, this.style.maxWidth);
|
|
3860
4106
|
}
|
|
3861
4107
|
else {
|
|
3862
|
-
|
|
4108
|
+
ctx.fillText(text, x, y);
|
|
3863
4109
|
}
|
|
3864
4110
|
}
|
|
3865
|
-
if(this.
|
|
3866
|
-
|
|
4111
|
+
if(this.style.strokeStyle && ctx.strokeText) {
|
|
3867
4112
|
if(this.style.maxWidth) {
|
|
3868
|
-
|
|
4113
|
+
ctx.strokeText(text, x, y, this.style.maxWidth);
|
|
3869
4114
|
}
|
|
3870
4115
|
else {
|
|
3871
|
-
|
|
4116
|
+
ctx.strokeText(text, x, y);
|
|
3872
4117
|
}
|
|
3873
4118
|
}
|
|
4119
|
+
|
|
3874
4120
|
// 用纹理图片代替文字
|
|
3875
|
-
const data =
|
|
4121
|
+
const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
3876
4122
|
this.fillImage(data, this.points, bounds);
|
|
3877
4123
|
}
|
|
3878
4124
|
}
|
|
@@ -6105,8 +6351,8 @@ class jmHArc extends jmArc {
|
|
|
6105
6351
|
|
|
6106
6352
|
maxps.reverse();//大圆逆序
|
|
6107
6353
|
if(!this.style || !this.style.close) {
|
|
6108
|
-
maxps[0].m = true
|
|
6109
|
-
}
|
|
6354
|
+
maxps[0].m = true;//非闭合时标记 moveTo,分隔内外两个子路径
|
|
6355
|
+
}
|
|
6110
6356
|
this.points = minps.concat(maxps);
|
|
6111
6357
|
}
|
|
6112
6358
|
}
|
|
@@ -8124,8 +8370,6 @@ class jmGraph extends jmControl {
|
|
|
8124
8370
|
else {
|
|
8125
8371
|
this.context = canvas.getContext(this.mode);
|
|
8126
8372
|
}
|
|
8127
|
-
|
|
8128
|
-
this.textureCanvas = option.textureCanvas || null;
|
|
8129
8373
|
|
|
8130
8374
|
// webgl模式
|
|
8131
8375
|
if(this.mode === 'webgl') {
|
|
@@ -8834,10 +9078,14 @@ class jmGraphImpl extends jmGraph {
|
|
|
8834
9078
|
if(typeof option == 'function') {
|
|
8835
9079
|
callback = option;
|
|
8836
9080
|
option = {};
|
|
8837
|
-
|
|
9081
|
+
}
|
|
8838
9082
|
|
|
8839
9083
|
super(canvas, option, callback);
|
|
8840
9084
|
}
|
|
9085
|
+
|
|
9086
|
+
static create(...args) {
|
|
9087
|
+
return new jmGraphImpl(...args);
|
|
9088
|
+
}
|
|
8841
9089
|
}
|
|
8842
9090
|
|
|
8843
9091
|
/**
|