jmgraph 3.2.19 → 3.2.21

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 (63) hide show
  1. package/README.md +311 -6
  2. package/dist/jmgraph.core.min.js +1 -1
  3. package/dist/jmgraph.core.min.js.map +1 -1
  4. package/dist/jmgraph.js +2022 -368
  5. package/dist/jmgraph.min.js +1 -1
  6. package/index.js +23 -25
  7. package/package.json +1 -1
  8. package/src/core/jmControl.js +199 -30
  9. package/src/core/jmFilter.js +150 -0
  10. package/src/core/jmGraph.js +207 -7
  11. package/src/core/jmLayer.js +142 -0
  12. package/src/core/jmPath.js +55 -0
  13. package/src/core/jmUtils.js +46 -37
  14. package/src/lib/webgl/base.js +10 -36
  15. package/src/lib/webgl/gradient.js +16 -3
  16. package/src/lib/webgl/index.js +5 -4
  17. package/src/lib/webgl/path.js +156 -33
  18. package/src/shapes/jmEllipse.js +91 -0
  19. package/src/shapes/jmLabel.js +126 -75
  20. package/src/shapes/jmPolygon.js +129 -0
  21. package/src/shapes/jmRect.js +107 -29
  22. package/src/shapes/jmStar.js +160 -0
  23. package/example/ball.html +0 -217
  24. package/example/base.html +0 -112
  25. package/example/canvas.html +0 -54
  26. package/example/cell.html +0 -284
  27. package/example/controls/arc.html +0 -129
  28. package/example/controls/arrowline.html +0 -78
  29. package/example/controls/bezier.html +0 -299
  30. package/example/controls/img.html +0 -97
  31. package/example/controls/label.html +0 -87
  32. package/example/controls/line.html +0 -173
  33. package/example/controls/prismatic.html +0 -63
  34. package/example/controls/rect.html +0 -64
  35. package/example/controls/resize.html +0 -112
  36. package/example/controls/test.html +0 -360
  37. package/example/es.html +0 -70
  38. package/example/es5module.html +0 -63
  39. package/example/heartarc.html +0 -116
  40. package/example/index.html +0 -47
  41. package/example/js/require.js +0 -5
  42. package/example/love/img/bling/bling.tps +0 -265
  43. package/example/love/img/bling.json +0 -87
  44. package/example/love/img/bling.tps +0 -295
  45. package/example/love/img/doc/bling.gif +0 -0
  46. package/example/love/img/love.json +0 -95
  47. package/example/love/img/love.tps +0 -315
  48. package/example/love/img/qq/qq.tps +0 -399
  49. package/example/love/img/qq.json +0 -242
  50. package/example/love/index.html +0 -40
  51. package/example/love/js/game.js +0 -558
  52. package/example/music.html +0 -211
  53. package/example/node/test.js +0 -138
  54. package/example/pdf.html +0 -187
  55. package/example/progress.html +0 -173
  56. package/example/pso.html +0 -148
  57. package/example/sort.html +0 -805
  58. package/example/tweenjs.html +0 -84
  59. package/example/webgl.html +0 -278
  60. package/example/xfj/img/dr_die.gif +0 -0
  61. package/example/xfj/index.html +0 -332
  62. package/example/xfj/shake.js +0 -49
  63. package/example/xfj/testori.html +0 -76
