jmgraph 3.2.9 → 3.2.10

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.
@@ -47,7 +47,7 @@
47
47
  fill: 'yellow',
48
48
  lineWidth: 2, //边线宽
49
49
  rotation: {
50
- angle: Math.PI / 3
50
+ angle: 0
51
51
  },
52
52
  //小方块样式
53
53
  rectStyle: {
@@ -19,6 +19,7 @@
19
19
  </body>
20
20
  <script type="module">
21
21
  import {jmGraph} from "../../index.js";
22
+ import {jmRect} from "../../src/shapes/jmRect.js";
22
23
  import {jmPath} from "../../src/core/jmPath.js";
23
24
  /**
24
25
  * 测试
@@ -91,7 +92,211 @@
91
92
  }
92
93
  }
93
94
 
95
+ // 图片裁剪框
96
+ class ImageEditCover extends jmPath {
97
+ constructor(params = {}) {
98
+ super(params);
99
+ this.style.close = this.style.close || true;
100
+ this.init(params);
101
+ }
102
+ // 初始化控件元素
103
+ init(params) {
104
+ this.width = this.width || '100%';
105
+ this.height = this.height || '100%';
106
+ // 生成层
107
+ const rectStyle = {
108
+ stroke: 'transparent',
109
+ fill: 'rgba(100,100,100,0.4)',
110
+ lineWidth: 0.1,
111
+ zIndex: 1,
112
+ };
113
+ const graph = (this.graph || params.graph);
114
+ const leftRect = graph.createShape(jmRect, {
115
+ position:{x:0, y:0 },
116
+ width: 0,
117
+ height: '100%',
118
+ style: rectStyle,
119
+ interactive: false
120
+ });
121
+ const topRect = graph.createShape(jmRect, {
122
+ position:{x:0, y:0 },
123
+ width: '100%',
124
+ height: 0,
125
+ style: rectStyle,
126
+ interactive: false
127
+ });
128
+ const rightRect = graph.createShape(jmRect, {
129
+ position:{x:0, y:0 },
130
+ width: 0,
131
+ height: '100%',
132
+ style: rectStyle,
133
+ interactive: false
134
+ });
135
+ const bottomRect = graph.createShape(jmRect, {
136
+ position:{x:0, y:0 },
137
+ width: '100%',
138
+ height: 0,
139
+ style: rectStyle,
140
+ interactive: false
141
+ });
142
+ this.rects.push(leftRect, topRect, rightRect, bottomRect);
143
+ this.children.add(leftRect);
144
+ this.children.add(topRect);
145
+ this.children.add(rightRect);
146
+ this.children.add(bottomRect);
147
+
148
+ const controlStyle = {
149
+ lineWidth: 3,
150
+ stroke: '#fff',
151
+ zIndex: 100,
152
+ }
153
+ const c1 = graph.createShape(jmPath, {
154
+ style: controlStyle,
155
+ interactive: true
156
+ });
157
+ const c2 = graph.createShape(jmPath, {
158
+ style: controlStyle,
159
+ interactive: true
160
+ });
161
+ const c3 = graph.createShape(jmPath, {
162
+ style: controlStyle,
163
+ interactive: true
164
+ });
165
+ const c4 = graph.createShape(jmPath, {
166
+ style: controlStyle,
167
+ interactive: true
168
+ });
169
+ this.controls.push(c1, c2, c3, c4);
170
+
171
+ this.children.add(c1);
172
+ this.children.add(c2);
173
+ this.children.add(c3);
174
+ this.children.add(c4);
175
+
176
+ c1.canMove(true);
177
+ c2.canMove(true);
178
+ c3.canMove(true);
179
+ c4.canMove(true);
180
+
181
+ }
182
+
183
+ // 裁剪的目录区域
184
+ // 最终改变结果也是这个参数
185
+ targetBounds = {
186
+ x: 0,
187
+ y: 0,
188
+ width: 0,
189
+ height: 0
190
+ }
191
+
192
+ rects = [];
193
+ // 操作折角
194
+ controls = [];
195
+
196
+ /**
197
+ * 初始化图形点
198
+ * 控件都是由点形成
199
+ *
200
+ * @method initPoint
201
+ * @private
202
+ * @for jmArc
203
+ */
204
+ initPoints() {
205
+ //可以获取当前控件的左上坐标,可以用来画相对位置
206
+ const location = this.getLocation();//获取位置参数
207
+
208
+ const targetLeft = location.width / 2 - this.targetBounds.width / 2;
209
+ const targetRight = location.width / 2 + this.targetBounds.width / 2;
210
+ const targetBottom = this.targetBounds.y + this.targetBounds.height;
211
+
212
+ this.rects[0].width = targetLeft + 0.1;
213
+
214
+ this.rects[1].height = this.targetBounds.y;
215
+ this.rects[1].width = this.targetBounds.width + 0.1;
216
+ this.rects[1].position.x = targetLeft;
217
+
218
+ this.rects[2].width = location.width - targetRight;
219
+ this.rects[2].position.x = targetRight;
220
+
221
+ this.rects[3].height = location.height - targetBottom;
222
+ this.rects[3].width = this.targetBounds.width + 0.1;
223
+ this.rects[3].position.x = targetLeft;
224
+ this.rects[3].position.y = targetBottom;
225
+
226
+ return this.points;
227
+ }
228
+ resetControlsPosition() {
229
+ //可以获取当前控件的左上坐标,可以用来画相对位置
230
+ const location = this.getLocation();//获取位置参数
231
+
232
+ const targetLeft = location.width / 2 - this.targetBounds.width / 2;
233
+ const targetRight = location.width / 2 + this.targetBounds.width / 2;
234
+ const targetBottom = this.targetBounds.y + this.targetBounds.height;
94
235
 
236
+ // 操作折角位置
237
+ const controlWidth = 10;
238
+ const controlLineWidth = this.controls[0].style.lineWidth || 2;
239
+ this.controls[0].points = [
240
+ {
241
+ x: targetLeft - controlLineWidth/2,
242
+ y: this.targetBounds.y + controlWidth
243
+ },
244
+ {
245
+ x: targetLeft - controlLineWidth/2,
246
+ y: this.targetBounds.y - controlLineWidth/2
247
+ },
248
+ {
249
+ x: targetLeft + controlWidth,
250
+ y: this.targetBounds.y - controlLineWidth/2
251
+ }
252
+ ];
253
+
254
+ this.controls[1].points = [
255
+ {
256
+ x: targetRight - controlWidth,
257
+ y: this.targetBounds.y - controlLineWidth/2
258
+ },
259
+ {
260
+ x: targetRight + controlLineWidth/2,
261
+ y: this.targetBounds.y - controlLineWidth/2
262
+ },
263
+ {
264
+ x: targetRight + controlLineWidth/2,
265
+ y: this.targetBounds.y + controlWidth
266
+ }
267
+ ];
268
+
269
+ this.controls[2].points = [
270
+ {
271
+ x: targetRight - controlWidth,
272
+ y: targetBottom + controlLineWidth/2
273
+ },
274
+ {
275
+ x: targetRight + controlLineWidth/2,
276
+ y: targetBottom + controlLineWidth/2
277
+ },
278
+ {
279
+ x: targetRight + controlLineWidth/2,
280
+ y: targetBottom - controlWidth
281
+ }
282
+ ];
283
+
284
+ this.controls[3].points = [
285
+ {
286
+ x: targetLeft - controlLineWidth/2,
287
+ y: targetBottom - controlWidth
288
+ },
289
+ {
290
+ x: targetLeft - controlLineWidth/2,
291
+ y: targetBottom + controlLineWidth/2
292
+ },
293
+ {
294
+ x: targetLeft + controlWidth,
295
+ y: targetBottom + controlLineWidth/2
296
+ }
297
+ ];
298
+ }
299
+ }
95
300
  var container = document.getElementById('mycanvas_container');
