jmgraph 3.2.2 → 3.2.3
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 +4 -4
- package/dist/jmgraph.core.min.js +1 -1
- package/dist/jmgraph.core.min.js.map +1 -1
- package/dist/jmgraph.js +3064 -351
- package/dist/jmgraph.min.js +1 -1
- package/index.js +4 -0
- package/package.json +4 -4
- package/src/core/jmControl.js +97 -57
- package/src/core/jmGradient.js +29 -14
- package/src/core/jmGraph.js +70 -26
- package/src/core/jmObject.js +2 -3
- package/src/core/jmPath.js +19 -3
- package/src/core/jmProperty.js +29 -14
- package/src/core/jmUtils.js +218 -31
- package/src/lib/earcut.js +680 -0
- package/src/lib/earcut.md +73 -0
- package/src/lib/webgl/base.js +201 -0
- package/src/lib/webgl/core/buffer.js +48 -0
- package/src/lib/webgl/core/mapSize.js +40 -0
- package/src/lib/webgl/core/mapType.js +43 -0
- package/src/lib/webgl/core/program.js +139 -0
- package/src/lib/webgl/core/shader.js +14 -0
- package/src/lib/webgl/core/texture.js +61 -0
- package/src/lib/webgl/gradient.js +196 -0
- package/src/lib/webgl/index.js +11 -0
- package/src/lib/webgl/path.js +679 -0
- package/src/shapes/jmArc.js +15 -11
- package/src/shapes/jmArrow.js +10 -10
- package/src/shapes/jmBezier.js +2 -2
- package/src/shapes/jmCircle.js +8 -1
- package/src/shapes/jmHArc.js +17 -15
- package/src/shapes/jmImage.js +68 -38
- package/src/shapes/jmLabel.js +11 -10
- package/src/shapes/jmLine.js +20 -12
- package/src/shapes/jmPrismatic.js +4 -2
- package/src/shapes/jmRect.js +5 -4
- package/src/shapes/jmResize.js +6 -4
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"family": "jmgraph",
|
|
3
3
|
"name": "jmgraph",
|
|
4
|
-
"version": "3.2.
|
|
4
|
+
"version": "3.2.3",
|
|
5
5
|
"description": "一个简单的canvas画图库",
|
|
6
6
|
"homepage": "http://graph.jm47.com/",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"canvas",
|
|
9
|
-
"html5"
|
|
9
|
+
"html5",
|
|
10
|
+
"webgl"
|
|
10
11
|
],
|
|
11
12
|
"author": "jiamao<haofefe@163.com>",
|
|
12
13
|
"engines": {},
|
|
13
|
-
"dependencies": {
|
|
14
|
-
},
|
|
14
|
+
"dependencies": {},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@commitlint/cli": "^7.6.1",
|
|
17
17
|
"@commitlint/config-conventional": "^7.6.0",
|
package/src/core/jmControl.js
CHANGED
|
@@ -4,10 +4,12 @@ import {jmList} from "./jmList.js";
|
|
|
4
4
|
import {jmGradient} from "./jmGradient.js";
|
|
5
5
|
import {jmShadow} from "./jmShadow.js";
|
|
6
6
|
import {jmProperty} from "./jmProperty.js";
|
|
7
|
+
import WebglPath from "../lib/webgl/path.js";
|
|
7
8
|
|
|
8
9
|
//样式名称,也当做白名单使用
|
|
9
10
|
const jmStyleMap = {
|
|
10
11
|
'fill':'fillStyle',
|
|
12
|
+
'fillImage':'fillImage',
|
|
11
13
|
'stroke':'strokeStyle',
|
|
12
14
|
'shadow.blur':'shadowBlur',
|
|
13
15
|
'shadow.x':'shadowOffsetX',
|
|
@@ -40,8 +42,8 @@ export default class jmControl extends jmProperty {
|
|
|
40
42
|
|
|
41
43
|
constructor(params, t) {
|
|
42
44
|
params = params||{};
|
|
43
|
-
super();
|
|
44
|
-
this.
|
|
45
|
+
super(params);
|
|
46
|
+
this.property('type', t || new.target.name);
|
|
45
47
|
this.style = params && params.style ? params.style : {};
|
|
46
48
|
//this.position = params.position || {x:0,y:0};
|
|
47
49
|
this.width = params.width || 0;
|
|
@@ -55,6 +57,15 @@ export default class jmControl extends jmProperty {
|
|
|
55
57
|
this.zIndex = params.zIndex || 0;
|
|
56
58
|
this.interactive = typeof params.interactive == 'undefined'? true : params.interactive;
|
|
57
59
|
|
|
60
|
+
// webgl模式
|
|
61
|
+
if(this.mode === 'webgl') {
|
|
62
|
+
this.webglControl = new WebglPath(this.graph, {
|
|
63
|
+
style: this.style,
|
|
64
|
+
isRegular: params.isRegular,
|
|
65
|
+
needCut: params.needCut
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
58
69
|
this.initializing();
|
|
59
70
|
|
|
60
71
|
this.on = this.bind;
|
|
@@ -70,7 +81,7 @@ export default class jmControl extends jmProperty {
|
|
|
70
81
|
* @type string
|
|
71
82
|
*/
|
|
72
83
|
get type() {
|
|
73
|
-
return this.
|
|
84
|
+
return this.property('type');
|
|
74
85
|
}
|
|
75
86
|
|
|
76
87
|
/**
|
|
@@ -79,17 +90,17 @@ export default class jmControl extends jmProperty {
|
|
|
79
90
|
* @type {object}
|
|
80
91
|
*/
|
|
81
92
|
get context() {
|
|
82
|
-
let s = this.
|
|
93
|
+
let s = this.property('context');
|
|
83
94
|
if(s) return s;
|
|
84
95
|
else if(this.is('jmGraph') && this.canvas && this.canvas.getContext) {
|
|
85
|
-
return this.context = this.canvas.getContext('2d');
|
|
96
|
+
return this.context = this.canvas.getContext(this.mode || '2d');
|
|
86
97
|
}
|
|
87
|
-
|
|
98
|
+
const g = this.graph;
|
|
88
99
|
if(g) return g.context;
|
|
89
|
-
return g.canvas.getContext('2d');
|
|
100
|
+
return g.canvas.getContext(this.mode || '2d');
|
|
90
101
|
}
|
|
91
102
|
set context(v) {
|
|
92
|
-
return this.
|
|
103
|
+
return this.property('context', v);
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
/**
|
|
@@ -98,13 +109,13 @@ export default class jmControl extends jmProperty {
|
|
|
98
109
|
* @type {object}
|
|
99
110
|
*/
|
|
100
111
|
get style() {
|
|
101
|
-
let s = this.
|
|
102
|
-
if(!s) s = this.
|
|
112
|
+
let s = this.property('style');
|
|
113
|
+
if(!s) s = this.property('style', {});
|
|
103
114
|
return s;
|
|
104
115
|
}
|
|
105
116
|
set style(v) {
|
|
106
117
|
this.needUpdate = true;
|
|
107
|
-
return this.
|
|
118
|
+
return this.property('style', v);
|
|
108
119
|
}
|
|
109
120
|
|
|
110
121
|
/**
|
|
@@ -114,13 +125,13 @@ export default class jmControl extends jmProperty {
|
|
|
114
125
|
* @type {boolean}
|
|
115
126
|
*/
|
|
116
127
|
get visible() {
|
|
117
|
-
let s = this.
|
|
118
|
-
if(typeof s == 'undefined') s = this.
|
|
128
|
+
let s = this.property('visible');
|
|
129
|
+
if(typeof s == 'undefined') s = this.property('visible', true);
|
|
119
130
|
return s;
|
|
120
131
|
}
|
|
121
132
|
set visible(v) {
|
|
122
133
|
this.needUpdate = true;
|
|
123
|
-
return this.
|
|
134
|
+
return this.property('visible', v);
|
|
124
135
|
}
|
|
125
136
|
|
|
126
137
|
/**
|
|
@@ -131,11 +142,11 @@ export default class jmControl extends jmProperty {
|
|
|
131
142
|
* @type {boolean}
|
|
132
143
|
*/
|
|
133
144
|
get interactive() {
|
|
134
|
-
let s = this.
|
|
145
|
+
let s = this.property('interactive');
|
|
135
146
|
return s;
|
|
136
147
|
}
|
|
137
148
|
set interactive(v) {
|
|
138
|
-
return this.
|
|
149
|
+
return this.property('interactive', v);
|
|
139
150
|
}
|
|
140
151
|
|
|
141
152
|
/**
|
|
@@ -144,13 +155,13 @@ export default class jmControl extends jmProperty {
|
|
|
144
155
|
* @type {list}
|
|
145
156
|
*/
|
|
146
157
|
get children() {
|
|
147
|
-
let s = this.
|
|
148
|
-
if(!s) s = this.
|
|
158
|
+
let s = this.property('children');
|
|
159
|
+
if(!s) s = this.property('children', new jmList());
|
|
149
160
|
return s;
|
|
150
161
|
}
|
|
151
162
|
set children(v) {
|
|
152
163
|
this.needUpdate = true;
|
|
153
|
-
return this.
|
|
164
|
+
return this.property('children', v);
|
|
154
165
|
}
|
|
155
166
|
|
|
156
167
|
/**
|
|
@@ -159,13 +170,13 @@ export default class jmControl extends jmProperty {
|
|
|
159
170
|
* @type {number}
|
|
160
171
|
*/
|
|
161
172
|
get width() {
|
|
162
|
-
let s = this.
|
|
163
|
-
if(typeof s == 'undefined') s = this.
|
|
173
|
+
let s = this.property('width');
|
|
174
|
+
if(typeof s == 'undefined') s = this.property('width', 0);
|
|
164
175
|
return s;
|
|
165
176
|
}
|
|
166
177
|
set width(v) {
|
|
167
178
|
this.needUpdate = true;
|
|
168
|
-
return this.
|
|
179
|
+
return this.property('width', v);
|
|
169
180
|
}
|
|
170
181
|
|
|
171
182
|
/**
|
|
@@ -174,13 +185,13 @@ export default class jmControl extends jmProperty {
|
|
|
174
185
|
* @type {number}
|
|
175
186
|
*/
|
|
176
187
|
get height() {
|
|
177
|
-
let s = this.
|
|
178
|
-
if(typeof s == 'undefined') s = this.
|
|
188
|
+
let s = this.property('height');
|
|
189
|
+
if(typeof s == 'undefined') s = this.property('height', 0);
|
|
179
190
|
return s;
|
|
180
191
|
}
|
|
181
192
|
set height(v) {
|
|
182
193
|
this.needUpdate = true;
|
|
183
|
-
return this.
|
|
194
|
+
return this.property('height', v);
|
|
184
195
|
}
|
|
185
196
|
|
|
186
197
|
/**
|
|
@@ -189,13 +200,13 @@ export default class jmControl extends jmProperty {
|
|
|
189
200
|
* @type {number}
|
|
190
201
|
*/
|
|
191
202
|
get zIndex() {
|
|
192
|
-
let s = this.
|
|
193
|
-
if(!s) s = this.
|
|
203
|
+
let s = this.property('zIndex');
|
|
204
|
+
if(!s) s = this.property('zIndex', 0);
|
|
194
205
|
return s;
|
|
195
206
|
}
|
|
196
207
|
set zIndex(v) {
|
|
197
208
|
this.needUpdate = true;
|
|
198
|
-
this.
|
|
209
|
+
this.property('zIndex', v);
|
|
199
210
|
this.children.sort();//层级发生改变,需要重新排序
|
|
200
211
|
this.needUpdate = true;
|
|
201
212
|
return v;
|
|
@@ -376,12 +387,25 @@ export default class jmControl extends jmProperty {
|
|
|
376
387
|
__setStyle(styleValue.toGradient(this), mpname||name);
|
|
377
388
|
}
|
|
378
389
|
else if(mpname) {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
390
|
+
|
|
391
|
+
if(this.webglControl) {
|
|
392
|
+
//只有存在白名单中才处理
|
|
393
|
+
//颜色转换
|
|
394
|
+
if(t == 'string' && ['fillStyle', 'strokeStyle', 'shadowColor'].indexOf(mpname) > -1) {
|
|
395
|
+
styleValue = jmUtils.hexToRGBA(styleValue);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
this.webglControl.setStyle(mpname, styleValue);
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
//只有存在白名单中才处理
|
|
402
|
+
//颜色转换
|
|
403
|
+
if(t == 'string' && ['fillStyle', 'strokeStyle', 'shadowColor'].indexOf(mpname) > -1) {
|
|
404
|
+
styleValue = jmUtils.toColor(styleValue);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
this.context[mpname] = styleValue;
|
|
383
408
|
}
|
|
384
|
-
this.context[mpname] = styleValue;
|
|
385
409
|
}
|
|
386
410
|
else {
|
|
387
411
|
switch(name) {
|
|
@@ -398,7 +422,7 @@ export default class jmControl extends jmProperty {
|
|
|
398
422
|
}
|
|
399
423
|
//平移
|
|
400
424
|
case 'translate' : {
|
|
401
|
-
this.context.translate(styleValue.x, styleValue.y);
|
|
425
|
+
this.context.translate && this.context.translate(styleValue.x, styleValue.y);
|
|
402
426
|
break;
|
|
403
427
|
}
|
|
404
428
|
//旋转
|
|
@@ -417,9 +441,9 @@ export default class jmControl extends jmProperty {
|
|
|
417
441
|
tranY = styleValue.rotateY + bounds.top;
|
|
418
442
|
}
|
|
419
443
|
|
|
420
|
-
if(tranX!=0 || tranY != 0) this.context.translate(tranX,tranY);
|
|
444
|
+
if(tranX!=0 || tranY != 0) this.context.translate && this.context.translate(tranX,tranY);
|
|
421
445
|
this.context.rotate(styleValue.angle);
|
|
422
|
-
if(tranX!=0 || tranY != 0) this.context.translate(-tranX,-tranY);
|
|
446
|
+
if(tranX!=0 || tranY != 0) this.context.translate && this.context.translate(-tranX,-tranY);
|
|
423
447
|
break;
|
|
424
448
|
}
|
|
425
449
|
case 'transform' : {
|
|
@@ -778,7 +802,8 @@ export default class jmControl extends jmProperty {
|
|
|
778
802
|
*/
|
|
779
803
|
beginDraw() {
|
|
780
804
|
this.getLocation(true);//重置位置信息
|
|
781
|
-
this.context.beginPath();
|
|
805
|
+
this.context.beginPath && this.context.beginPath();
|
|
806
|
+
if(this.webglControl && this.webglControl.beginDraw) this.webglControl.beginDraw();
|
|
782
807
|
}
|
|
783
808
|
|
|
784
809
|
/**
|
|
@@ -789,16 +814,24 @@ export default class jmControl extends jmProperty {
|
|
|
789
814
|
endDraw() {
|
|
790
815
|
//如果当前为封闭路径
|
|
791
816
|
if(this.style.close) {
|
|
792
|
-
this.
|
|
817
|
+
if(this.webglControl) this.webglControl.closePath();
|
|
818
|
+
else this.context.closePath && this.context.closePath();
|
|
793
819
|
}
|
|
794
820
|
|
|
795
821
|
if(this.style['fill']) {
|
|
796
|
-
this.
|
|
822
|
+
if(this.webglControl) {
|
|
823
|
+
const bounds = this.getBounds();
|
|
824
|
+
this.webglControl.fill(bounds);
|
|
825
|
+
}
|
|
826
|
+
else this.context.fill && this.context.fill();
|
|
797
827
|
}
|
|
798
|
-
if(this.style['stroke'] || !this.style['fill']) {
|
|
799
|
-
this.
|
|
828
|
+
if(this.style['stroke'] || (!this.style['fill'] && !this.is('jmGraph'))) {
|
|
829
|
+
if(this.webglControl) this.webglControl.stroke();
|
|
830
|
+
else this.context.stroke && this.context.stroke();
|
|
800
831
|
}
|
|
801
832
|
|
|
833
|
+
if(this.webglControl && this.webglControl.endDraw) this.webglControl.endDraw();
|
|
834
|
+
|
|
802
835
|
this.needUpdate = false;
|
|
803
836
|
}
|
|
804
837
|
|
|
@@ -811,20 +844,27 @@ export default class jmControl extends jmProperty {
|
|
|
811
844
|
draw() {
|
|
812
845
|
if(this.points && this.points.length > 0) {
|
|
813
846
|
//获取当前控件的绝对位置
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
847
|
+
const bounds = this.parent && this.parent.absoluteBounds?this.parent.absoluteBounds:this.absoluteBounds;
|
|
848
|
+
if(this.webglControl) {
|
|
849
|
+
this.webglControl.setParentBounds(bounds);
|
|
850
|
+
this.webglControl.draw([
|
|
851
|
+
...this.points
|
|
852
|
+
]);
|
|
853
|
+
}
|
|
854
|
+
else if(this.context && this.context.moveTo) {
|
|
855
|
+
this.context.moveTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
|
|
856
|
+
let len = this.points.length;
|
|
857
|
+
for(let i=1; i < len;i++) {
|
|
858
|
+
let p = this.points[i];
|
|
859
|
+
//移至当前坐标
|
|
860
|
+
if(p.m) {
|
|
861
|
+
this.context.moveTo(p.x + bounds.left,p.y + bounds.top);
|
|
862
|
+
}
|
|
863
|
+
else {
|
|
864
|
+
this.context.lineTo(p.x+ bounds.left,p.y + bounds.top);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
}
|
|
828
868
|
}
|
|
829
869
|
}
|
|
830
870
|
|
|
@@ -848,7 +888,7 @@ export default class jmControl extends jmProperty {
|
|
|
848
888
|
else if(this.absoluteBounds.bottom <= 0) needDraw = false;
|
|
849
889
|
}
|
|
850
890
|
|
|
851
|
-
this.context.save();
|
|
891
|
+
this.context.save && this.context.save();
|
|
852
892
|
|
|
853
893
|
this.emit('beginDraw', this);
|
|
854
894
|
|
|
@@ -865,7 +905,7 @@ export default class jmControl extends jmProperty {
|
|
|
865
905
|
}
|
|
866
906
|
|
|
867
907
|
this.emit('endDraw',this);
|
|
868
|
-
this.context.restore();
|
|
908
|
+
this.context.restore && this.context.restore();
|
|
869
909
|
|
|
870
910
|
this.needUpdate = false;
|
|
871
911
|
}
|
package/src/core/jmGradient.js
CHANGED
|
@@ -84,8 +84,13 @@ export default class jmGradient {
|
|
|
84
84
|
let sy1 = Number(y1) + bounds.top;
|
|
85
85
|
let sx2 = Number(x2) + bounds.left;
|
|
86
86
|
let sy2 = Number(y2) + bounds.top;
|
|
87
|
-
if(this.type === 'linear') {
|
|
88
|
-
|
|
87
|
+
if(this.type === 'linear') {
|
|
88
|
+
if(control.mode === 'webgl' && control.webglControl) {
|
|
89
|
+
gradient = control.webglControl.createLinearGradient(x1, y1, x2, y2, bounds);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
context.createLinearGradient && (gradient = context.createLinearGradient(sx1, sy1, sx2, sy2));
|
|
93
|
+
}
|
|
89
94
|
}
|
|
90
95
|
else if(this.type === 'radial') {
|
|
91
96
|
let r1 = this.r1||0;
|
|
@@ -97,22 +102,32 @@ export default class jmGradient {
|
|
|
97
102
|
if(jmUtils.checkPercent(r2)) {
|
|
98
103
|
r2 = jmUtils.percentToNumber(r2);
|
|
99
104
|
r2 = d * r2;
|
|
105
|
+
}
|
|
106
|
+
if(control.mode === 'webgl' && control.webglControl) {
|
|
107
|
+
gradient = control.webglControl.createRadialGradient(x1, y1, r1, x2, y2, r2, bounds);
|
|
100
108
|
}
|
|
101
109
|
//offsetLine = Math.abs(r2 - r1);//二圆半径差
|
|
110
|
+
else if(context.createRadialGradient) {
|
|
111
|
+
gradient = context.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2);
|
|
112
|
+
}
|
|
102
113
|
//小程序的接口特殊
|
|
103
|
-
if(context.createCircularGradient) {
|
|
114
|
+
else if(context.createCircularGradient) {
|
|
104
115
|
gradient = context.createCircularGradient(sx1, sy1, r2);
|
|
105
116
|
}
|
|
106
|
-
else {
|
|
107
|
-
gradient = context.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2);
|
|
108
|
-
}
|
|
109
117
|
}
|
|
118
|
+
|
|
110
119
|
//颜色渐变
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
120
|
+
if(gradient) {
|
|
121
|
+
this.stops.each(function(i,s) {
|
|
122
|
+
let c = jmUtils.toColor(s.color);
|
|
123
|
+
//s.offset 0.0 ~ 1.0
|
|
124
|
+
gradient && gradient.addColorStop(s.offset, c);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const s = this.stops.get(0);
|
|
129
|
+
return (s && s.color) || '#000';
|
|
130
|
+
}
|
|
116
131
|
|
|
117
132
|
return gradient;
|
|
118
133
|
}
|
|
@@ -191,14 +206,14 @@ export default class jmGradient {
|
|
|
191
206
|
toString() {
|
|
192
207
|
let str = this.type + '-gradient(';
|
|
193
208
|
if(this.type == 'linear') {
|
|
194
|
-
str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
|
|
209
|
+
str += this.x1.toFixed(2) + ' ' + this.y1.toFixed(2) + ' ' + this.x2.toFixed(2) + ' ' + this.y2.toFixed(2);
|
|
195
210
|
}
|
|
196
211
|
else {
|
|
197
|
-
str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
|
|
212
|
+
str += this.x1.toFixed(2) + ' ' + this.y1.toFixed(2) + ' ' + this.r1.toFixed(2) + ' ' + this.x2.toFixed(2) + ' ' + this.y2.toFixed(2) + ' ' + this.r2.toFixed(2);
|
|
198
213
|
}
|
|
199
214
|
//颜色渐变
|
|
200
215
|
this.stops.each(function(i,s) {
|
|
201
|
-
str += ',' + s.color + ' ' + s.offset;
|
|
216
|
+
str += ',' + s.color + ' ' + s.offset.toFixed(2);
|
|
202
217
|
});
|
|
203
218
|
return str + ')';
|
|
204
219
|
}
|
package/src/core/jmGraph.js
CHANGED
|
@@ -29,7 +29,8 @@ export default class jmGraph extends jmControl {
|
|
|
29
29
|
option = option || {};
|
|
30
30
|
option.mode = option.mode || '2d'; // webgl | 2d
|
|
31
31
|
option.interactive = true;
|
|
32
|
-
|
|
32
|
+
option.isRegular = true;// 规则的
|
|
33
|
+
|
|
33
34
|
super(option, 'jmGraph');
|
|
34
35
|
|
|
35
36
|
this.option = option || {};
|
|
@@ -41,7 +42,9 @@ export default class jmGraph extends jmControl {
|
|
|
41
42
|
* @property utils/util
|
|
42
43
|
* @type {jmUtils}
|
|
43
44
|
*/
|
|
44
|
-
this.util = this.utils = jmUtils;
|
|
45
|
+
this.util = this.utils = jmUtils;
|
|
46
|
+
// 模式 webgl | 2d
|
|
47
|
+
this.mode = option.mode;
|
|
45
48
|
|
|
46
49
|
//如果是小程序
|
|
47
50
|
if(typeof wx != 'undefined' && wx.canIUse && wx.canIUse('canvas')) {
|
|
@@ -70,7 +73,25 @@ export default class jmGraph extends jmControl {
|
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
75
|
this.canvas = canvas;
|
|
73
|
-
this.context = canvas.getContext(
|
|
76
|
+
this.context = canvas.getContext(this.mode);
|
|
77
|
+
|
|
78
|
+
this.textureCanvas = option.textureCanvas || null;
|
|
79
|
+
|
|
80
|
+
// webgl模式
|
|
81
|
+
if(this.mode === 'webgl') {
|
|
82
|
+
|
|
83
|
+
this.context.enable(this.context.BLEND);// 开启混合功能:(注意,它不可和gl.DEPTH_TEST一起使用)
|
|
84
|
+
this.context.blendFunc(this.context.SRC_ALPHA, this.context.ONE_MINUS_SRC_ALPHA); // 指定混合函数:
|
|
85
|
+
// webglcontextlost webglcontextrestored
|
|
86
|
+
jmUtils.bindEvent(canvas, 'webglcontextlost', (e)=> {
|
|
87
|
+
console.log('canvas webglcontextlost', e);
|
|
88
|
+
this.emit('webglcontextlost', e);
|
|
89
|
+
});
|
|
90
|
+
jmUtils.bindEvent(canvas, 'webglcontextrestored', (e)=> {
|
|
91
|
+
console.log('canvas webglcontextrestored', e);
|
|
92
|
+
this.emit('webglcontextrestored', e);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
74
95
|
this.__init(callback);
|
|
75
96
|
}
|
|
76
97
|
|
|
@@ -93,13 +114,13 @@ export default class jmGraph extends jmControl {
|
|
|
93
114
|
* 为了解决一像素线条问题
|
|
94
115
|
*/
|
|
95
116
|
this.on('beginDraw', function() {
|
|
96
|
-
this.context.translate(0.5, 0.5);
|
|
117
|
+
this.context.translate && this.context.translate(0.5, 0.5);
|
|
97
118
|
});
|
|
98
119
|
/**
|
|
99
120
|
* 结束控件绘制 为了解决一像素线条问题
|
|
100
121
|
*/
|
|
101
122
|
this.on('endDraw', function() {
|
|
102
|
-
this.context.translate(-0.5, -0.5);
|
|
123
|
+
this.context.translate && this.context.translate(-0.5, -0.5);
|
|
103
124
|
});
|
|
104
125
|
|
|
105
126
|
// devicePixelRatio初始化
|
|
@@ -110,7 +131,7 @@ export default class jmGraph extends jmControl {
|
|
|
110
131
|
this.devicePixelRatio = dpr;
|
|
111
132
|
// 为了解决锯齿问题,先放大canvas再缩放
|
|
112
133
|
this.dprScaleSize = this.devicePixelRatio > 1? this.devicePixelRatio : 2;
|
|
113
|
-
|
|
134
|
+
|
|
114
135
|
if(this.option.width > 0) this.width = this.option.width;
|
|
115
136
|
if(this.option.height > 0) this.height = this.option.height;
|
|
116
137
|
this.resize();
|
|
@@ -138,9 +159,17 @@ export default class jmGraph extends jmControl {
|
|
|
138
159
|
|
|
139
160
|
this.css('width', w + "px");
|
|
140
161
|
this.css('height', h + "px");
|
|
141
|
-
this.
|
|
142
|
-
|
|
143
|
-
|
|
162
|
+
if(this.mode === '2d') {
|
|
163
|
+
this.canvas.height = h * this.dprScaleSize;
|
|
164
|
+
this.canvas.width = w * this.dprScaleSize;
|
|
165
|
+
if(this.dprScaleSize !== 1) this.context.scale && this.context.scale(this.dprScaleSize, this.dprScaleSize);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
this.canvas.width = w;
|
|
169
|
+
this.canvas.height = h;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.context.viewport && this.context.viewport(0, 0, w, h);
|
|
144
173
|
}
|
|
145
174
|
|
|
146
175
|
/**
|
|
@@ -210,7 +239,7 @@ export default class jmGraph extends jmControl {
|
|
|
210
239
|
* @return {postion} 返回定位坐标
|
|
211
240
|
*/
|
|
212
241
|
getPosition() {
|
|
213
|
-
|
|
242
|
+
const p = jmUtils.getElementPosition(this.canvas.canvas || this.canvas);
|
|
214
243
|
p.width = this.width;
|
|
215
244
|
p.height = this.height;
|
|
216
245
|
p.right = p.left + p.width;
|
|
@@ -261,7 +290,7 @@ export default class jmGraph extends jmControl {
|
|
|
261
290
|
* @return {jmShadow} 阴影对象
|
|
262
291
|
*/
|
|
263
292
|
createShadow(x, y, blur, color) {
|
|
264
|
-
|
|
293
|
+
const sh = new jmShadow(x, y, blur, color);
|
|
265
294
|
return sh;
|
|
266
295
|
}
|
|
267
296
|
|
|
@@ -276,7 +305,7 @@ export default class jmGraph extends jmControl {
|
|
|
276
305
|
* @return {jmGradient} 线性渐变对象
|
|
277
306
|
*/
|
|
278
307
|
createLinearGradient(x1, y1, x2, y2) {
|
|
279
|
-
|
|
308
|
+
const gradient = new jmGradient({
|
|
280
309
|
type:'linear',
|
|
281
310
|
x1: x1,
|
|
282
311
|
y1: y1,
|
|
@@ -299,7 +328,7 @@ export default class jmGraph extends jmControl {
|
|
|
299
328
|
* @return {jmGradient} 放射渐变对象
|
|
300
329
|
*/
|
|
301
330
|
createRadialGradient(x1, y1, r1, x2, y2, r2) {
|
|
302
|
-
|
|
331
|
+
const gradient = new jmGradient({
|
|
303
332
|
type:'radial',
|
|
304
333
|
x1: x1,
|
|
305
334
|
y1: y1,
|
|
@@ -354,16 +383,30 @@ export default class jmGraph extends jmControl {
|
|
|
354
383
|
h = h / this.scaleSize.y;
|
|
355
384
|
}*/
|
|
356
385
|
}
|
|
357
|
-
|
|
358
|
-
if(this.
|
|
359
|
-
this.
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
386
|
+
|
|
387
|
+
if(this.context.clearRect) {
|
|
388
|
+
if(this.style && this.style.fill) {
|
|
389
|
+
this.points = [
|
|
390
|
+
{x:0, y:0},
|
|
391
|
+
{x:w, y:0},
|
|
392
|
+
{x:w, y:h},
|
|
393
|
+
{x:0, y:h}
|
|
394
|
+
];
|
|
395
|
+
this.style.close = true;// 封闭填充
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
this.context.clearRect(0, 0, w, h);
|
|
399
|
+
}
|
|
400
|
+
else if(this.mode === 'webgl' && this.context.clear) {
|
|
401
|
+
const color = this.style && this.style.fill? this.utils.hexToRGBA(this.style.fill): {
|
|
402
|
+
r: 0,
|
|
403
|
+
g: 0,
|
|
404
|
+
b: 0,
|
|
405
|
+
a: 0
|
|
406
|
+
};
|
|
407
|
+
this.context.clearColor(color.r, color.g, color.b, color.a); // 设置清空颜色缓冲时的颜色值
|
|
408
|
+
this.context.clear(this.context.COLOR_BUFFER_BIT); // 清空颜色缓冲区,也就是清空画布
|
|
365
409
|
}
|
|
366
|
-
else if(this.context.clearRect) this.context.clearRect(0,0,w,h);
|
|
367
410
|
}
|
|
368
411
|
|
|
369
412
|
/**
|
|
@@ -388,10 +431,11 @@ export default class jmGraph extends jmControl {
|
|
|
388
431
|
* @param {style} style 当前路径的样式
|
|
389
432
|
* @return {jmPath} 路径对象jmPath
|
|
390
433
|
*/
|
|
391
|
-
createPath(points, style) {
|
|
392
|
-
|
|
434
|
+
createPath(points, style, option={}) {
|
|
435
|
+
const path = this.createShape('path',{
|
|
393
436
|
points: points,
|
|
394
|
-
style: style
|
|
437
|
+
style: style,
|
|
438
|
+
...option
|
|
395
439
|
});
|
|
396
440
|
return path;
|
|
397
441
|
}
|
|
@@ -406,7 +450,7 @@ export default class jmGraph extends jmControl {
|
|
|
406
450
|
* @return {jmLine} 直线对象
|
|
407
451
|
*/
|
|
408
452
|
createLine(start, end, style) {
|
|
409
|
-
|
|
453
|
+
const line = this.createShape('line', {
|
|
410
454
|
start: start,
|
|
411
455
|
end: end,
|
|
412
456
|
style: style
|
package/src/core/jmObject.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import {jmList} from "./jmList.js";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
var control_id_counter = 0;
|
|
6
5
|
/**
|
|
7
6
|
* 所有jm对象的基础对象
|
|
8
7
|
*
|
|
@@ -15,7 +14,7 @@ export default class jmObject {
|
|
|
15
14
|
if(g && g.type == 'jmGraph') {
|
|
16
15
|
this.graph = g;
|
|
17
16
|
}
|
|
18
|
-
|
|
17
|
+
this.id = ++control_id_counter; //生成一个唯一id
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
/**
|