jmgraph 3.2.7 → 3.2.8

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.
Files changed (51) hide show
  1. package/README.md +0 -1
  2. package/dist/jmgraph.core.min.js +1 -1
  3. package/dist/jmgraph.core.min.js.map +1 -1
  4. package/dist/jmgraph.js +2834 -325
  5. package/dist/jmgraph.min.js +1 -1
  6. package/example/ball.html +8 -14
  7. package/example/cell.html +1 -1
  8. package/example/controls/arc.html +10 -7
  9. package/example/controls/arrowline.html +1 -0
  10. package/example/controls/img.html +30 -23
  11. package/example/controls/label.html +6 -4
  12. package/example/controls/line.html +54 -51
  13. package/example/controls/resize.html +45 -8
  14. package/example/index.html +2 -1
  15. package/example/music.html +101 -82
  16. package/example/webgl.html +48 -25
  17. package/package.json +56 -56
  18. package/src/core/jmControl.js +124 -95
  19. package/src/core/jmEvents.js +2 -2
  20. package/src/core/jmGradient.js +5 -3
  21. package/src/core/jmGraph.js +10 -20
  22. package/src/core/jmPath.js +1 -17
  23. package/src/core/jmUtils.js +6 -0
  24. package/src/lib/webgl/base.js +253 -1
  25. package/src/lib/webgl/core/buffer.js +2 -1
  26. package/src/lib/webgl/core/program.js +2 -2
  27. package/src/lib/webgl/core/texture.js +8 -8
  28. package/src/lib/webgl/gradient.js +11 -38
  29. package/src/lib/webgl/path.js +118 -235
  30. package/src/shapes/jmImage.js +18 -3
  31. package/src/shapes/jmLabel.js +84 -38
  32. package/src/shapes/jmRect.js +5 -2
  33. package/docs/_config.yml +0 -1
  34. package/docs/about.html +0 -41
  35. package/docs/api/jmGraph.md +0 -2302
  36. package/docs/css/index.css +0 -131
  37. package/docs/images/ball.gif +0 -0
  38. package/docs/images/bezier.gif +0 -0
  39. package/docs/images/cell.gif +0 -0
  40. package/docs/images/chart.gif +0 -0
  41. package/docs/images/editor.gif +0 -0
  42. package/docs/images/sort.gif +0 -0
  43. package/docs/index.html +0 -80
  44. package/docs/js/helper.js +0 -89
  45. package/docs/js/jquery.min.js +0 -6
  46. package/example/love/img/music/bg.mp3 +0 -0
  47. package/example/love/img/music/bg_2019130144035.mp3 +0 -0
  48. package/example/love/img/music/f.mp3 +0 -0
  49. package/example/love/img/music/fail.mp3 +0 -0
  50. package/example/love/img/music/s.mp3 +0 -0
  51. package/example/love/img/music/s_201913014415.mp3 +0 -0
@@ -21,6 +21,85 @@ import {
21
21
  deleteTexture
22
22
  } from './core/texture.js';
23
23
 
