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