@@ -0,0 +1,129 @@
1
+ import {jmPath} from "../core/jmPath.js";
2
+
3
+ /**
4
+ * 画多边形
5
+ * 支持规则多边形(正多边形)和自定义多边形
6
+ * 规则多边形通过边数和半径自动计算顶点,自定义多边形通过顶点数组定义
7
+ *
8
+ * @class jmPolygon
9
+ * @extends jmPath
10
+ * @param {object} params 多边形的参数
11
+ * @param {array} [params.points] 自定义顶点数组,如果提供则忽略sides和radius
12
+ * @param {number} [params.sides=3] 多边形边数(3-100)
13
+ * @param {number} [params.radius=50] 多边形半径(像素)
14
+ * @param {object} [params.center={x:0,y:0}] 多边形中心点坐标
15
+ */
16
+ export default class jmPolygon extends jmPath {
17
+
18
+ constructor(params, t='jmPolygon') {
19
+ params = params || {};
20
+ params.isRegular = true; // 标记为规则图形,便于优化渲染
21
+ super(params, t);
22
+
23
+ // 参数验证和初始化
24
+ this.sides = params.sides || params.points?.length || 3;
25
+ this.radius = params.radius || 50;
26
+ this.center = params.center || {x: 0, y: 0};
27
+ }
28
+
29
+ /**
30
+ * 设定或获取多边形边数
31
+ * 边数决定了多边形的形状,最小为3(三角形)
32
+ *
33
+ * @property sides
34
+ * @for jmPolygon
35
+ * @type {number}
36
+ */
37
+ get sides() {
38
+ return this.property('sides');
39
+ }
40
+ set sides(v) {
41
+ // 参数验证:边数必须在3-100之间
42
+ if(typeof v !== 'number' || isNaN(v) || v < 3) {
43
+ console.warn('jmPolygon: sides must be a number >= 3');
44
+ v = 3;
45
+ }
46
+ if(v > 100) {
47
+ console.warn('jmPolygon: sides should not exceed 100 for performance reasons');
48
+ v = 100;
49
+ }
50
+ this.needUpdate = true;
51
+ return this.property('sides', Math.floor(v)); // 确保是整数
52
+ }
53
+
54
+ /**
55
+ * 设定或获取多边形半径
56
+ * 半径是从中心点到顶点的距离
57
+ *
58
+ * @property radius
59
+ * @for jmPolygon
60
+ * @type {number}
61
+ */
62
+ get radius() {
63
+ return this.property('radius');
64
+ }
65
+ set radius(v) {
66
+ // 参数验证:半径必须为正数
67
+ if(typeof v !== 'number' || isNaN(v) || v <= 0) {
68
+ console.warn('jmPolygon: radius must be a positive number');
69
+ v = 1;
70
+ }
71
+ this.needUpdate = true;
72
+ return this.property('radius', v);
73
+ }
74
+
75
+ /**
76
+ * 设定或获取多边形中心
77
+ * 中心点是多边形的几何中心
78
+ *
79
+ * @property center
80
+ * @for jmPolygon
81
+ * @type {object}
82
+ */
83
+ get center() {
84
+ return this.property('center');
85
+ }
86
+ set center(v) {
87
+ // 参数验证:中心点必须包含x和y属性
88
+ if(!v || typeof v.x !== 'number' || typeof v.y !== 'number') {
89
+ console.warn('jmPolygon: center must be an object with x and y properties');
90
+ v = {x: 0, y: 0};
91
+ }
92
+ this.needUpdate = true;
93
+ return this.property('center', v);
94
+ }
95
+
96
+ /**
97
+ * 初始化图形点
98
+ * 如果提供了自定义顶点,则使用自定义顶点
99
+ * 否则根据边数和半径自动计算规则多边形的顶点
100
+ *
101
+ * @method initPoints
102
+ * @private
103
+ * @for jmPolygon
104
+ */
105
+ initPoints() {
106
+ // 如果提供了自定义顶点,直接使用
107
+ if (this.points && this.points.length > 0) {
108
+ return;
109
+ }
110
+
111
+ // 计算规则多边形的顶点
112
+ const points = [];
113
+ const sides = this.sides;
114
+ const radius = this.radius;
115
+ const center = this.center;
116
+
117
+ // 从顶部开始绘制(-90度),顺时针方向
118
+ for (let i = 0; i < sides; i++) {
119
+ const angle = (i / sides) * Math.PI * 2 - Math.PI / 2;
120
+ const x = center.x + Math.cos(angle) * radius;
121
+ const y = center.y + Math.sin(angle) * radius;
122
+ points.push({x, y});
123
+ }
124
+
125
+ this.points = points;
126
+ }
127
+ }
128
+
129
+ export { jmPolygon };
@@ -8,6 +8,7 @@ import {jmLine} from './jmLine.js';
8
8
  * @class jmRect
9
9
  * @extends jmPath
10
10
  * @param {object} params 参数 position=矩形左上角顶点坐标,width=宽,height=高,radius=边角弧度
11
+ * radius支持数字(四角相同)或对象 { topLeft, topRight, bottomRight, bottomLeft }
11
12
  */
