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