jmgraph 3.2.16 → 3.2.18
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 +251 -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 +3500 -2668
- 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 +58 -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 +522 -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 +137 -11
- package/src/lib/webgl/path.js +568 -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,282 @@ 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
|
-
| shadow
|
|
128
|
-
|
|
|
129
|
-
|
|
|
130
|
-
|
|
|
131
|
-
|
|
|
132
|
-
|
|
|
133
|
-
|
|
|
134
|
-
|
|
|
135
|
-
|
|
|
136
|
-
|
|
|
137
|
-
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
98
|
+
- [在线示例](https://fefeding.github.io/jmgraph/example/index.html)
|
|
99
|
+
- [基于 jmGraph 的图表库](https://github.com/fefeding/jmchart)
|
|
100
|
+
|
|
101
|
+
## 🎨 样式说明
|
|
102
|
+
|
|
103
|
+
jmGraph 支持简化的样式名称和原生 Canvas 样式:
|
|
104
|
+
|
|
105
|
+
| 简化名称 | 原生名称 | 说明 |
|
|
106
|
+
| :- | :- | :- |
|
|
107
|
+
| fill | fillStyle | 填充颜色、渐变或模式 |
|
|
108
|
+
| stroke | strokeStyle | 描边颜色、渐变或模式 |
|
|
109
|
+
| shadow | - | 阴影,格式:'0,0,10,#fff' |
|
|
110
|
+
| shadow.blur | shadowBlur | 阴影模糊级别 |
|
|
111
|
+
| shadow.x | shadowOffsetX | 阴影水平偏移 |
|
|
112
|
+
| shadow.y | shadowOffsetY | 阴影垂直偏移 |
|
|
113
|
+
| shadow.color | shadowColor | 阴影颜色 |
|
|
114
|
+
| lineWidth | lineWidth | 线条宽度 |
|
|
115
|
+
| miterLimit | miterLimit | 最大斜接长度 |
|
|
116
|
+
| font | font | 字体 |
|
|
117
|
+
| fontSize | font | 字体大小 |
|
|
118
|
+
| fontFamily | font | 字体名称 |
|
|
119
|
+
| opacity | globalAlpha | 透明度 |
|
|
120
|
+
| textAlign | textAlign | 文本水平对齐 |
|
|
121
|
+
| textBaseline | textBaseline | 文本垂直对齐 |
|
|
122
|
+
| lineJoin | lineJoin | 线条连接样式 |
|
|
123
|
+
| lineCap | lineCap | 线条端点样式 |
|
|
124
|
+
|
|
125
|
+
## 🎯 内置图形
|
|
126
|
+
|
|
127
|
+
### 矩形 (Rect)
|
|
142
128
|
|
|
143
|
-
事件的绑定函数:`bind/unbind`
|
|
144
|
-
示例:
|
|
145
129
|
```javascript
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
this.cursor('pointer');
|
|
152
|
-
this.neadUpdate = true; //需要刷新
|
|
130
|
+
const rect = g.createShape('rect', {
|
|
131
|
+
style: style,
|
|
132
|
+
position: {x: 100, y: 100},
|
|
133
|
+
width: 100,
|
|
134
|
+
height: 100
|
|
153
135
|
});
|
|
154
136
|
```
|
|
155
137
|
|
|
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)
|
|
138
|
+
### 圆形/椭圆 (Arc)
|
|
178
139
|
|
|
179
140
|
```javascript
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
141
|
+
const arc = g.createShape('arc', {
|
|
142
|
+
style: style,
|
|
143
|
+
center: {x: 100, y: 150},
|
|
144
|
+
width: 120,
|
|
145
|
+
height: 80
|
|
146
|
+
});
|
|
186
147
|
```
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
具体请参考示例。
|
|
190
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/arc.html)
|
|
148
|
+
|
|
149
|
+
### 线条 (Line)
|
|
191
150
|
|
|
192
151
|
```javascript
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
height: 80
|
|
199
|
-
});
|
|
152
|
+
const line = g.createLine(
|
|
153
|
+
{x: 10, y: 200},
|
|
154
|
+
{x: 80, y: 120},
|
|
155
|
+
style
|
|
156
|
+
);
|
|
200
157
|
```
|
|
201
158
|
|
|
202
|
-
|
|
203
|
-
`arrow`为创建一个箭头,
|
|
204
|
-
`Arrowline`是一条带箭头的直线。
|
|
205
|
-
具体请参考示例。
|
|
206
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/Arrowline.html)
|
|
159
|
+
### 箭头 (Arrow)
|
|
207
160
|
|
|
208
161
|
```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
|
-
});
|
|
162
|
+
const arrow = g.createShape('arrow', {
|
|
163
|
+
style: style,
|
|
164
|
+
start: {x: 150, y: 120},
|
|
165
|
+
end: {x: 160, y: 150}
|
|
166
|
+
});
|
|
224
167
|
```
|
|
225
168
|
|
|
226
|
-
|
|
227
|
-
`bezier`可以指定无数个控制点,绘制复杂的曲线。
|
|
228
|
-
具体请参考示例。
|
|
229
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/bezier.html)
|
|
169
|
+
### 贝塞尔曲线 (Bezier)
|
|
230
170
|
|
|
231
171
|
```javascript
|
|
232
|
-
|
|
233
|
-
|
|
172
|
+
const bezier = g.createShape('bezier', {
|
|
173
|
+
style: style,
|
|
174
|
+
points: [p0, p1, p2, p3, p4]
|
|
175
|
+
});
|
|
234
176
|
```
|
|
235
177
|
|
|
236
|
-
|
|
237
|
-
`img`是用来承载一张图片的控件,可以用style.src来指定图片url。
|
|
238
|
-
具体请参考示例。
|
|
239
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/img.html)
|
|
178
|
+
### 图片 (Image)
|
|
240
179
|
|
|
241
180
|
```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
|
-
//设置图片可以用鼠标移动
|
|
181
|
+
const img = g.createShape('image', {
|
|
182
|
+
style: {src: 'image.png'},
|
|
183
|
+
position: {x: 100, y: 100}
|
|
184
|
+
});
|
|
254
185
|
img.canMove(true);
|
|
255
186
|
```
|
|
256
187
|
|
|
257
|
-
|
|
258
|
-
`label`可以用来绘制文字,通过style指定样式。
|
|
259
|
-
具体请参考示例。
|
|
260
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/label.html)
|
|
188
|
+
### 文字 (Label)
|
|
261
189
|
|
|
262
190
|
```javascript
|
|
263
|
-
|
|
191
|
+
const label = g.createShape('label', {
|
|
192
|
+
style: {
|
|
264
193
|
stroke: '#effaaa',
|
|
265
194
|
fill: '#fff',
|
|
266
|
-
textAlign: 'center',
|
|
267
|
-
textBaseline: 'middle',
|
|
195
|
+
textAlign: 'center',
|
|
196
|
+
textBaseline: 'middle',
|
|
268
197
|
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
|
-
});
|
|
198
|
+
fontFamily: 'Arial'
|
|
199
|
+
},
|
|
200
|
+
position: {x: 200, y: 150},
|
|
201
|
+
text: 'Hello World',
|
|
202
|
+
width: 120,
|
|
203
|
+
height: 80
|
|
204
|
+
});
|
|
292
205
|
```
|
|
293
206
|
|
|
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
|
-
```
|
|
207
|
+
## 🎮 事件系统
|
|
307
208
|
|
|
308
|
-
|
|
309
|
-
`resize` 可以自由放大缩小的控件。
|
|
310
|
-
具体请参考示例。
|
|
311
|
-
[在线示例](http://jiamao.github.io/jmgraph/example/controls/resize.html)
|
|
209
|
+
### 事件绑定
|
|
312
210
|
|
|
313
211
|
```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
|
-
});
|
|
212
|
+
const shape = g.createShape('rect', {...});
|
|
213
|
+
|
|
214
|
+
shape.bind('mouseover', function(evt) {
|
|
215
|
+
this.style.stroke = 'rgba(39,72,188,0.5)';
|
|
216
|
+
this.cursor('pointer');
|
|
217
|
+
this.needUpdate = true;
|
|
218
|
+
});
|
|
339
219
|
```
|
|
340
220
|
|
|
341
|
-
|
|
221
|
+
### 支持的事件
|
|
342
222
|
|
|
343
|
-
|
|
344
|
-
|
|
223
|
+
| 事件名称 | 说明 | 回调参数 |
|
|
224
|
+
| :- | :- | :- |
|
|
225
|
+
| mousedown | 鼠标按下 | - |
|
|
226
|
+
| mousemove | 鼠标移动 | {target, position} |
|
|
227
|
+
| mouseover | 鼠标移入 | {target} |
|
|
228
|
+
| mouseleave | 鼠标移出 | {target} |
|
|
229
|
+
| mouseup | 鼠标松开 | - |
|
|
230
|
+
| click | 鼠标点击 | - |
|
|
231
|
+
| dblclick | 鼠标双击 | - |
|
|
232
|
+
| touchstart | 触摸开始 | {position} |
|
|
233
|
+
| touchmove | 触摸移动 | {position} |
|
|
234
|
+
| touchend | 触摸结束 | {position} |
|
|
345
235
|
|
|
346
|
-
|
|
347
|
-
或写一个class的js文件,构建成es5的。
|
|
236
|
+
## 🔧 自定义控件
|
|
348
237
|
|
|
238
|
+
大多数控件继承 `jmPath` 即可,通过实现 `initPoints` 方法来绘制自定义图形:
|
|
349
239
|
|
|
350
|
-
##### 示例
|
|
351
|
-
来画一个X
|
|
352
|
-
在线示例:[http://jiamao.github.io/jmgraph/example/controls/test.html](http://jiamao.github.io/jmgraph/example/controls/test.html)
|
|
353
240
|
```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
|
-
}
|
|
241
|
+
import {jmPath} from "jmgraph";
|
|
242
|
+
|
|
243
|
+
class CustomShape extends jmPath {
|
|
244
|
+
constructor(params) {
|
|
245
|
+
super(params);
|
|
246
|
+
this.center = params.center || {x: 0, y: 0};
|
|
247
|
+
this.radius = params.radius || 0;
|
|
248
|
+
}
|
|
391
249
|
|
|
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 ;
|
|
250
|
+
initPoints() {
|
|
251
|
+
const location = this.getLocation();
|
|
252
|
+
const cx = location.center.x;
|
|
253
|
+
const cy = location.center.y;
|
|
254
|
+
|
|
255
|
+
this.points = [];
|
|
256
|
+
this.points.push({x: cx - this.radius, y: cy - this.radius});
|
|
257
|
+
this.points.push({x: cx + this.radius, y: cy + this.radius});
|
|
406
258
|
|
|
407
|
-
|
|
259
|
+
return this.points;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
```
|
|
408
263
|
|
|
409
|
-
|
|
264
|
+
## 📱 微信小程序支持
|
|
410
265
|
|
|
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});
|
|
266
|
+
jmGraph 支持微信小程序,详情请参考 [mini-jmchart](https://github.com/fefeding/mini-jmchart)。
|
|
416
267
|
|
|
417
|
-
|
|
418
|
-
//画完上面的线后,需要重新移到这条线的起点,指定m:true即可
|
|
419
|
-
this.points.push({x:cx - offw, y:cy+offw, m:true}, {x:cx + offw, y:cy-offw});
|
|
268
|
+
### 使用方法
|
|
420
269
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
}
|
|
424
|
-
```
|
|
270
|
+
```javascript
|
|
271
|
+
const jmGraph = require('../../utils/jmgraph');
|
|
425
272
|
|
|
273
|
+
const g = jmGraph.create('mycanvas', {
|
|
274
|
+
style: {fill: '#000'},
|
|
275
|
+
width: 400,
|
|
276
|
+
height: 600
|
|
277
|
+
});
|
|
426
278
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
279
|
+
this.canvastouchstart = function (...arg) {
|
|
280
|
+
return g.eventHandler.touchStart(...arg);
|
|
281
|
+
}
|
|
282
|
+
this.canvastouchmove = function (...arg) {
|
|
283
|
+
return g.eventHandler.touchMove(...arg);
|
|
284
|
+
}
|
|
285
|
+
this.canvastouchend = function (...arg) {
|
|
286
|
+
return g.eventHandler.touchEnd(...arg);
|
|
287
|
+
}
|
|
288
|
+
```
|
|
430
289
|
|
|
431
|
-
|
|
290
|
+
## 🛠️ 开发
|
|
432
291
|
|
|
433
|
-
|
|
292
|
+
### 构建
|
|
434
293
|
|
|
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>
|
|
294
|
+
```bash
|
|
295
|
+
npm run build
|
|
445
296
|
```
|
|
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
|
-
}
|
|
297
|
+
|
|
298
|
+
### 运行示例
|
|
299
|
+
|
|
300
|
+
```bash
|
|
301
|
+
npm run dev
|
|
511
302
|
```
|
|
512
303
|
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
304
|
+
## 🤝 贡献
|
|
305
|
+
|
|
306
|
+
欢迎贡献代码!请遵循以下步骤:
|
|
307
|
+
|
|
308
|
+
1. Fork 本仓库
|
|
309
|
+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
|
|
310
|
+
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
|
|
311
|
+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
|
|
312
|
+
5. 开启 Pull Request
|
|
313
|
+
|
|
314
|
+
## 📄 许可证
|
|
315
|
+
|
|
316
|
+
本项目采用 [MIT](LICENSE) 许可证。
|
|
317
|
+
|
|
318
|
+
## 💬 讨论
|
|
319
|
+
|
|
320
|
+
- [GitHub Issues](https://github.com/fefeding/jmgraph/issues) - 报告 Bug 和功能请求
|
|
321
|
+
- [GitHub Discussions](https://github.com/fefeding/jmgraph/discussions) - 问题和讨论
|
|
322
|
+
|
|
323
|
+
## 🙏 致谢
|
|
324
|
+
|
|
325
|
+
感谢所有为本项目做出贡献的开发者!
|
|
326
|
+
|
|
327
|
+
## 🔗 相关项目
|
|
328
|
+
|
|
329
|
+
- [jmChart](https://github.com/fefeding/jmchart) - 基于 jmGraph 的图表库
|
|
330
|
+
- [mini-jmchart](https://github.com/fefeding/mini-jmchart) - 微信小程序图表库
|
|
331
|
+
|
|
332
|
+
## 📮 联系方式
|
|
333
|
+
|
|
334
|
+
- 作者: jiamao
|
|
335
|
+
- 邮箱: haofefe@163.com
|
|
336
|
+
- 主页: https://fefeding.github.io/jmgraph/
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
如果这个项目对你有帮助,请给个 ⭐️ Star!
|