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