96
301
 
97
302
  var g = jmGraph.create(container, {
@@ -125,6 +330,25 @@
125
330
  g.children.add(t);
126
331
  t.canMove(true);
127
332
 
333
+ const editorCover = g.createShape(ImageEditCover, {
334
+ style: {
335
+ fill: 'transparent',
336
+ stroke: 'transparent',
337
+ },
338
+ interactive: true
339
+ });
340
+
341
+ editorCover.targetBounds = {
342
+ x: 100,
343
+ y: 100,
344
+ width: 200,
345
+ height: 300,
346
+ };
347
+ //editorCover.visible = false;
348
+ g.children.add(editorCover);
349
+
350
+ editorCover.resetControlsPosition();
351
+
128
352
  function update() {
129
353
  if(g.needUpdate) g.redraw();
130
354
  requestAnimationFrame(update);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "family": "jmgraph",
3
3
  "name": "jmgraph",
4
- "version": "3.2.9",
4
+ "version": "3.2.10",
5
5
  "description": "一个简单的canvas画图库",
6
6
  "homepage": "http://graph.jm47.com/",
7
7
  "keywords": [
@@ -1151,13 +1151,13 @@ export default class jmControl extends jmProperty {
1151
1151
  if(ps && ps.length) {
1152
1152
  const rotation = this.getRotation(null, bounds);//获取当前旋转参数
1153
1153
  //如果有旋转参数,则需要转换坐标再处理
1154
- if(rotation && rotation.angle != 0) {
1154
+ if(rotation && rotation.angle) {
1155
1155
  ps = jmUtils.clone(ps, true);//拷贝一份数据
1156
1156
  //rotateX ,rotateY 是相对当前控件的位置
1157
1157
  ps = jmUtils.rotatePoints(ps, {
1158
1158
  x: rotation.x + bounds.left,
1159
1159
  y: rotation.y + bounds.top
1160
- }, rotation.angle);
1160
+ }, rotation.angle || 0);
1161
1161
  }
1162
1162
  //如果当前路径不是实心的
1163
1163
  //就只用判断点是否在边上即可
@@ -1210,6 +1210,7 @@ export default class jmControl extends jmProperty {
1210
1210
  if(this.visible === false) return ;//如果不显示则不响应事件
1211
1211
  if(!args.position) {
1212
1212
  const graph = this.graph;
1213
+ args.isWXMiniApp = graph.isWXMiniApp;
1213
1214
 
1214
1215
  const srcElement = args.srcElement || args.target;
1215
1216
 
@@ -1222,7 +1223,8 @@ export default class jmControl extends jmProperty {
1222
1223
  ctrlKey: args.ctrlKey,
1223
1224
  cancel : false,
1224
1225
  event: args, // 原生事件
1225
- srcElement : srcElement
1226
+ srcElement : srcElement,
1227
+ isWXMiniApp: graph.isWXMiniApp,
1226
1228
  };
1227
1229
  }
1228
1230
  args.path = args.path||[]; //事件冒泡路径
@@ -334,9 +334,16 @@ export default class jmUtils {
334
334
  let ox = evt.offsetX;
335
335
  let oy = evt.offsetY;
336
336
  if(typeof ox === 'undefined' && typeof oy === 'undefined') {
337
- let p = this.getElementPosition(target);
338
- ox= px - p.left;
339
- oy = py - p.top;
337
+ // 小程序下取x,y就是它的相对坐标
338
+ if(evt.isWXMiniApp) {
339
+ ox = evt.x;
340
+ oy = evt.y;
341
+ }
342
+ else {
343
+ let p = this.getElementPosition(target);
344
+ ox= px - p.left;
345
+ oy = py - p.top;
346
+ }
340
347
  }
341
348
  if(scale) {
342
349
  if(scale.x) ox = ox / scale.x;
@@ -175,7 +175,10 @@ export default class jmImage extends jmControl {
175
175
  //如果当次计算过,则不重复计算
176
176
  if(this.bounds && !isReset) return this.bounds;
177
177
  let rect = {};
178
- let img = this.getImage();
178
+ let img = this.getImage() || {
179
+ width: 0,
180
+ height: 0
181
+ };
179
182
  let p = this.getLocation();
180
183
  let w = p.width || img.width;
181
184
  let h = p.height || img.height;