jmgraph 3.2.6 → 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.
- package/README.md +0 -1
- package/build/gulpfile.js +143 -0
- package/build/package-lock.json +19282 -0
- package/build/package.json +71 -0
- package/dist/jmgraph.core.min.js +1 -1
- package/dist/jmgraph.core.min.js.map +1 -1
- package/dist/jmgraph.js +2865 -331
- package/dist/jmgraph.min.js +1 -1
- package/example/ball.html +217 -0
- package/example/base.html +112 -0
- package/example/canvas.html +54 -0
- package/example/cell.html +284 -0
- package/example/controls/arc.html +129 -0
- package/example/controls/arrowline.html +78 -0
- package/example/controls/bezier.html +223 -0
- package/example/controls/img.html +97 -0
- package/example/controls/label.html +87 -0
- package/example/controls/line.html +173 -0
- package/example/controls/prismatic.html +63 -0
- package/example/controls/rect.html +64 -0
- package/example/controls/resize.html +112 -0
- package/example/controls/test.html +136 -0
- package/example/es.html +70 -0
- package/example/es5module.html +64 -0
- package/example/heartarc.html +116 -0
- package/example/index.html +47 -0
- package/example/js/require.js +5 -0
- package/example/love/img/bling/bling.tps +265 -0
- package/example/love/img/bling.json +87 -0
- package/example/love/img/bling.tps +295 -0
- package/example/love/img/doc/bling.gif +0 -0
- package/example/love/img/love.json +95 -0
- package/example/love/img/love.tps +315 -0
- package/example/love/img/qq/qq.tps +399 -0
- package/example/love/img/qq.json +242 -0
- package/example/love/index.html +40 -0
- package/example/love/js/game.js +558 -0
- package/example/music.html +211 -0
- package/example/node/test.js +138 -0
- package/example/pdf.html +187 -0
- package/example/progress.html +173 -0
- package/example/pso.html +148 -0
- package/example/sort.html +816 -0
- package/example/tweenjs.html +84 -0
- package/example/webgl.html +278 -0
- package/example/xfj/img/dr_die.gif +0 -0
- package/example/xfj/index.html +332 -0
- package/example/xfj/shake.js +49 -0
- package/example/xfj/testori.html +76 -0
- package/package.json +56 -56
- package/src/core/jmControl.js +127 -97
- package/src/core/jmEvents.js +2 -2
- package/src/core/jmGradient.js +9 -3
- package/src/core/jmGraph.js +16 -24
- package/src/core/jmPath.js +1 -17
- package/src/core/jmUtils.js +6 -0
- package/src/lib/webgl/base.js +253 -1
- package/src/lib/webgl/core/buffer.js +2 -1
- package/src/lib/webgl/core/program.js +2 -2
- package/src/lib/webgl/core/texture.js +8 -8
- package/src/lib/webgl/gradient.js +11 -38
- package/src/lib/webgl/path.js +118 -235
- package/src/shapes/jmImage.js +18 -3
- package/src/shapes/jmLabel.js +84 -38
- package/src/shapes/jmRect.js +5 -2
package/src/core/jmEvents.js
CHANGED
|
@@ -99,7 +99,7 @@ class jmMouseEvent {
|
|
|
99
99
|
//}
|
|
100
100
|
});
|
|
101
101
|
|
|
102
|
-
this.eventEvents['
|
|
102
|
+
this.eventEvents['mousemove'] = jmUtils.bindEvent(this.target,'mousemove',function(evt) {
|
|
103
103
|
evt = evt || window.event;
|
|
104
104
|
evt.eventName = 'mousemove';
|
|
105
105
|
let target = evt.target || evt.srcElement;
|
|
@@ -112,7 +112,7 @@ class jmMouseEvent {
|
|
|
112
112
|
}
|
|
113
113
|
});
|
|
114
114
|
|
|
115
|
-
this.eventEvents['
|
|
115
|
+
this.eventEvents['mouseover'] = jmUtils.bindEvent(this.target,'mouseover',function(evt) {
|
|
116
116
|
evt = evt || window.event;
|
|
117
117
|
evt.eventName = 'mouseover';
|
|
118
118
|
container.raiseEvent('mouseover',evt);
|
package/src/core/jmGradient.js
CHANGED
|
@@ -13,8 +13,12 @@ export default class jmGradient {
|
|
|
13
13
|
|
|
14
14
|
if(opt && typeof opt == 'object') {
|
|
15
15
|
for(let k in opt) {
|
|
16
|
+
if(k === 'stops') continue;
|
|
16
17
|
this[k] = opt[k];
|
|
17
18
|
}
|
|
19
|
+
if(opt.stops && Array.isArray(opt.stops)) {
|
|
20
|
+
this.stops.push(...opt.stops);
|
|
21
|
+
}
|
|
18
22
|
}
|
|
19
23
|
//解析字符串格式
|
|
20
24
|
//linear-gradient(direction, color-stop1, color-stop2, ...);
|
|
@@ -87,6 +91,7 @@ export default class jmGradient {
|
|
|
87
91
|
if(this.type === 'linear') {
|
|
88
92
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
89
93
|
gradient = control.webglControl.createLinearGradient(x1, y1, x2, y2, bounds);
|
|
94
|
+
gradient.key = this.toString();
|
|
90
95
|
}
|
|
91
96
|
else {
|
|
92
97
|
context.createLinearGradient && (gradient = context.createLinearGradient(sx1, sy1, sx2, sy2));
|
|
@@ -105,6 +110,7 @@ export default class jmGradient {
|
|
|
105
110
|
}
|
|
106
111
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
107
112
|
gradient = control.webglControl.createRadialGradient(x1, y1, r1, x2, y2, r2, bounds);
|
|
113
|
+
gradient.key = this.toString();
|
|
108
114
|
}
|
|
109
115
|
//offsetLine = Math.abs(r2 - r1);//二圆半径差
|
|
110
116
|
else if(context.createRadialGradient) {
|
|
@@ -206,14 +212,14 @@ export default class jmGradient {
|
|
|
206
212
|
toString() {
|
|
207
213
|
let str = this.type + '-gradient(';
|
|
208
214
|
if(this.type == 'linear') {
|
|
209
|
-
str += this.x1
|
|
215
|
+
str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
|
|
210
216
|
}
|
|
211
217
|
else {
|
|
212
|
-
str += this.x1
|
|
218
|
+
str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
|
|
213
219
|
}
|
|
214
220
|
//颜色渐变
|
|
215
221
|
this.stops.each(function(i,s) {
|
|
216
|
-
str += ',' + s.color + ' ' + s.offset
|
|
222
|
+
str += ',' + s.color + ' ' + s.offset;
|
|
217
223
|
});
|
|
218
224
|
return str + ')';
|
|
219
225
|
}
|
package/src/core/jmGraph.js
CHANGED
|
@@ -27,7 +27,7 @@ export default class jmGraph extends jmControl {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
option = option || {};
|
|
30
|
-
option.mode = '2d'; // webgl | 2d 暂不支持webgl
|
|
30
|
+
//option.mode = '2d'; // webgl | 2d 暂不支持webgl
|
|
31
31
|
option.interactive = true;
|
|
32
32
|
option.isRegular = true;// 规则的
|
|
33
33
|
|
|
@@ -170,20 +170,7 @@ export default class jmGraph extends jmControl {
|
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
this.context.viewport && this.context.viewport(0, 0, w, h);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 内部坐标转为页面坐标,这里主要是有devicePixelRatio倍数问题
|
|
177
|
-
* @param {x, y} point 内部坐标
|
|
178
|
-
*/
|
|
179
|
-
pointToPixes(point) {
|
|
180
|
-
if(this.dprScaleSize && this.dprScaleSize !== 1) {
|
|
181
|
-
point = Object.assign({}, point, {
|
|
182
|
-
x: point.x / this.dprScaleSize,
|
|
183
|
-
y: point.y / this.dprScaleSize
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
return point;
|
|
173
|
+
this.needUpdate = true;
|
|
187
174
|
}
|
|
188
175
|
|
|
189
176
|
/**
|
|
@@ -304,13 +291,14 @@ export default class jmGraph extends jmControl {
|
|
|
304
291
|
* @param {number} y2 线性渐变结束点Y坐标
|
|
305
292
|
* @return {jmGradient} 线性渐变对象
|
|
306
293
|
*/
|
|
307
|
-
createLinearGradient(x1, y1, x2, y2) {
|
|
294
|
+
createLinearGradient(x1, y1, x2, y2, stops=[]) {
|
|
308
295
|
const gradient = new jmGradient({
|
|
309
296
|
type:'linear',
|
|
310
297
|
x1: x1,
|
|
311
298
|
y1: y1,
|
|
312
299
|
x2: x2,
|
|
313
|
-
y2: y2
|
|
300
|
+
y2: y2,
|
|
301
|
+
stops
|
|
314
302
|
});
|
|
315
303
|
return gradient;
|
|
316
304
|
}
|
|
@@ -327,7 +315,7 @@ export default class jmGraph extends jmControl {
|
|
|
327
315
|
* @param {number} r2 放射渐变大圆半径
|
|
328
316
|
* @return {jmGradient} 放射渐变对象
|
|
329
317
|
*/
|
|
330
|
-
createRadialGradient(x1, y1, r1, x2, y2, r2) {
|
|
318
|
+
createRadialGradient(x1, y1, r1, x2, y2, r2, stops=[]) {
|
|
331
319
|
const gradient = new jmGradient({
|
|
332
320
|
type:'radial',
|
|
333
321
|
x1: x1,
|
|
@@ -335,7 +323,8 @@ export default class jmGraph extends jmControl {
|
|
|
335
323
|
r1: r1,
|
|
336
324
|
x2: x2,
|
|
337
325
|
y2: y2,
|
|
338
|
-
r2: r2
|
|
326
|
+
r2: r2,
|
|
327
|
+
stops
|
|
339
328
|
});
|
|
340
329
|
return gradient;
|
|
341
330
|
}
|
|
@@ -505,14 +494,14 @@ export default class jmGraph extends jmControl {
|
|
|
505
494
|
};
|
|
506
495
|
}
|
|
507
496
|
|
|
508
|
-
this.context.scale(dx,dy);
|
|
497
|
+
//this.context.scale && this.context.scale(dx,dy);
|
|
509
498
|
if(!this.scaleSize) {
|
|
510
|
-
this.scaleSize = {x:
|
|
499
|
+
this.scaleSize = {x: 1,y: 1};
|
|
511
500
|
}
|
|
512
501
|
else {
|
|
513
|
-
this.scaleSize = {x:dx * this.scaleSize.x, y:dy * this.scaleSize.y};
|
|
502
|
+
this.scaleSize = {x: dx * this.scaleSize.x, y: dy * this.scaleSize.y};
|
|
514
503
|
}
|
|
515
|
-
this.
|
|
504
|
+
this.canvas.style && (this.canvas.style.transform = `scale(${this.scaleSize.x}, ${this.scaleSize.y})`);
|
|
516
505
|
}
|
|
517
506
|
|
|
518
507
|
/**
|
|
@@ -535,14 +524,17 @@ export default class jmGraph extends jmControl {
|
|
|
535
524
|
const self = this;
|
|
536
525
|
this.___isAutoRefreshing = true;
|
|
537
526
|
|
|
527
|
+
const refreshStartTime = Date.now();
|
|
538
528
|
function update() {
|
|
539
529
|
if(self.destroyed) {
|
|
540
530
|
self.___isAutoRefreshing = false;
|
|
541
531
|
return;// 已销毁
|
|
542
532
|
}
|
|
543
533
|
if(self.needUpdate) self.redraw();
|
|
534
|
+
|
|
535
|
+
const time = Date.now() - refreshStartTime;
|
|
544
536
|
// 触发刷新事件
|
|
545
|
-
self.emit('update');
|
|
537
|
+
self.emit('update', time);
|
|
546
538
|
|
|
547
539
|
self.__requestAnimationFrameFunHandler && self.cancelAnimationFrame(self.__requestAnimationFrameFunHandler);
|
|
548
540
|
self.__requestAnimationFrameFunHandler = self.requestAnimationFrame(update);
|
package/src/core/jmPath.js
CHANGED
|
@@ -13,7 +13,6 @@ export default class jmPath extends jmControl {
|
|
|
13
13
|
constructor(params, t='jmPath') {
|
|
14
14
|
super(params, t);
|
|
15
15
|
this.points = params && params.points ? params.points : [];
|
|
16
|
-
this.polygonIndices = params && params.polygonIndices ? params.polygonIndices : [];
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
/**
|
|
@@ -29,22 +28,7 @@ export default class jmPath extends jmControl {
|
|
|
29
28
|
set points(v) {
|
|
30
29
|
this.needUpdate = true;
|
|
31
30
|
return this.property('points', v);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 顶点数组索引,对应points中的顶点
|
|
36
|
-
* @property polygonIndices
|
|
37
|
-
* @type {array}
|
|
38
|
-
*/
|
|
39
|
-
get polygonIndices() {
|
|
40
|
-
let s = this.property('polygonIndices');
|
|
41
|
-
return s;
|
|
42
|
-
}
|
|
43
|
-
set polygonIndices(v) {
|
|
44
|
-
this.needUpdate = true;
|
|
45
|
-
return this.property('polygonIndices', v);
|
|
46
|
-
}
|
|
47
|
-
|
|
31
|
+
}
|
|
48
32
|
|
|
49
33
|
}
|
|
50
34
|
|
package/src/core/jmUtils.js
CHANGED
|
@@ -883,6 +883,12 @@ export default class jmUtils {
|
|
|
883
883
|
b = typeof color.b !== 'undefined'? color.b: b;
|
|
884
884
|
a = typeof color.a !== 'undefined'? color.a: a;
|
|
885
885
|
}
|
|
886
|
+
if(r && typeof r === 'object') {
|
|
887
|
+
g = r.g;
|
|
888
|
+
b = r.b;
|
|
889
|
+
a = r.a || 1;
|
|
890
|
+
r = r.r;
|
|
891
|
+
}
|
|
886
892
|
if(typeof r != 'undefined' && typeof g != 'undefined' && typeof b != 'undefined') {
|
|
887
893
|
if(typeof a != 'undefined') {
|
|
888
894
|
return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
|
package/src/lib/webgl/base.js
CHANGED
|
@@ -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
|
-
|
|
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.
|
|
11
|
-
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.
|
|
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
|
|
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.
|
|
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 =
|
|
63
|
+
gradient = control.textureContext.createLinearGradient(this.x1, this.y1, this.x2, this.y2);
|
|
79
64
|
}
|
|
80
65
|
else {
|
|
81
|
-
gradient =
|
|
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
|
-
|
|
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
|
|
169
|
+
export default WebglGradient;
|