jmgraph 3.2.15 → 3.2.17
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/LICENSE +21 -21
- package/README.md +252 -428
- package/build/gulpfile.js +142 -142
- package/build/package-lock.json +10666 -0
- package/build/package.json +71 -71
- package/dev.js +9 -9
- package/dist/jmgraph.core.min.js +1 -1
- package/dist/jmgraph.core.min.js.map +1 -1
- package/dist/jmgraph.js +3379 -2664
- package/dist/jmgraph.min.js +1 -1
- package/example/ball.html +216 -216
- package/example/base.html +111 -111
- package/example/canvas.html +53 -53
- package/example/cell.html +283 -283
- package/example/controls/arc.html +128 -128
- package/example/controls/arrowline.html +77 -77
- package/example/controls/bezier.html +298 -298
- package/example/controls/img.html +96 -96
- package/example/controls/label.html +86 -86
- package/example/controls/line.html +172 -172
- package/example/controls/prismatic.html +62 -62
- package/example/controls/rect.html +63 -63
- package/example/controls/resize.html +111 -111
- package/example/controls/test.html +359 -359
- package/example/es.html +69 -69
- package/example/es5module.html +62 -63
- package/example/heartarc.html +115 -115
- package/example/index.html +46 -46
- package/example/js/require.js +4 -4
- package/example/love/img/bling/bling.tps +265 -265
- package/example/love/img/bling.json +87 -87
- package/example/love/img/bling.tps +295 -295
- package/example/love/img/love.json +95 -95
- package/example/love/img/love.tps +315 -315
- package/example/love/img/qq/qq.tps +399 -399
- package/example/love/img/qq.json +242 -242
- package/example/love/index.html +40 -40
- package/example/love/js/game.js +558 -558
- package/example/music.html +210 -210
- package/example/node/test.js +137 -137
- package/example/pdf.html +186 -186
- package/example/progress.html +172 -172
- package/example/pso.html +147 -147
- package/example/sort.html +804 -815
- package/example/tweenjs.html +83 -83
- package/example/webgl.html +278 -278
- package/example/xfj/index.html +331 -331
- package/example/xfj/shake.js +48 -48
- package/example/xfj/testori.html +75 -75
- package/index.js +99 -99
- package/package.json +55 -56
- package/src/core/jmControl.js +1376 -1531
- package/src/core/jmEvents.js +240 -281
- package/src/core/jmGradient.js +231 -231
- package/src/core/jmGraph.js +569 -569
- package/src/core/jmList.js +92 -157
- package/src/core/jmObject.js +83 -103
- package/src/core/jmPath.js +35 -35
- package/src/core/jmProperty.js +71 -110
- package/src/core/jmShadow.js +65 -65
- package/src/core/jmUtils.js +906 -919
- package/src/lib/earcut.js +680 -680
- package/src/lib/earcut.md +73 -73
- package/src/lib/webgl/base.js +452 -452
- package/src/lib/webgl/core/buffer.js +48 -48
- package/src/lib/webgl/core/mapSize.js +40 -40
- package/src/lib/webgl/core/mapType.js +43 -43
- package/src/lib/webgl/core/program.js +138 -138
- package/src/lib/webgl/core/shader.js +13 -13
- package/src/lib/webgl/core/texture.js +60 -60
- package/src/lib/webgl/gradient.js +168 -168
- package/src/lib/webgl/index.js +10 -10
- package/src/lib/webgl/path.js +561 -561
- package/src/shapes/jmArrowLine.js +36 -36
- package/src/shapes/jmImage.js +244 -244
- package/src/shapes/jmLabel.js +271 -271
- package/src/shapes/jmResize.js +332 -330
package/README.md
CHANGED
|
@@ -1,59 +1,57 @@
|
|
|
1
|
-
jmGraph
|
|
2
|
-
=========
|
|
1
|
+
# jmGraph
|
|
3
2
|
|
|
4
|
-
[](https://www.npmjs.com/package/jmgraph)
|
|
4
|
+
[](https://www.npmjs.com/package/jmgraph)
|
|
5
|
+
[](https://github.com/fefeding/jmgraph/blob/master/LICENSE)
|
|
6
|
+
[](https://travis-ci.org/fefeding/jmgraph)
|
|
6
7
|
|
|
7
|
-
基于
|
|
8
|
-
`让你用类似于dom的方式,在canvas上画图,感觉会不会很爽。`
|
|
8
|
+
基于 Canvas 的简单画图组件,让你用类似于 DOM 的方式,在 Canvas 上画图。
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
- 示例:[https://jiamao.github.io/jmgraph/example/index.html](https://jiamao.github.io/jmgraph/example/index.html)
|
|
10
|
+
## ✨ 特性
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
- 🎨 **简单易用** - 类似 DOM 的 API 设计,学习成本低
|
|
13
|
+
- 🚀 **高性能** - 基于 Canvas 原生渲染,支持大量图形
|
|
14
|
+
- 📱 **跨平台** - 支持浏览器、Node.js 和微信小程序
|
|
15
|
+
- 🎯 **丰富图形** - 内置矩形、圆形、线条、箭头、贝塞尔曲线等常用图形
|
|
16
|
+
- 🎭 **事件系统** - 完整的鼠标和触摸事件支持
|
|
17
|
+
- 🔧 **可扩展** - 支持自定义图形控件
|
|
18
|
+
- 🌈 **样式丰富** - 支持渐变、阴影、透明度等样式
|
|
14
19
|
|
|
15
|
-
安装
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
```
|
|
20
|
+
## 📦 安装
|
|
21
|
+
|
|
22
|
+
### npm
|
|
23
|
+
|
|
24
|
+
```bash
|
|
20
25
|
npm install jmgraph
|
|
21
26
|
```
|
|
22
|
-
|
|
27
|
+
|
|
28
|
+
### yarn
|
|
29
|
+
|
|
30
|
+
```bash
|
|
23
31
|
yarn add jmgraph
|
|
24
32
|
```
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
--------
|
|
34
|
+
### CDN
|
|
28
35
|
|
|
29
|
-
|
|
36
|
+
直接下载 `dist/jmgraph.min.js` 并在 HTML 中引用:
|
|
30
37
|
|
|
31
|
-
下载`dist/jmgraph.min.js`代码,并引用到你的html中。
|
|
32
|
-
```html
|
|
33
|
-
<script type="text/javascript" src="../dist/jmgraph.min.js"></script>
|
|
34
|
-
```
|
|
35
|
-
也可以用`commonjs`、`requirejs`等模块化库。
|
|
36
|
-
##### requirejs
|
|
37
38
|
```html
|
|
38
|
-
<script type="text/javascript" src="
|
|
39
|
-
<script>
|
|
40
|
-
require(['../dist/jmgraph.js'], function(m) {
|
|
41
|
-
var g = new m.jmGraph();
|
|
42
|
-
});
|
|
43
|
-
</script>
|
|
39
|
+
<script type="text/javascript" src="../dist/jmgraph.min.js"></script>
|
|
44
40
|
```
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
|
|
42
|
+
## 🚀 快速开始
|
|
43
|
+
|
|
44
|
+
### ES6 模块方式
|
|
45
|
+
|
|
47
46
|
```html
|
|
48
47
|
<script type="module">
|
|
49
|
-
// import jmGraph from "../dist/jmgraph.es6.js";
|
|
50
|
-
// import jmGraph from "./node_modules/jmgraph/index.js";
|
|
51
48
|
import jmGraph from "jmgraph";
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
|
|
50
|
+
const container = document.getElementById('mycanvas_container');
|
|
51
|
+
const g = new jmGraph(container, {
|
|
54
52
|
width: 800,
|
|
55
53
|
height: 600,
|
|
56
|
-
autoRefresh: true,
|
|
54
|
+
autoRefresh: true,
|
|
57
55
|
style: {
|
|
58
56
|
fill: '#000'
|
|
59
57
|
}
|
|
@@ -61,457 +59,283 @@ yarn add jmgraph
|
|
|
61
59
|
</script>
|
|
62
60
|
```
|
|
63
61
|
|
|
62
|
+
### CommonJS 方式
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
height: 600,
|
|
76
|
-
//样式,规则请参照样式说明
|
|
77
|
-
style: {
|
|
78
|
-
fill: '#000' //指定背景色
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
</script>
|
|
64
|
+
```javascript
|
|
65
|
+
const jmGraph = require('jmgraph');
|
|
66
|
+
|
|
67
|
+
const g = jmGraph.create('mycanvas_container', {
|
|
68
|
+
width: 800,
|
|
69
|
+
height: 600,
|
|
70
|
+
style: {
|
|
71
|
+
fill: '#000'
|
|
72
|
+
}
|
|
73
|
+
});
|
|
82
74
|
```
|
|
83
|
-
|
|
75
|
+
|
|
76
|
+
### 绘制一个矩形
|
|
77
|
+
|
|
84
78
|
```javascript
|
|
79
|
+
const style = {
|
|
80
|
+
stroke: '#46BF86',
|
|
81
|
+
lineWidth: 2,
|
|
82
|
+
shadow: '0,0,10,#fff'
|
|
83
|
+
};
|
|
85
84
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
//创建一个方块
|
|
96
|
-
var rect = g.createShape('rect',{
|
|
97
|
-
style:style,
|
|
98
|
-
position: {x:100,y:100}, //左上角坐标
|
|
99
|
-
width:100,
|
|
100
|
-
height:100
|
|
101
|
-
});
|
|
102
|
-
g.children.add(rect);
|
|
103
|
-
|
|
104
|
-
//绘制,可以用requestAnimationFrame动态刷新
|
|
105
|
-
function update() {
|
|
106
|
-
g.redraw();
|
|
107
|
-
//requestAnimationFrame(update);
|
|
108
|
-
}
|
|
109
|
-
update();
|
|
110
|
-
}
|
|
85
|
+
const rect = g.createShape('rect', {
|
|
86
|
+
style: style,
|
|
87
|
+
position: {x: 100, y: 100},
|
|
88
|
+
width: 100,
|
|
89
|
+
height: 100
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
g.children.add(rect);
|
|
93
|
+
g.redraw();
|
|
111
94
|
```
|
|
112
95
|
|
|
113
|
-
|
|
114
|
-
---
|
|
96
|
+
## 📚 文档
|
|
115
97
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
|
125
|
-
|
|
|
126
|
-
|
|
|
127
|
-
|
|
|
128
|
-
|
|
|
129
|
-
|
|
|
130
|
-
|
|
|
131
|
-
|
|
|
132
|
-
|
|
|
133
|
-
|
|
|
134
|
-
|
|
|
135
|
-
|
|
|
136
|
-
|
|
|
137
|
-
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
98
|
+
- [在线示例](https://fefeding.github.io/jmgraph/)
|
|
99
|
+
- [API 文档](https://fefeding.github.io/jmgraph/example/index.html)
|
|
100
|
+
- [基于 jmGraph 的图表库](https://github.com/fefeding/jmchart)
|
|
101
|
+
|
|
102
|
+
## 🎨 样式说明
|
|
103
|
+
|
|
104
|
+
jmGraph 支持简化的样式名称和原生 Canvas 样式:
|
|
105
|
+
|
|
106
|
+
| 简化名称 | 原生名称 | 说明 |
|
|
107
|
+
| :- | :- | :- |
|
|
108
|
+
| fill | fillStyle | 填充颜色、渐变或模式 |
|
|
109
|
+
| stroke | strokeStyle | 描边颜色、渐变或模式 |
|
|
110
|
+
| shadow | - | 阴影,格式:'0,0,10,#fff' |
|
|
111
|
+
| shadow.blur | shadowBlur | 阴影模糊级别 |
|
|
112
|
+
| shadow.x | shadowOffsetX | 阴影水平偏移 |
|
|
113
|
+
| shadow.y | shadowOffsetY | 阴影垂直偏移 |
|
|
114
|
+
| shadow.color | shadowColor | 阴影颜色 |
|
|
115
|
+
| lineWidth | lineWidth | 线条宽度 |
|
|
116
|
+
| miterLimit | miterLimit | 最大斜接长度 |
|
|
117
|
+
| font | font | 字体 |
|
|
118
|
+
| fontSize | font | 字体大小 |
|
|
119
|
+
| fontFamily | font | 字体名称 |
|
|
120
|
+
| opacity | globalAlpha | 透明度 |
|
|
121
|
+
| textAlign | textAlign | 文本水平对齐 |
|
|
122
|
+
| textBaseline | textBaseline | 文本垂直对齐 |
|
|
123
|
+
| lineJoin | lineJoin | 线条连接样式 |
|
|
124
|
+
| lineCap | lineCap | 线条端点样式 |
|
|
125
|
+
|
|
126
|
+
## 🎯 内置图形
|
|
127
|
+
|
|
128
|
+
### 矩形 (Rect)
|
|
142
129
|
|
|
143
|
-
事件的绑定函数:`bind/unbind`
|
|
144
|
-
示例:
|
|
145
130
|
```javascript
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
this.cursor('pointer');
|
|
152
|
-
this.neadUpdate = true; //需要刷新
|
|
131
|
+
const rect = g.createShape('rect', {
|
|
132
|
+
style: style,
|
|
133
|
+
position: {x: 100, y: 100},
|
|
134
|
+
width: 100,
|
|
135
|
+
height: 100
|
|
153
136
|
});
|
|
154
137
|
```
|
|
155
138
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
#### 事件一览
|
|
159
|
-
| 名称 | 说明 | 回调参数
|
|
160
|
-
| :- | :- | :- |
|
|
161
|
-
| mousedown | 鼠标按下时触发 | -
|
|
162
|
-
| mousemove | 鼠标在对象上移动时触发 |{target:当前元素,position: 当前位置}
|
|
163
|
-
| mouseover | 鼠标从某元素移开 | {target:当前元素}
|
|
164
|
-
| mouseleave | 某个鼠标按键被松开 | -
|
|
165
|
-
| mouseup | 某个鼠标按键被松开 | -
|
|
166
|
-
| dblclick | 鼠标双击某个对象 | -
|
|
167
|
-
| click | 鼠标点击某个对象 | -
|
|
168
|
-
| touchstart | 触控开始 | position: 当前位置
|
|
169
|
-
| touchmove | 触控移动手指 | position: 当前位置
|
|
170
|
-
| touchend | 触控结束 | position: 当前位置
|
|
171
|
-
|
|
172
|
-
控件
|
|
173
|
-
---
|
|
174
|
-
|
|
175
|
-
#### Path
|
|
176
|
-
`path`是多数图形的基类,可以指定一个points数组来绘制一个路径。
|
|
177
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/line.html)
|
|
139
|
+
### 圆形/椭圆 (Arc)
|
|
178
140
|
|
|
179
141
|
```javascript
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
142
|
+
const arc = g.createShape('arc', {
|
|
143
|
+
style: style,
|
|
144
|
+
center: {x: 100, y: 150},
|
|
145
|
+
width: 120,
|
|
146
|
+
height: 80
|
|
147
|
+
});
|
|
186
148
|
```
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
具体请参考示例。
|
|
190
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/arc.html)
|
|
149
|
+
|
|
150
|
+
### 线条 (Line)
|
|
191
151
|
|
|
192
152
|
```javascript
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
height: 80
|
|
199
|
-
});
|
|
153
|
+
const line = g.createLine(
|
|
154
|
+
{x: 10, y: 200},
|
|
155
|
+
{x: 80, y: 120},
|
|
156
|
+
style
|
|
157
|
+
);
|
|
200
158
|
```
|
|
201
159
|
|
|
202
|
-
|
|
203
|
-
`arrow`为创建一个箭头,
|
|
204
|
-
`Arrowline`是一条带箭头的直线。
|
|
205
|
-
具体请参考示例。
|
|
206
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/Arrowline.html)
|
|
160
|
+
### 箭头 (Arrow)
|
|
207
161
|
|
|
208
162
|
```javascript
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
});
|
|
215
|
-
//一起结束点和一个角度angle可以决定一个箭头,如果不填angle,则会用start和end来计算角度
|
|
216
|
-
var arrow = g.createShape('arrow', {
|
|
217
|
-
style:style,
|
|
218
|
-
start: {x:150, y:120},
|
|
219
|
-
end: {x: 160, y: 150}
|
|
220
|
-
//angle: Math.PI/2, //箭头角度 可以不填
|
|
221
|
-
//offsetX: 5, //箭头X偏移量
|
|
222
|
-
//offsetY: 8 //箭头Y偏移量
|
|
223
|
-
});
|
|
163
|
+
const arrow = g.createShape('arrow', {
|
|
164
|
+
style: style,
|
|
165
|
+
start: {x: 150, y: 120},
|
|
166
|
+
end: {x: 160, y: 150}
|
|
167
|
+
});
|
|
224
168
|
```
|
|
225
169
|
|
|
226
|
-
|
|
227
|
-
`bezier`可以指定无数个控制点,绘制复杂的曲线。
|
|
228
|
-
具体请参考示例。
|
|
229
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/bezier.html)
|
|
170
|
+
### 贝塞尔曲线 (Bezier)
|
|
230
171
|
|
|
231
172
|
```javascript
|
|
232
|
-
|
|
233
|
-
|
|
173
|
+
const bezier = g.createShape('bezier', {
|
|
174
|
+
style: style,
|
|
175
|
+
points: [p0, p1, p2, p3, p4]
|
|
176
|
+
});
|
|
234
177
|
```
|
|
235
178
|
|
|
236
|
-
|
|
237
|
-
`img`是用来承载一张图片的控件,可以用style.src来指定图片url。
|
|
238
|
-
具体请参考示例。
|
|
239
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/img.html)
|
|
179
|
+
### 图片 (Image)
|
|
240
180
|
|
|
241
181
|
```javascript
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
//style.opacity = 0.2;
|
|
247
|
-
|
|
248
|
-
//创建一个image
|
|
249
|
-
var img = g.createShape('image',{
|
|
250
|
-
style:style,
|
|
251
|
-
position: {x:100,y:100}
|
|
252
|
-
});
|
|
253
|
-
//设置图片可以用鼠标移动
|
|
182
|
+
const img = g.createShape('image', {
|
|
183
|
+
style: {src: 'image.png'},
|
|
184
|
+
position: {x: 100, y: 100}
|
|
185
|
+
});
|
|
254
186
|
img.canMove(true);
|
|
255
187
|
```
|
|
256
188
|
|
|
257
|
-
|
|
258
|
-
`label`可以用来绘制文字,通过style指定样式。
|
|
259
|
-
具体请参考示例。
|
|
260
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/label.html)
|
|
189
|
+
### 文字 (Label)
|
|
261
190
|
|
|
262
191
|
```javascript
|
|
263
|
-
|
|
192
|
+
const label = g.createShape('label', {
|
|
193
|
+
style: {
|
|
264
194
|
stroke: '#effaaa',
|
|
265
195
|
fill: '#fff',
|
|
266
|
-
textAlign: 'center',
|
|
267
|
-
textBaseline: 'middle',
|
|
196
|
+
textAlign: 'center',
|
|
197
|
+
textBaseline: 'middle',
|
|
268
198
|
fontSize: 24,
|
|
269
|
-
fontFamily: 'Arial'
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
style: {
|
|
277
|
-
stroke: 'red' //颜色
|
|
278
|
-
}
|
|
279
|
-
}, //边框
|
|
280
|
-
shadow: '0,0,10,#fff'
|
|
281
|
-
};
|
|
282
|
-
//style.opacity = 0.2;
|
|
283
|
-
|
|
284
|
-
//创建一个label
|
|
285
|
-
var label = g.createShape('label',{
|
|
286
|
-
style:style,
|
|
287
|
-
position:{x:200,y:150},
|
|
288
|
-
text:'test label',
|
|
289
|
-
width:120,
|
|
290
|
-
height:80
|
|
291
|
-
});
|
|
199
|
+
fontFamily: 'Arial'
|
|
200
|
+
},
|
|
201
|
+
position: {x: 200, y: 150},
|
|
202
|
+
text: 'Hello World',
|
|
203
|
+
width: 120,
|
|
204
|
+
height: 80
|
|
205
|
+
});
|
|
292
206
|
```
|
|
293
207
|
|
|
294
|
-
|
|
295
|
-
`prismatic`
|
|
296
|
-
具体请参考示例。
|
|
297
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/prismatic.html)
|
|
298
|
-
|
|
299
|
-
```javascript
|
|
300
|
-
var prismatic = g.createShape('prismatic',{
|
|
301
|
-
style:style,
|
|
302
|
-
center:{x:200,y:150},
|
|
303
|
-
width:120,
|
|
304
|
-
height:80
|
|
305
|
-
});
|
|
306
|
-
```
|
|
208
|
+
## 🎮 事件系统
|
|
307
209
|
|
|
308
|
-
|
|
309
|
-
`resize` 可以自由放大缩小的控件。
|
|
310
|
-
具体请参考示例。
|
|
311
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/resize.html)
|
|
210
|
+
### 事件绑定
|
|
312
211
|
|
|
313
212
|
```javascript
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
fill: 'transparent',//小方块填充色
|
|
322
|
-
lineWidth: 1, //小方块线宽
|
|
323
|
-
close: true
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
//style.opacity = 0.2;
|
|
327
|
-
|
|
328
|
-
//创建一个resize
|
|
329
|
-
var resize = g.createShape('resize', {
|
|
330
|
-
style: style,
|
|
331
|
-
position: {x:200, y:150},
|
|
332
|
-
width: 120,
|
|
333
|
-
height: 80
|
|
334
|
-
});
|
|
335
|
-
//大小改变事件
|
|
336
|
-
resize.on('resize', function() {
|
|
337
|
-
console.log(arguments);
|
|
338
|
-
});
|
|
213
|
+
const shape = g.createShape('rect', {...});
|
|
214
|
+
|
|
215
|
+
shape.bind('mouseover', function(evt) {
|
|
216
|
+
this.style.stroke = 'rgba(39,72,188,0.5)';
|
|
217
|
+
this.cursor('pointer');
|
|
218
|
+
this.needUpdate = true;
|
|
219
|
+
});
|
|
339
220
|
```
|
|
340
221
|
|
|
341
|
-
|
|
222
|
+
### 支持的事件
|
|
342
223
|
|
|
343
|
-
|
|
344
|
-
|
|
224
|
+
| 事件名称 | 说明 | 回调参数 |
|
|
225
|
+
| :- | :- | :- |
|
|
226
|
+
| mousedown | 鼠标按下 | - |
|
|
227
|
+
| mousemove | 鼠标移动 | {target, position} |
|
|
228
|
+
| mouseover | 鼠标移入 | {target} |
|
|
229
|
+
| mouseleave | 鼠标移出 | {target} |
|
|
230
|
+
| mouseup | 鼠标松开 | - |
|
|
231
|
+
| click | 鼠标点击 | - |
|
|
232
|
+
| dblclick | 鼠标双击 | - |
|
|
233
|
+
| touchstart | 触摸开始 | {position} |
|
|
234
|
+
| touchmove | 触摸移动 | {position} |
|
|
235
|
+
| touchend | 触摸结束 | {position} |
|
|
345
236
|
|
|
346
|
-
|
|
347
|
-
或写一个class的js文件,构建成es5的。
|
|
237
|
+
## 🔧 自定义控件
|
|
348
238
|
|
|
239
|
+
大多数控件继承 `jmPath` 即可,通过实现 `initPoints` 方法来绘制自定义图形:
|
|
349
240
|
|
|
350
|
-
##### 示例
|
|
351
|
-
来画一个X
|
|
352
|
-
在线示例:[http://jiamao.github.io/jmgraph/example/controls/test.html](http://jiamao.github.io/jmgraph/example/controls/test.html)
|
|
353
241
|
```javascript
|
|
354
|
-
import {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
super(params);
|
|
363
|
-
this.center = params.center || {x:0, y:0};
|
|
364
|
-
this.radius = params.radius || 0;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
//定义属性
|
|
368
|
-
/**
|
|
369
|
-
* 中心点
|
|
370
|
-
* point格式:{x:0,y:0,m:true}
|
|
371
|
-
* @property center
|
|
372
|
-
* @type {point}
|
|
373
|
-
*/
|
|
374
|
-
get center() {
|
|
375
|
-
return this.property('center');
|
|
376
|
-
}
|
|
377
|
-
set center(v) {
|
|
378
|
-
return this.property('center', v);
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* 半径
|
|
382
|
-
* @property radius
|
|
383
|
-
* @type {number}
|
|
384
|
-
*/
|
|
385
|
-
get radius() {
|
|
386
|
-
return this.property('radius');
|
|
387
|
-
}
|
|
388
|
-
set radius(v) {
|
|
389
|
-
return this.property('radius', v);
|
|
390
|
-
}
|
|
242
|
+
import {jmPath} from "jmgraph";
|
|
243
|
+
|
|
244
|
+
class CustomShape extends jmPath {
|
|
245
|
+
constructor(params) {
|
|
246
|
+
super(params);
|
|
247
|
+
this.center = params.center || {x: 0, y: 0};
|
|
248
|
+
this.radius = params.radius || 0;
|
|
249
|
+
}
|
|
391
250
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
initPoints() {
|
|
401
|
-
//可以获取当前控件的左上坐标,可以用来画相对位置
|
|
402
|
-
var location = this.getLocation();//获取位置参数
|
|
403
|
-
|
|
404
|
-
var cx = location.center.x ;
|
|
405
|
-
var cy = location.center.y ;
|
|
251
|
+
initPoints() {
|
|
252
|
+
const location = this.getLocation();
|
|
253
|
+
const cx = location.center.x;
|
|
254
|
+
const cy = location.center.y;
|
|
255
|
+
|
|
256
|
+
this.points = [];
|
|
257
|
+
this.points.push({x: cx - this.radius, y: cy - this.radius});
|
|
258
|
+
this.points.push({x: cx + this.radius, y: cy + this.radius});
|
|
406
259
|
|
|
407
|
-
|
|
260
|
+
return this.points;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
```
|
|
408
264
|
|
|
409
|
-
|
|
265
|
+
## 📱 微信小程序支持
|
|
410
266
|
|
|
411
|
-
|
|
412
|
-
//由于是圆,偏移量相同
|
|
413
|
-
var offw = Math.sqrt(location.radius * location.radius / 2);
|
|
414
|
-
//左上角到右下角对角线
|
|
415
|
-
this.points.push({x:cx - offw, y:cy-offw}, {x:cx + offw, y:cy+offw});
|
|
267
|
+
jmGraph 支持微信小程序,详情请参考 [mini-jmchart](https://github.com/fefeding/mini-jmchart)。
|
|
416
268
|
|
|
417
|
-
|
|
418
|
-
//画完上面的线后,需要重新移到这条线的起点,指定m:true即可
|
|
419
|
-
this.points.push({x:cx - offw, y:cy+offw, m:true}, {x:cx + offw, y:cy-offw});
|
|
269
|
+
### 使用方法
|
|
420
270
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
}
|
|
424
|
-
```
|
|
271
|
+
```javascript
|
|
272
|
+
const jmGraph = require('../../utils/jmgraph');
|
|
425
273
|
|
|
274
|
+
const g = jmGraph.create('mycanvas', {
|
|
275
|
+
style: {fill: '#000'},
|
|
276
|
+
width: 400,
|
|
277
|
+
height: 600
|
|
278
|
+
});
|
|
426
279
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
280
|
+
this.canvastouchstart = function (...arg) {
|
|
281
|
+
return g.eventHandler.touchStart(...arg);
|
|
282
|
+
}
|
|
283
|
+
this.canvastouchmove = function (...arg) {
|
|
284
|
+
return g.eventHandler.touchMove(...arg);
|
|
285
|
+
}
|
|
286
|
+
this.canvastouchend = function (...arg) {
|
|
287
|
+
return g.eventHandler.touchEnd(...arg);
|
|
288
|
+
}
|
|
289
|
+
```
|
|
430
290
|
|
|
431
|
-
|
|
291
|
+
## 🛠️ 开发
|
|
432
292
|
|
|
433
|
-
|
|
293
|
+
### 构建
|
|
434
294
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
```html
|
|
438
|
-
<canvas style="width: 400px; height: 600px;background:#000;"
|
|
439
|
-
canvas-id="mycanvas"
|
|
440
|
-
bindtouchstart="canvastouchstart"
|
|
441
|
-
bindtouchmove="canvastouchmove"
|
|
442
|
-
bindtouchend="canvastouchend"
|
|
443
|
-
bindtouchcancel="canvastouchcancel">
|
|
444
|
-
</canvas>
|
|
295
|
+
```bash
|
|
296
|
+
npm run build
|
|
445
297
|
```
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
onReady: function () {
|
|
452
|
-
|
|
453
|
-
//这里引用jmgraph
|
|
454
|
-
let jmGraph = require('../../utils/jmgraph');
|
|
455
|
-
|
|
456
|
-
var self = this;
|
|
457
|
-
|
|
458
|
-
var g = jmGraph.create('mycanvas', {
|
|
459
|
-
style: {
|
|
460
|
-
fill: '#000'
|
|
461
|
-
},
|
|
462
|
-
width: 400,
|
|
463
|
-
height: 600
|
|
464
|
-
});
|
|
465
|
-
init(g);
|
|
466
|
-
|
|
467
|
-
function init(g) {
|
|
468
|
-
//g.style.fill = '#000'; //画布背景
|
|
469
|
-
var style = {
|
|
470
|
-
stroke: '#46BF86',
|
|
471
|
-
fill: '#556662',
|
|
472
|
-
lineWidth: 2
|
|
473
|
-
};
|
|
474
|
-
style.shadow = '0,0,10,#fff';
|
|
475
|
-
//style.opacity = 0.2;
|
|
476
|
-
//style.lineCap = 'round';
|
|
477
|
-
|
|
478
|
-
//创建一个方块
|
|
479
|
-
var rect = g.createShape('rect', {
|
|
480
|
-
style: style,
|
|
481
|
-
position: { x: 100, y: 100 },
|
|
482
|
-
width: 100,
|
|
483
|
-
height: 100
|
|
484
|
-
});
|
|
485
|
-
rect.canMove(true);
|
|
486
|
-
g.children.add(rect);
|
|
487
|
-
|
|
488
|
-
function update() {
|
|
489
|
-
if (g.needUpdate) g.redraw();
|
|
490
|
-
setTimeout(update, 20);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
update();
|
|
494
|
-
|
|
495
|
-
//初始化jmGraph事件
|
|
496
|
-
//把小程序中的canvas事件交给jmGraph处理
|
|
497
|
-
this.canvastouchstart = function (...arg) {
|
|
498
|
-
return g.eventHandler.touchStart(...arg);
|
|
499
|
-
}
|
|
500
|
-
this.canvastouchmove = function (...arg) {
|
|
501
|
-
return g.eventHandler.touchMove(...arg);
|
|
502
|
-
}
|
|
503
|
-
this.canvastouchend = function (...arg) {
|
|
504
|
-
return g.eventHandler.touchEnd(...arg);
|
|
505
|
-
}
|
|
506
|
-
this.canvastouchcancel = function (...arg) {
|
|
507
|
-
return g.eventHandler.touchCancel(...arg);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
298
|
+
|
|
299
|
+
### 运行示例
|
|
300
|
+
|
|
301
|
+
```bash
|
|
302
|
+
npm run dev
|
|
511
303
|
```
|
|
512
304
|
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
305
|
+
## 🤝 贡献
|
|
306
|
+
|
|
307
|
+
欢迎贡献代码!请遵循以下步骤:
|
|
308
|
+
|
|
309
|
+
1. Fork 本仓库
|
|
310
|
+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
|
|
311
|
+
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
|
|
312
|
+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
|
|
313
|
+
5. 开启 Pull Request
|
|
314
|
+
|
|
315
|
+
## 📄 许可证
|
|
316
|
+
|
|
317
|
+
本项目采用 [MIT](LICENSE) 许可证。
|
|
318
|
+
|
|
319
|
+
## 💬 讨论
|
|
320
|
+
|
|
321
|
+
- [GitHub Issues](https://github.com/fefeding/jmgraph/issues) - 报告 Bug 和功能请求
|
|
322
|
+
- [GitHub Discussions](https://github.com/fefeding/jmgraph/discussions) - 问题和讨论
|
|
323
|
+
|
|
324
|
+
## 🙏 致谢
|
|
325
|
+
|
|
326
|
+
感谢所有为本项目做出贡献的开发者!
|
|
327
|
+
|
|
328
|
+
## 🔗 相关项目
|
|
329
|
+
|
|
330
|
+
- [jmChart](https://github.com/fefeding/jmchart) - 基于 jmGraph 的图表库
|
|
331
|
+
- [mini-jmchart](https://github.com/fefeding/mini-jmchart) - 微信小程序图表库
|
|
332
|
+
|
|
333
|
+
## 📮 联系方式
|
|
334
|
+
|
|
335
|
+
- 作者: jiamao
|
|
336
|
+
- 邮箱: haofefe@163.com
|
|
337
|
+
- 主页: https://fefeding.github.io/jmgraph/
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
如果这个项目对你有帮助,请给个 ⭐️ Star!
|