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.
@@ -0,0 +1,196 @@
1
+ const WebglGradientTextureCache = {};
2
+ // 渐变
3
+ class WeblGradient {
4
+ // type:[linear= 线性渐变,radial=放射性渐变]
5
+ constructor(type='linear', params={}) {
6
+ this.type = type || 'linear';
7
+
8
+ this.x1 = params.x1 || 0;
9
+ this.y1 = params.y1 || 0;
10
+ this.r1 = params.r1 || 0;
11
+ this.x2 = params.x2 || 0;
12
+ this.y2 = params.y2 || 0;
13
+ this.r2 = params.r2 || 0;
14
+
15
+ this.bounds = params.bounds || {
16
+ left: 0,
17
+ top: 0,
18
+ width: 0,
19
+ height: 0
20
+ }
21
+
22
+ this.control = params.control;
23
+
24
+ this.stops = [];
25
+ this.init();
26
+ }
27
+
28
+ init() {
29
+ const dx = this.x2 - this.x1;
30
+ const dy = this.y2 - this.y1;
31
+
32
+ if(this.type === 'radial') {
33
+ this.length = this.r2 - this.r1;
34
+ }
35
+ else if(dx === 0 && dy === 0) {
36
+ this.length = 0;
37
+ }
38
+ else {
39
+ // 渐变中心的距离
40
+ this.length = Math.sqrt(Math.pow(dx, 2), Math.pow(dy, 2));
41
+ this.sin = dy / this.length;
42
+ this.cos = dx / this.length;
43
+ }
44
+ }
45
+
46
+ // 渐变颜色
47
+ addColorStop(offset, color) {
48
+ this.stops.push({
49
+ offset,
50
+ color
51
+ });
52
+ }
53
+
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) {
67
+ return null;
68
+ }
69
+
70
+ const ctx = canvas.getContext('2d', {
71
+ willReadFrequently: true
72
+ });
73
+
74
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
75
+
76
+ let gradient = null;
77
+ if(this.type === 'linear') {
78
+ gradient = ctx.createLinearGradient(this.x1, this.y1, this.x2, this.y2);
79
+ }
80
+ else {
81
+ gradient = ctx.createRadialGradient(this.x1, this.y1, this.r1, this.x2, this.y2, this.r2);
82
+ }
83
+ this.stops.forEach(function(s, i) {
84
+ const c = control.graph.utils.toColor(s.color);
85
+ gradient && gradient.addColorStop(s.offset, c);
86
+ });
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);
101
+
102
+ WebglGradientTextureCache[key] = data;
103
+
104
+ return data;
105
+ }
106
+
107
+ // 根据绘制图形的坐标计算出对应点的颜色
108
+ /*
109
+ toPointColors(points) {
110
+ const stops = this.getStops();
111
+ const colors = [];
112
+ for(let i=0; i<points.length; i+=2) {
113
+ const p = {
114
+ x: points[i],
115
+ y: points[i+1]
116
+ }
117
+ if(this.type === 'radial') {
118
+ const dx = p.x - this.x1;
119
+ const dy = p.y - this.y1;
120
+ const len = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
121
+ const rang = this.getStopRange(len, stops);
122
+ if(!rang.start && rang.end) {
123
+ colors.push(rang.end.color);
124
+ }
125
+ else if(!rang.end && rang.start) {
126
+ colors.push(rang.start.color);
127
+ }
128
+ else {
129
+ const rangLength = rang.end.length - rang.start.length;
130
+ const offlen = len - rang.start.length;
131
+ const per = offlen / rangLength;
132
+ const color = {
133
+ r: rang.start.color.r + (rang.end.color.r - rang.start.color.r) * per,
134
+ g: rang.start.color.g + (rang.end.color.g - rang.start.color.g) * per,
135
+ b: rang.start.color.b + (rang.end.color.b - rang.start.color.b) * per,
136
+ a: rang.start.color.a + (rang.end.color.a - rang.start.color.a) * per,
137
+ };
138
+ colors.push(color);
139
+ }
140
+ }
141
+ }
142
+ return colors;
143
+ }
144
+ */
145
+ // 根据起点距离获取边界stop
146
+ /*
147
+ getStopRange(len, stops) {
148
+ const res = {};
149
+ for(const s of stops) {
150
+ if(s.length <= len) {
151
+ res.start = s;
152
+ }
153
+ else {
154
+ res.end = s;
155
+ }
156
+ }
157
+ return res;
158
+ }
159
+
160
+ // 根据stop计算offset长度
161
+ getStops() {
162
+ const stops = this.stops.sort((p1, p2) => p1.offset - p2.offset); // 渐变色排序从小于大
163
+ for(const s of stops) {
164
+
165
+ const color = typeof s.color === 'string'? this.control.graph.utils.hexToRGBA(s.color) : s.color;
166
+ console.log(s, color);
167
+ s.color = this.control.graph.utils.rgbToDecimal(color);
168
+ s.length = s.offset * this.length;
169
+ }
170
+ return stops;
171
+ }
172
+ */
173
+ /**
174
+ * 转换为渐变的字符串表达
175
+ *
176
+ * @method toString
177
+ * @for jmGradient
178
+ * @return {string} linear-gradient(x1 y1 x2 y2, color1 step, color2 step, ...); //radial-gradient(x1 y1 r1 x2 y2 r2, color1 step,color2 step, ...);
179
+ */
180
+ toString() {
181
+ let str = this.type + '-gradient(';
182
+ if(this.type == 'linear') {
183
+ str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
184
+ }
185
+ else {
186
+ str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
187
+ }
188
+ //颜色渐变
189
+ this.stops.forEach(function(s) {
190
+ str += ',' + s.color + ' ' + s.offset;
191
+ });
192
+ return str + ')';
193
+ }
194
+ }
195
+
196
+ export default WeblGradient;
@@ -0,0 +1,11 @@
1
+
2
+
3
+ /**
4
+ * 采用webgl基础绘图
5
+ */
6
+ class webgl {
7
+ constructor(context, option) {
8
+ this.option = option || {};
9
+
10
+ }
11
+ }