jmgraph 3.2.18 → 3.2.19

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.
@@ -15,7 +15,7 @@
15
15
  var g = jmGraph.create('mycanvas', {
16
16
  width: 800,
17
17
  height: 600,
18
- mode: '2d'
18
+ mode: 'webgl'
19
19
  });
20
20
 
21
21
  //实时更新画布
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "family": "jmgraph",
3
3
  "name": "jmgraph",
4
- "version": "3.2.18",
4
+ "version": "3.2.19",
5
5
  "description": "一个简单的canvas画图库",
6
6
  "homepage": "https://surl.fit/tools/tools/jmgraph",
7
7
  "keywords": [
@@ -73,7 +73,13 @@ export default class jmGraph extends jmControl {
73
73
  }
74
74
  }
75
75
  this.canvas = canvas;
76
- this.context = canvas.getContext(this.mode);
76
+ // Create context with preserveDrawingBuffer for webgl to prevent flickering
77
+ if(this.mode === 'webgl') {
78
+ this.context = canvas.getContext(this.mode, { preserveDrawingBuffer: true });
79
+ }
80
+ else {
81
+ this.context = canvas.getContext(this.mode);
82
+ }
77
83
 
78
84
  this.textureCanvas = option.textureCanvas || null;
79
85
 
@@ -377,6 +377,10 @@ class WeblBase {
377
377
  // 多边切割, 得到三角形顶点
378
378
  // polygonIndices 顶点索引,
379
379
  earCutPointsToTriangles(points) {
380
+ this.earCutCache = this.earCutCache || (this.earCutCache = {});
381
+ const key = JSON.stringify(points);
382
+ if (this.earCutCache[key]) return this.earCutCache[key];
383
+
380
384
  const ps = this.earCutPoints(points);// 切割得到3角色顶点索引,
381
385
  const triangles = [];
382
386
  // 用顶点索引再组合成坐标数组
@@ -387,6 +391,8 @@ class WeblBase {
387
391
 
388
392
  triangles.push([p1, p2, p3]);// 每三个顶点构成一个三角
389
393
  }
394
+
395
+ this.earCutCache[key] = triangles;
390
396
  return triangles;
391
397
  }
392
398
 
@@ -371,9 +371,9 @@ class WebglPath extends WebglBase {
371
371
  // 分割成一个个规则的三角形,不规则的多边形不全割的话纹理就会没法正确覆盖
372
372
  getTriangles(points) {
373
373
 
374
- //this.trianglesCache = this.trianglesCache||(this.trianglesCache={});
375
- //const key = JSON.stringify(points);
376
- //if(this.trianglesCache[key]) return this.trianglesCache[key];
374
+ this.trianglesCache = this.trianglesCache||(this.trianglesCache={});
375
+ const key = JSON.stringify(points);
376
+ if(this.trianglesCache[key]) return this.trianglesCache[key];
377
377
 
378
378
  const res = [];
379
379
  const polygons = this.getPolygon(points);
@@ -384,7 +384,7 @@ class WebglPath extends WebglBase {
384
384
  res.push(...triangles);
385
385
  }
386
386
  }
387
- //this.trianglesCache[key] = res;
387
+ this.trianglesCache[key] = res;
388
388
  return res;
389
389
  }
390
390
 
@@ -486,18 +486,15 @@ class WebglPath extends WebglBase {
486
486
 
487
487
  // 进行多边形填充
488
488
  fillPolygons(points, isTexture = false) {
489
- //const indexBuffer = this.createUint16Buffer(triangles, this.context.ELEMENT_ARRAY_BUFFER);
490
- //this.context.drawElements(this.context.TRIANGLES, triangles.length, this.context.UNSIGMED_SHORT, 0);
491
- //this.deleteBuffer(indexBuffer);
492
- /*if(points.length > 3 && (!regular || this.needCut)) {
493
- const triangles = regular && this.needCut? this.earCutPointsToTriangles(points): this.getTriangles(points);
489
+ if(points.length > 3) {
490
+ const triangles = this.needCut? this.earCutPointsToTriangles(points): this.getTriangles(points);
494
491
  if(triangles.length) {
495
492
  for(const triangle of triangles) {
496
493
  this.fillPolygons(triangle, isTexture);// 这里就变成了规则的图形了
497
494
  }
498
495
  }
499
496
  }
500
- else {*/
497
+ else {
501
498
  const buffer = this.writePoints(points);
502
499
  // 纹理坐标
503
500
  const coordBuffer = isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
@@ -505,7 +502,7 @@ class WebglPath extends WebglBase {
505
502
  this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
506
503
  this.deleteBuffer(buffer);
507
504
  coordBuffer && this.deleteBuffer(coordBuffer);
508
- //}
505
+ }
509
506
  }
510
507
 
511
508
  // 填充图形