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