12
13
  export default class jmRect extends jmPath {
13
14
 
@@ -17,12 +18,24 @@ export default class jmRect extends jmPath {
17
18
  super(params, t);
18
19
 
19
20
  this.style.close = true;
20
- this.radius = params.radius || this.style.radius || 0;
21
+ const r = params.radius || this.style.radius || this.style.borderRadius || 0;
22
+ if(typeof r === 'object' && r !== null) {
23
+ // 四角独立圆角
24
+ this.radius = {
25
+ topLeft: Number(r.topLeft) || 0,
26
+ topRight: Number(r.topRight) || 0,
27
+ bottomRight: Number(r.bottomRight) || 0,
28
+ bottomLeft: Number(r.bottomLeft) || 0
29
+ };
30
+ }
31
+ else {
32
+ this.radius = r;
33
+ }
21
34
  }
22
35
  /**
23
- * 圆角半径
36
+ * 圆角半径,支持数字或四角独立对象
24
37
  * @property radius
25
- * @type {number}
38
+ * @type {number|object}
26
39
  */
27
40
  get radius() {
28
41
  return this.property('radius');
@@ -30,6 +43,36 @@ export default class jmRect extends jmPath {
30
43
  set radius(v) {
31
44
  this.needUpdate = true;
32
45
  return this.property('radius', v);
46
+ }
47
+
48
+ /**
49
+ * 获取规范化的圆角值(四角独立)
50
+ * @returns {object} { topLeft, topRight, bottomRight, bottomLeft }
51
+ */
52
+ getNormalizedRadius() {
53
+ const r = this.radius;
54
+ if(typeof r === 'number') {
55
+ const v = Math.max(0, r);
56
+ return { topLeft: v, topRight: v, bottomRight: v, bottomLeft: v };
57
+ }
58
+ if(typeof r === 'object' && r !== null) {
59
+ return {
60
+ topLeft: Math.max(0, Number(r.topLeft) || 0),
61
+ topRight: Math.max(0, Number(r.topRight) || 0),
62
+ bottomRight: Math.max(0, Number(r.bottomRight) || 0),
63
+ bottomLeft: Math.max(0, Number(r.bottomLeft) || 0)
64
+ };
65
+ }
66
+ return { topLeft: 0, topRight: 0, bottomRight: 0, bottomLeft: 0 };
67
+ }
68
+
69
+ /**
70
+ * 检查是否有圆角
71
+ * @returns {boolean}
72
+ */
73
+ hasRadius() {
74
+ const nr = this.getNormalizedRadius();
75
+ return nr.topLeft > 0 || nr.topRight > 0 || nr.bottomRight > 0 || nr.bottomLeft > 0;
33
76
  }
34
77
 
35
78
  /**
@@ -92,7 +135,7 @@ export default class jmRect extends jmPath {
92
135
 
93
136
  /**
94
137
  * 初始化图形点
95
- * 如果有边角弧度则类型圆绝计算其描点
138
+ * 支持四角独立圆角,借助圆弧对象计算描点
96
139
  *
97
140
  * @method initPoints
98
141
  * @private
@@ -109,32 +152,67 @@ export default class jmRect extends jmPath {
109
152
  this.dottedLine = this.graph.createShape(jmLine, {style: this.style});
110
153
  }
111
154
 
112
- //如果有边界弧度则借助圆弧对象计算描点
113
- if(location.radius && location.radius < location.width/2 && location.radius < location.height/2) {
155
+ const nr = this.getNormalizedRadius();
156
+ const hasRadius = this.hasRadius();
157
+
158
+ // 如果有圆角(支持四角独立),借助圆弧对象计算描点
159
+ if(hasRadius) {
114
160
  let q = Math.PI / 2;
115
- let arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
116
- arc.center = {x:location.left + location.radius,y:location.top+location.radius};
117
- arc.startAngle = Math.PI;
118
- arc.endAngle = Math.PI + q;
119
- let ps1 = arc.initPoints();
120
-
121
- arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
122
- arc.center = {x:p2.x - location.radius,y:p2.y + location.radius};
123
- arc.startAngle = Math.PI + q;
124
- arc.endAngle = Math.PI * 2;
125
- let ps2 = arc.initPoints();
126
-
127
- arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
128
- arc.center = {x:p3.x - location.radius,y:p3.y - location.radius};
129
- arc.startAngle = 0;
130
- arc.endAngle = q;
131
- let ps3 = arc.initPoints();
132
-
133
- arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
134
- arc.center = {x:p4.x + location.radius,y:p4.y - location.radius};
135
- arc.startAngle = q;
136
- arc.endAngle = Math.PI;
137
- let ps4 = arc.initPoints();
161
+
162
+ // 限制圆角不超过短边的一半
163
+ const maxR = Math.min(location.width / 2, location.height / 2);
164
+ const rtl = Math.min(nr.topLeft, maxR);
165
+ const rtr = Math.min(nr.topRight, maxR);
166
+ const rbr = Math.min(nr.bottomRight, maxR);
167
+ const rbl = Math.min(nr.bottomLeft, maxR);
168
+
169
+ // 左上角圆弧
170
+ if(rtl > 0) {
171
+ let arc = this.graph.createShape(jmArc,{radius:rtl,anticlockwise:false});
172
+ arc.center = {x:location.left + rtl, y:location.top + rtl};
173
+ arc.startAngle = Math.PI;
174
+ arc.endAngle = Math.PI + q;
175
+ var ps1 = arc.initPoints();
176
+ }
177
+ else {
178
+ var ps1 = [p1];
179
+ }
180
+
181
+ // 右上角圆弧
182
+ if(rtr > 0) {
183
+ let arc = this.graph.createShape(jmArc,{radius:rtr,anticlockwise:false});
184
+ arc.center = {x:p2.x - rtr, y:p2.y + rtr};
185
+ arc.startAngle = Math.PI + q;
186
+ arc.endAngle = Math.PI * 2;
187
+ var ps2 = arc.initPoints();
188
+ }
189
+ else {
190
+ var ps2 = [p2];
191
+ }
192
+
193
+ // 右下角圆弧
194
+ if(rbr > 0) {
195
+ let arc = this.graph.createShape(jmArc,{radius:rbr,anticlockwise:false});
196
+ arc.center = {x:p3.x - rbr, y:p3.y - rbr};
197
+ arc.startAngle = 0;
198
+ arc.endAngle = q;
199
+ var ps3 = arc.initPoints();
200
+ }
201
+ else {
202
+ var ps3 = [p3];
203
+ }
204
+
205
+ // 左下角圆弧
206
+ if(rbl > 0) {
207
+ let arc = this.graph.createShape(jmArc,{radius:rbl,anticlockwise:false});
208
+ arc.center = {x:p4.x + rbl, y:p4.y - rbl};
209
+ arc.startAngle = q;
210
+ arc.endAngle = Math.PI;
211
+ var ps4 = arc.initPoints();
212
+ }
213
+ else {
214
+ var ps4 = [p4];
215
+ }
138
216
  this.points = ps1.concat(ps2,ps3,ps4);
139
217
  }
140
218
  else {
@@ -0,0 +1,160 @@
1
+ import {jmPath} from "../core/jmPath.js";
2
+
3
+ /**
4
+ * 画星形
5
+ * 支持自定义顶点数和内外半径,创建各种星形图案
6
+ * 星形由交替的外半径和内半径顶点组成
7
+ *
8
+ * @class jmStar
9
+ * @extends jmPath
10
+ * @param {object} params 星形的参数
11
+ * @param {array} [params.points] 自定义顶点数组,如果提供则忽略其他参数
12
+ * @param {number} [params.points=5] 星形顶点数(角数,3-50)
13
+ * @param {number} [params.radius=50] 星形外半径(从中心到尖角的距离)
14
+ * @param {number} [params.innerRadius=25] 星形内半径(从中心到凹陷处的距离)
15
+ * @param {object} [params.center={x:0,y:0}] 星形中心点坐标
16
+ */
17
+ export default class jmStar extends jmPath {
18
+
19
+ constructor(params, t='jmStar') {
20
+ params = params || {};
21
+ params.isRegular = true; // 标记为规则图形
22
+ super(params, t);
23
+
24
+ // 参数验证和初始化
25
+ this.pointsCount = params.points || 5;
26
+ this.radius = params.radius || 50;
27
+ this.innerRadius = params.innerRadius || 25;
28
+ this.center = params.center || {x: 0, y: 0};
29
+ }
30
+
31
+ /**
32
+ * 设定或获取星形顶点数(角数)
33
+ * 顶点数决定了星形的角数,例如5表示五角星
34
+ *
35
+ * @property pointsCount
36
+ * @for jmStar
37
+ * @type {number}
38
+ */
39
+ get pointsCount() {
40
+ return this.property('pointsCount');
41
+ }
42
+ set pointsCount(v) {
43
+ // 参数验证:顶点数必须在3-50之间
44
+ if(typeof v !== 'number' || isNaN(v) || v < 3) {
45
+ console.warn('jmStar: pointsCount must be a number >= 3');
46
+ v = 3;
47
+ }
48
+ if(v > 50) {
49
+ console.warn('jmStar: pointsCount should not exceed 50 for performance reasons');
50
+ v = 50;
51
+ }
52
+ this.needUpdate = true;
53
+ return this.property('pointsCount', Math.floor(v)); // 确保是整数
54
+ }
55
+
56
+ /**
57
+ * 设定或获取星形外半径
58
+ * 外半径是从中心到尖角的距离
59
+ *
60
+ * @property radius
61
+ * @for jmStar
62
+ * @type {number}
63
+ */
64
+ get radius() {
65
+ return this.property('radius');
66
+ }
67
+ set radius(v) {
68
+ // 参数验证:半径必须为正数
69
+ if(typeof v !== 'number' || isNaN(v) || v <= 0) {
70
+ console.warn('jmStar: radius must be a positive number');
71
+ v = 1;
72
+ }
73
+ this.needUpdate = true;
74
+ return this.property('radius', v);
75
+ }
76
+
77
+ /**
78
+ * 设定或获取星形内半径
79
+ * 内半径是从中心到凹陷处的距离
80
+ * 内半径应该小于外半径,否则会产生奇怪的形状
81
+ *
82
+ * @property innerRadius
83
+ * @for jmStar
84
+ * @type {number}
85
+ */
86
+ get innerRadius() {
87
+ return this.property('innerRadius');
88
+ }
89
+ set innerRadius(v) {
90
+ // 参数验证:内半径必须为正数
91
+ if(typeof v !== 'number' || isNaN(v) || v <= 0) {
92
+ console.warn('jmStar: innerRadius must be a positive number');
93
+ v = 1;
94
+ }
95
+ // 警告:内半径不应大于外半径
96
+ if(v >= this.radius) {
97
+ console.warn('jmStar: innerRadius should be less than radius for proper star shape');
98
+ }
99
+ this.needUpdate = true;
100
+ return this.property('innerRadius', v);
101
+ }
102
+
103
+ /**
104
+ * 设定或获取星形中心
105
+ * 中心点是星形的几何中心
106
+ *
107
+ * @property center
108
+ * @for jmStar
109
+ * @type {object}
110
+ */
111
+ get center() {
112
+ return this.property('center');
113
+ }
114
+ set center(v) {
115
+ // 参数验证:中心点必须包含x和y属性
116
+ if(!v || typeof v.x !== 'number' || typeof v.y !== 'number') {
117
+ console.warn('jmStar: center must be an object with x and y properties');
118
+ v = {x: 0, y: 0};
119
+ }
120
+ this.needUpdate = true;
121
+ return this.property('center', v);
122
+ }
123
+
124
+ /**
125
+ * 初始化图形点
126
+ * 计算星形的顶点坐标,交替使用外半径和内半径
127
+ *
128
+ * @method initPoints
129
+ * @private
130
+ * @for jmStar
131
+ */
132
+ initPoints() {
133
+ // 如果提供了自定义顶点,直接使用
134
+ if (this.points && this.points.length > 0) {
135
+ return;
136
+ }
137
+
138
+ // 计算星形顶点
139
+ const points = [];
140
+ const pointsCount = this.pointsCount;
141
+ const radius = this.radius;
142
+ const innerRadius = this.innerRadius;
143
+ const center = this.center;
144
+
145
+ // 星形有2倍顶点数的点(外半径和内半径交替)
146
+ // 从顶部开始绘制(-90度),顺时针方向
147
+ for (let i = 0; i < pointsCount * 2; i++) {
148
+ const angle = (i / pointsCount) * Math.PI - Math.PI / 2;
149
+ // 偶数索引使用外半径,奇数索引使用内半径
150
+ const r = i % 2 === 0 ? radius : innerRadius;
151
+ const x = center.x + Math.cos(angle) * r;
152
+ const y = center.y + Math.sin(angle) * r;
153
+ points.push({x, y});
154
+ }
155
+
156
+ this.points = points;
157
+ }
158
+ }
159
+
160
+ export { jmStar };
package/example/ball.html DELETED
@@ -1,217 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
5
- <meta name="viewport" content="width=device-width,initial-scale=1">
6
- <!--<script type="text/javascript" src="../index.js"></script>
7
- <script type="text/javascript" src="../dist/jmGraph.min.js"></script> -->
8
- <style>
9
- html,body {
10
- margin:0;
11
- padding:0;
12
- width:100%;
13
- height:100%;
14
- overflow: hidden;
15
- }
16
- #mycanvas {
17
- background-color:#000;
18
- position: absolute;
19
- width:100%;
20
- height:100%;
21
- }
22
- </style>
23
-
24
- </head>
25
- <body>
26
- <div id="mycanvas">
27
- </div>
28
-
29
- </body>
30
- </html>
31
-
32
- <script type="module">
33
- import jmGraph from "../index.js";
34
- var graph = new jmGraph('mycanvas', {
35
- mode: 'webgl',
36
- autoRefresh: true
37
- });
38
- //初始化jmgraph
39
- /*jmGraph('mycanvas').then((g)=>{
40
- graph = g;
41
- jmUtils.bindEvent(window,'resize',resize);
42
- refreshBall(g);
43
-
44
-
45
- }); */
46
-
47
- refreshBall(graph);
48
-
49
-
50
-
51
- function refreshBall(g) {
52
- resize();
53
-
54
- var style = {
55
- lineWidth: 1.2,
56
- close:true,
57
- stroke:'#fff',
58
- fill:'#05a2e2'
59
- };
60
-
61
-
62
- var balls = [];
63
- function createPosition(radius,i) {
64
- var x = Math.random() * graph.width + radius;
65
- var y = Math.random() * graph.height + radius;
66
-
67
- for(var j=i+1;j<balls.length;j++) {
68
- var b2= balls[j];
69
- var lx = Math.abs(x - b2.x());
70
- var ly = Math.abs(y - b2.y());
71
- var l = Math.sqrt(lx * lx + ly * ly);
72
- //如果二个球重叠则放 弃当前球
73
- if(l < radius + b2.radius) {
74
- return createPosition(radius);
75
- }
76
- }
77
- return {x:x,y:y};
78
- }
79
-
80
- for(var i=0;i<100;i++) {
81
- var radius = Math.random() * 10 + 8;
82
- var styletmp = graph.util.clone(style);
83
- styletmp.fill = graph.createRadialGradient('50%','50%',0,'50%','50%',radius);
84
- var rr1 = Math.floor(Math.random() * 255);
85
- var gg1 = Math.floor(Math.random() * 255);
86
- var bb1 = Math.floor(Math.random() * 255);
87
- var rr2 = Math.floor(Math.random() * 255);
88
- var gg2 = Math.floor(Math.random() * 255);
89
- var bb2 = Math.floor(Math.random() * 255);
90
- styletmp.fill.addStop(0,graph.util.toColor(rr1,gg1,bb1));
91
- styletmp.fill.addStop(1,graph.util.toColor(rr2,gg2,bb2));
92
- //styletmp.fill = 'r(0.5,0.1)#ccc-#ccc';
93
- var p = createPosition(radius);
94
- var b = new ball(graph,p.x,p.y,radius,styletmp);
95
- b.vx = 0;//Math.random() * 20 - 10;
96
- b.vy = 0;//Math.random() * 20 - 10;
97
- //b.ax = 0;
98
- //b.ay = 0.4;
99
- balls.push(b);
100
- }
101
-
102
- var radius = Math.random() * 10 + 6;
103
- var styletmp = graph.util.clone(style);
104
- styletmp.fill = graph.createRadialGradient(radius,radius,0,radius,radius,radius);
105
- styletmp.fill.addStop(0,'#fff');
106
- styletmp.fill.addStop(1, graph.util.toColor(255,149,255));
107
- var p = createPosition(radius,0);
108
- var b = new ball(graph,p.x,p.y,radius,styletmp);
109
- b.vx = 106;
110
- b.vy = 104;
111
- balls.push(b);
112
-
113
- function animate() {
114
- var bs = balls;
115
- var len = bs.length;
116
- //var mvx = 0;
117
- //var mvy = 0;
118
- for(var i=0;i<len;i++) {
119
- var b1 = bs[i];
120
- b1.vx += b1.ax;
121
- b1.vy += b1.ay;
122
- for(var j=i+1;j<len;j++) {
123
- var b2= bs[j];
124
- var lx = b1.x() - b2.x();
125
- var ly = b1.y() - b2.y();
126
- var l = Math.sqrt(lx * lx + ly * ly);
127
- if(l <= b1.radius + b2.radius) {
128
- var vx = b1.vx;
129
- var vy = b1.vy;
130
- var vxb = b2.vx;
131
- var vyb = b2.vy;
132
-
133
- var angle = Math.atan2(ly,lx);
134
- var sine = Math.sin(angle);
135
- var cosine = Math.cos(angle);
136
-
137
- var x = 0;
138
- var y = 0;
139
-
140
- var xb = lx * cosine + ly * sine;
141
- var yb = ly * cosine - lx * sine;
142
-
143
- var vx = b1.vx * cosine + b1.vy * sine;
144
- var vy = b1.vy * cosine - b1.vx * sine;
145
- var vxb = b2.vx * cosine + b2.vy * sine;
146
- var vyb = b2.vy * cosine - b2.vx * sine;
147
-
148
- var vtotal = vx - vxb;
149
-
150
- vx = ((b1.radius - b2.radius) * vx + 2 * b2.radius * vxb) / (b1.radius + b2.radius);
151
- vxb = vtotal + vx;
152
-
153
- var vx1 = vx * cosine - vy * sine;
154
- var vy1 = vy * cosine + vx * sine;
155
-
156
- var vx2 = vxb * cosine - vyb * sine;
157
- var vy2 = vyb * cosine + vxb * sine;
158
-
159
- b1.vx = vx1;
160
- b1.vy = vy1;
161
- b2.vx = vx2;
162
- b2.vy = vy2;
163
- }
164
- }
165
- var x = b1.x() + b1.vx;
166
- var maxX = b1.graph.width - b1.radius ;
167
- if(x <= b1.radius || x >= maxX) {
168
- //b1.vy -= 0.4;
169
- b1.vx *= -1;
170
- }
171
- x = Math.max(x,b1.radius);
172
- x = Math.min(maxX,x);
173
- b1.x(x);
174
- var y = b1.y() + b1.vy;
175
- var maxY = b1.graph.height - b1.radius;
176
- if(y <= b1.radius || y >= maxY) {
177
- //if(y >= maxY && b1.vy > 0) {b1.vy -= 1;b1.vx -= 0.4;}
178
- b1.vy *= -1;
179
- }
180
- y = Math.max(y,b1.radius);
181
- y = Math.min(maxY,y);
182
- b1.y(y);
183
- }
184
- graph.needUpdate = true;
185
- };
186
- g.on('update', animate);
187
- }
188
- function resize() {
189
- if(graph) {
190
- graph.width = window.innerWidth - 10;
191
- graph.height = window.innerHeight - 10;
192
- }
193
- }
194
-
195
- function ball(graph,x,y,radius,style) {
196
- this.graph = graph;
197
- this.radius = radius;
198
- this.center = {x:x,y:y};
199
- this.shape = graph.createShape('arc',{style:style,center:this.center,radius:this.radius,anticlockwise:true});
200
- graph.children.add(this.shape);
201
- this.shape.canMove(true);
202
- this.vx = 0;
203
- this.vy = 0;
204
- this.ax = 0;
205
- this.ay = 0;
206
-
207
- this.x = function(x) {
208
- if(typeof x !== 'undefined') this.center.x = x;
209
- return this.center.x;
210
- }
211
- this.y = function(y) {
212
- if(typeof y !== 'undefined') this.center.y = y;
213
- return this.center.y;
214
- }
215
- }
216
-
217
- </script>