24
+ // 把canvas坐标转为webgl坐标系
25
+ const convertPointSource = `
26
+ vec4 translatePosition(vec4 point, float x, float y) {
27
+ point.x = (point.x-x)/x;
28
+ point.y = (y-point.y)/y;
29
+ return point;
30
+ }`;
31
+ // 把纹理的canvas坐标转为纹理的坐标系
32
+ const convertTexturePosition = `
33
+ vec2 translateTexturePosition(in vec2 point, vec4 bounds) {
34
+ point.x = (point.x-bounds.x)/bounds.z; // 离左上角位置的X长比上纹理宽 0-1
35
+ point.y = 1.0-(point.y-bounds.y)/bounds.w; // 离左上角位置的Y长比上高,因为纹理坐标是左下角起,所以要用1-
36
+ return point;
37
+ }`;
38
+
39
+ // path顶点着色器源码
40
+ const pathVertexSource = `
41
+ attribute vec4 a_position;
42
+ attribute vec4 a_color;
43
+ attribute vec2 a_text_coord;
44
+ uniform vec2 a_center_point; // 当前canvas的中心位置
45
+ uniform float a_point_size; // 点的大小
46
+ uniform int a_type;
47
+ varying vec4 v_color;
48
+ varying vec2 v_text_coord;
49
+ varying float v_type;
50
+
51
+ ${convertPointSource}
52
+
53
+ void main() {
54
+ gl_PointSize = a_point_size == 0.0? 1.0 : a_point_size;
55
+ v_type = float(a_type);
56
+ vec4 pos = translatePosition(a_position, a_center_point.x, a_center_point.y);
57
+ gl_Position = pos;
58
+ v_color = a_color;
59
+ if(a_type == 2) {
60
+ v_text_coord = a_text_coord;
61
+ }
62
+ }
63
+ `;
64
+ // path 片段着色器源码
65
+ const pathFragmentSource = `
66
+ precision mediump float;
67
+ uniform sampler2D u_sample;
68
+ uniform vec4 v_texture_bounds; // 纹理的左上坐标和大小 x,y,z,w
69
+ uniform vec4 v_single_color;
70
+ varying float v_type;
71
+ varying vec4 v_color;
72
+ varying vec2 v_text_coord;
73
+
74
+ ${convertTexturePosition}
75
+
76
+ void main() {
77
+ // 如果是fill,则直接填充颜色
78
+ if(v_type == 1.0) {
79
+ gl_FragColor = v_single_color;
80
+ }
81
+ // 渐变色
82
+ else if(v_type == 3.0) {
83
+ gl_FragColor = v_color;
84
+ }
85
+ else if(v_type == 2.0) {
86
+ vec2 pos = translateTexturePosition(v_text_coord, v_texture_bounds);
87
+ gl_FragColor = texture2D(u_sample, pos);
88
+ }
89
+ else {
90
+ float r = distance(gl_PointCoord, vec2(0.5, 0.5));
91
+ //根据距离设置片元
92
+ if(r <= 0.5){
93
+ // 方形区域片元距离几何中心半径小于0.5,像素颜色设置红色
94
+ gl_FragColor = v_single_color;
95
+ }else {
96
+ // 方形区域距离几何中心半径不小于0.5的片元剪裁舍弃掉:
97
+ discard;
98
+ }
99
+ }
100
+ }
101
+ `;
102
+
24
103
  class WeblBase {
25
104
  constructor(graph, option) {
26
105
  this.graph = graph;
@@ -34,8 +113,89 @@ class WeblBase {
34
113
  if(this.graph) return this.graph.context;
35
114
  }
36
115
 
116
+ // 纹理绘制canvas
117
+ get textureCanvas() {
118
+ let canvas = this.graph.textureCanvas;
119
+ if(!canvas) {
120
+ if(typeof document === 'undefined') return null;
121
+ canvas = this.graph.textureCanvas = document.createElement('canvas');
122
+ }
123
+ return canvas;
124
+ }
125
+ // 纹理绘制canvas ctx
126
+ get textureContext() {
127
+ const ctx = this.textureCanvas.ctx || (this.textureCanvas.ctx = this.textureCanvas.getContext('2d', {
128
+ willReadFrequently: true
129
+ }));
130
+ return ctx;
131
+ }
132
+
133
+ // i当前程序
134
+ get program() {
135
+ // 默认所有path用同一个编译好的program
136
+ return this.graph.context.pathProgram || (this.graph.context.pathProgram=this.createProgram(pathVertexSource, pathFragmentSource));
137
+ }
138
+
139
+ // 设置样式
140
+ setStyle(style = this.style, value = '') {
141
+
142
+ if(typeof style === 'string') {
143
+ const obj = {};
144
+ obj[style] = value;
145
+ style = obj;
146
+ }
147
+ /*
148
+ // 设置线条颜色或填充色
149
+ if(style.strokeStyle) {
150
+ let color = style.strokeStyle;
151
+ if(typeof color === 'string') color = this.graph.utils.hexToRGBA(color);
152
+ this.style.strokeStyle = this.graph.utils.rgbToDecimal(color);
153
+ delete style.strokeStyle;
154
+ }
155
+ else if(style.fillStyle) {
156
+ let color = style.fillStyle;
157
+ if(this.isGradient(color)) {
158
+ this.style.fillStyle = color;
159
+ }
160
+ else {
161
+ if(typeof color === 'string') color = this.graph.utils.hexToRGBA(color);
162
+ this.style.fillStyle = this.graph.utils.rgbToDecimal(color);
163
+ }
164
+ delete style.fillStyle;
165
+ } */
166
+
167
+ this.style = {
168
+ ...this.style,
169
+ ...style
170
+ }
171
+ }
172
+
173
+ // 把传统颜色转为webgl识别的
174
+ convertColor(color) {
175
+ if(this.isGradient(color)) return color;
176
+ if(typeof color === 'string') color = this.graph.utils.hexToRGBA(color);
177
+ return this.graph.utils.rgbToDecimal(color);
178
+ }
179
+
180
+ setTextureStyle(style, value='') {
181
+
182
+ if(typeof style === 'string') {
183
+ if(['fillStyle', 'strokeStyle', 'shadowColor'].indexOf(style) > -1) {
184
+ value = this.graph.utils.toColor(value);
185
+ }
186
+ this.textureContext[style] = value;
187
+ }
188
+ else {
189
+ for(const name in style) {
190
+ if(name === 'constructor') continue;
191
+ this.setTextureStyle(name, style[name]);
192
+ }
193
+ }
194
+ }
195
+
37
196
  // 创建程序
38
- createProgram(vertexSrc, fragmentSrc) {
197
+ createProgram(vertexSrc, fragmentSrc) {
198
+ this.context.lineWidth(1);
39
199
  return createProgram(this.context, vertexSrc, fragmentSrc);
40
200
  }
41
201
 
@@ -196,6 +356,98 @@ class WeblBase {
196
356
  isGradient(obj) {
197
357
  return obj && obj instanceof webglGradient;
198
358
  }
359
+
360
+ /**
361
+ * 测试获取文本所占大小
362
+ *
363
+ * @method testSize
364
+ * @return {object} 含文本大小的对象
365
+ */
366
+ testSize(text, style=this.style) {
367
+
368
+ this.textureContext.save && this.textureContext.save();
369
+ // 修改字体,用来计算
370
+ if(style.font || style.fontSize) this.textureContext.font = style.font || (style.fontSize + 'px ' + style.fontFamily);
371
+
372
+ //计算宽度
373
+ const size = this.textureContext.measureText?
374
+ this.textureContext.measureText(text):
375
+ {width:15};
376
+ this.textureContext.restore &&this.textureContext.restore();
377
+ size.height = this.style.fontSize? this.style.fontSize: 15;
378
+ return size;
379
+ }
380
+
381
+ // 使用纹理canvas生成图,
382
+ // 填充可以是颜色或渐变对象
383
+ // 如果指定了points,则表明要绘制不规则的图形
384
+ toFillTexture(fillStyle, bounds, points=null) {
385
+ const canvas = this.textureCanvas;
386
+ if(!canvas) {
387
+ return fillStyle;
388
+ }
389
+ canvas.width = bounds.width;
390
+ canvas.height = bounds.height;
391
+
392
+ if(!canvas.width || !canvas.height) {
393
+ return fillStyle;
394
+ }
395
+
396
+ this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
397
+
398
+ this.textureContext.fillStyle = fillStyle;
399
+
400
+ this.textureContext.beginPath();
401
+ if(!points || !points.length) {
402
+ points = [];
403
+ points.push({
404
+ x: bounds.left,
405
+ y: bounds.top
406
+ });
407
+ points.push({
408
+ x: bounds.left + bounds.width,
409
+ y: bounds.top
410
+ });
411
+ points.push({
412
+ x: bounds.left + bounds.width,
413
+ y: bounds.top + bounds.height
414
+ });
415
+ points.push({
416
+ x: bounds.left,
417
+ y: bounds.top + bounds.height
418
+ });
419
+ points.push({
420
+ x: bounds.left,
421
+ y: bounds.top
422
+ });
423
+ }
424
+ if(points && points.length) {
425
+ for(const p of points) {
426
+ //移至当前坐标
427
+ if(p.m) {
428
+ this.textureContext.moveTo(p.x - bounds.left, p.y - bounds.top);
429
+ }
430
+ else {
431
+ this.textureContext.lineTo(p.x - bounds.left, p.y - bounds.top);
432
+ }
433
+ }
434
+ }
435
+ else {
436
+ this.textureContext.moveTo(0, 0);
437
+ this.textureContext.lineTo(bounds.width, 0);
438
+ this.textureContext.lineTo(bounds.width, bounds.height);
439
+ this.textureContext.lineTo(0, bounds.height);
440
+ this.textureContext.lineTo(0, 0);
441
+ }
442
+ this.textureContext.closePath();
443
+ this.textureContext.fill();
444
+
445
+ const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
446
+ return {
447
+ data,
448
+ points
449
+ };
450
+ }
199
451
  }
200
452
 
201
453
  export default WeblBase;
@@ -10,7 +10,8 @@ function createBuffer(gl, data, type=gl.ARRAY_BUFFER, drawType=gl.STATIC_DRAW) {
10
10
  gl.bindBuffer(type, buffer);
11
11
  //写入坐标数据
12
12
  // 因为会将数据发送到 GPU,为了省去数据解析,这里使用 Float32Array 直接传送数据
13
- gl.bufferData(type, data, drawType); // 表示缓冲区的内容不会经常更改
13
+ // data.buffer这里要使用data.buffer,否则在edge下可能导至数据发生较大的改变
14
+ gl.bufferData(type, data.buffer || data, drawType); // 表示缓冲区的内容不会经常更改
14
15
  return {
15
16
  type,
16
17
  drawType,
@@ -101,7 +101,7 @@ function extractUniforms(gl, program) {
101
101
  // strip: 步长 数组中一行长度,0 表示数据是紧密的没有空隙,让OpenGL决定具体步长
102
102
  // offset: 字节偏移量,必须是类型的字节长度的倍数。
103
103
  // dataType: 每个元素的数据类型
104
- function writeVertexAttrib(gl, buffer, attr, size=2,strip=0,offset=0,dataType=gl.FLOAT) {
104
+ function writeVertexAttrib(gl, buffer, attr, size=2, strip=0, offset=0, dataType=gl.FLOAT) {
105
105
  gl.bindBuffer(buffer.type, buffer.buffer);
106
106
  gl.vertexAttribPointer( // 告诉 OpenGL 如何从 Buffer 中获取数据
107
107
  attr.location, // 顶点属性的索引
@@ -110,7 +110,7 @@ function writeVertexAttrib(gl, buffer, attr, size=2,strip=0,offset=0,dataType=gl
110
110
  false, // 是否归一化到特定的范围,对 FLOAT 类型数据设置无效
111
111
  strip * buffer.unitSize,
112
112
  offset
113
- )
113
+ );
114
114
  gl.enableVertexAttribArray(attr.location);
115
115
  return buffer;
116
116
  }
@@ -2,15 +2,15 @@
2
2
  // 生成纹理
3
3
  function create2DTexture(gl) {
4
4
  const texture = gl.createTexture();
5
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1) // 图像反转Y轴
6
- gl.activeTexture(gl.TEXTURE0) // 激活纹理单元
7
- gl.bindTexture(gl.TEXTURE_2D, texture) // 绑定纹理对象
5
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 图像反转Y轴
6
+ gl.activeTexture(gl.TEXTURE0); // 激活纹理单元
7
+ gl.bindTexture(gl.TEXTURE_2D, texture); // 绑定纹理对象
8
8
 
9
-
10
- gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) // 放大处理方式
11
- gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) // 缩小处理方式
12
- gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) // 水平平铺方式
13
- gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) // 竖直平铺方式
9
+ //gl.generateMipmap(gl.TEXTURE_2D);
10
+ gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 放大处理方式 // LINEAR / NEAREST
11
+ gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 缩小处理方式
12
+ gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 水平平铺方式
13
+ gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 竖直平铺方式
14
14
 
15
15
 
16
16
  return texture;
@@ -1,6 +1,6 @@
1
1
  const WebglGradientTextureCache = {};
2
2
  // 渐变
3
- class WeblGradient {
3
+ class WebglGradient {
4
4
  // type:[linear= 线性渐变,radial=放射性渐变]
5
5
  constructor(type='linear', params={}) {
6
6
  this.type = type || 'linear';
@@ -52,54 +52,27 @@ class WeblGradient {
52
52
  }
53
53
 
54
54
  // 转为渐变为纹理
55
- toImageData(control, bounds) {
56
- const key = this.toString() + `-${bounds.width.toFixed(4)}x${bounds.height.toFixed(4)}`;
57
- if(WebglGradientTextureCache[key]) return WebglGradientTextureCache[key];
58
-
59
- let canvas = control.graph.textureCanvas;
60
- if(!canvas) {
61
- canvas = control.graph.textureCanvas = document.createElement('canvas');
62
- }
63
- canvas.width = bounds.width;
64
- canvas.height = bounds.height;
65
-
66
- if(!canvas.width || !canvas.height) {
55
+ toImageData(control, bounds, points=null) {
56
+ //const key = this.key || this.toString();
57
+ //if(WebglGradientTextureCache[key]) return WebglGradientTextureCache[key];
58
+ if(!control.textureContext) {
67
59
  return null;
68
60
  }
69
-
70
- const ctx = canvas.getContext('2d', {
71
- willReadFrequently: true
72
- });
73
-
74
- ctx.clearRect(0, 0, canvas.width, canvas.height);
75
-
76
61
  let gradient = null;
77
62
  if(this.type === 'linear') {
78
- gradient = ctx.createLinearGradient(this.x1, this.y1, this.x2, this.y2);
63
+ gradient = control.textureContext.createLinearGradient(this.x1, this.y1, this.x2, this.y2);
79
64
  }
80
65
  else {
81
- gradient = ctx.createRadialGradient(this.x1, this.y1, this.r1, this.x2, this.y2, this.r2);
66
+ gradient = control.textureContext.createRadialGradient(this.x1, this.y1, this.r1, this.x2, this.y2, this.r2);
82
67
  }
83
68
  this.stops.forEach(function(s, i) {
84
69
  const c = control.graph.utils.toColor(s.color);
85
70
  gradient && gradient.addColorStop(s.offset, c);
86
71
  });
87
- ctx.fillStyle = gradient;
88
-
89
- ctx.beginPath();
90
-
91
- ctx.moveTo(0, 0);
92
- ctx.lineTo(bounds.width, 0);
93
- ctx.lineTo(bounds.width, bounds.height);
94
- ctx.lineTo(0, bounds.height);
95
- ctx.lineTo(0, 0);
96
-
97
- ctx.closePath();
98
- ctx.fill();
99
-
100
- const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
72
+
73
+ const data = control.toFillTexture(gradient, bounds, points);
101
74
 
102
- WebglGradientTextureCache[key] = data;
75
+ //WebglGradientTextureCache[key] = data;
103
76
 
104
77
  return data;
105
78
  }
@@ -193,4 +166,4 @@ class WeblGradient {
193
166
  }
194
167
  }
195
168
 
196
- export default WeblGradient;
169
+ export default WebglGradient;