@leafer-in/arrow 2.0.2 → 2.0.4

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/dist/arrow.cjs CHANGED
@@ -106,12 +106,14 @@ const {layout: layout, rotate: rotate} = PathMatrixHelper;
106
106
 
107
107
  const {getAngle: getAngle} = draw.PointHelper;
108
108
 
109
- const half = {
109
+ const zero = {
110
+ x: 0
111
+ }, half = {
110
112
  x: -.5
111
113
  };
112
114
 
113
115
  const angle = {
114
- connect: half,
116
+ connect: zero,
115
117
  offset: {
116
118
  x: -.71,
117
119
  bevelJoin: .36,
@@ -132,73 +134,110 @@ const angleSide = {
132
134
 
133
135
  const triangleLinePath = [ 1, -3, 0, 2, -3, -2, 2, 0, 0, 2, -3, 2, 2, -3, 0 ];
134
136
 
135
- const triangle = {
136
- connect: half,
137
+ const triangleFill = {
138
+ connect: zero,
137
139
  offset: {
138
140
  x: -.9,
139
141
  bevelJoin: .624,
140
142
  roundJoin: .4
141
143
  },
142
- path: [ ...triangleLinePath, 1, -2.05, 1.1, 2, -2.05, -1.1 ],
143
- dashPath: [ 1, -2, 0, 2, -.5, 0 ]
144
+ path: [ ...triangleLinePath ]
144
145
  };
145
146
 
146
- const arrowLinePath = [ 1, -3, 0, 2, -3.5, -1.8, 2, 0, 0, 2, -3.5, 1.8, 2, -3, 0 ];
147
+ const triangle = draw.DataHelper.clone(triangleFill);
147
148
 
148
- const arrow = {
149
- connect: half,
150
- offset: {
151
- x: -1.1,
152
- bevelJoin: .872,
153
- roundJoin: .6
154
- },
155
- path: [ ...arrowLinePath, 1, -2.25, .95, 2, -2.25, -.95 ],
156
- dashPath: [ 1, -3, 0, 2, -.5, 0 ]
149
+ triangle.path = [ ...triangleLinePath, 1, -2.05, 1.1, 2, -2.05, -1.1 ];
150
+
151
+ triangle.dashPath = [ 1, -2, 0, 2, -.5, 0 ];
152
+
153
+ const triangleFlipFill = {
154
+ connect: zero,
155
+ offset: half,
156
+ path: [ ...triangleFill.path ]
157
157
  };
158
158
 
159
159
  const triangleFlip = {
160
+ connect: zero,
160
161
  offset: half,
161
162
  path: [ ...triangle.path ],
162
163
  dashPath: [ 1, -2.5, 0, 2, -1, 0 ]
163
164
  };
164
165
 
166
+ rotate(triangleFlipFill.path, 180, {
167
+ x: -1.5,
168
+ y: 0
169
+ });
170
+
165
171
  rotate(triangleFlip.path, 180, {
166
172
  x: -1.5,
167
173
  y: 0
168
174
  });
169
175
 
176
+ const arrowLinePath = [ 1, -3, 0, 2, -3.5, -1.8, 2, 0, 0, 2, -3.5, 1.8, 2, -3, 0 ];
177
+
178
+ const arrowFill = {
179
+ connect: zero,
180
+ offset: {
181
+ x: -1.1,
182
+ bevelJoin: .872,
183
+ roundJoin: .6
184
+ },
185
+ path: [ ...arrowLinePath ]
186
+ };
187
+
188
+ const arrow = draw.DataHelper.clone(arrowFill);
189
+
190
+ arrow.path = [ ...arrowLinePath, 1, -2.25, .95, 2, -2.25, -.95 ];
191
+
192
+ arrow.dashPath = [ 1, -3, 0, 2, -.5, 0 ];
193
+
170
194
  const circleLine = {
171
195
  connect: {
172
- x: -1.3
196
+ x: -1.8
173
197
  },
174
198
  path: [ 1, 1.8, -.1, 2, 1.8, 0, 26, 0, 0, 1.8, 0, 359, 0 ]
175
199
  };
176
200
 
177
- const circle = {
201
+ const circleFill = {
178
202
  connect: {
179
203
  x: .5
180
204
  },
181
- path: [ ...circleLine.path, 1, 0, 0, 26, 0, 0, 1, 0, 360, 0 ],
182
- dashPath: [ 1, -.5, 0, 2, .5, 0 ]
205
+ path: [ ...circleLine.path ]
183
206
  };
184
207
 
208
+ const circle = draw.DataHelper.clone(circleFill);
209
+
210
+ circle.path = [ ...circleLine.path, 1, 0, 0, 26, 0, 0, 1, 0, 360, 0 ];
211
+
212
+ circle.dashPath = [ 1, -.5, 0, 2, .5, 0 ];
213
+
185
214
  const squareLine = {
186
215
  connect: {
187
- x: -1.3
216
+ x: -1.4
188
217
  },
189
218
  path: [ 1, -1.4, 0, 2, -1.4, -1.4, 2, 1.4, -1.4, 2, 1.4, 1.4, 2, -1.4, 1.4, 2, -1.4, 0 ]
190
219
  };
191
220
 
221
+ const squareFill = {
222
+ path: [ ...squareLine.path ]
223
+ };
224
+
192
225
  const square = {
193
226
  path: [ ...squareLine.path, 2, -1.4, -.49, 2, 1, -.49, 1, -1.4, .49, 2, 1, .49 ]
194
227
  };
195
228
 
196
229
  const diamondLine = draw.DataHelper.clone(squareLine);
197
230
 
231
+ diamondLine.connect.x = -1.9;
232
+
233
+ const diamondFill = draw.DataHelper.clone(squareFill);
234
+
198
235
  const diamond = draw.DataHelper.clone(square);
199
236
 
200
237
  rotate(diamondLine.path, 45);
201
238
 
239
+ rotate(diamondFill.path, 45);
240
+
202
241
  rotate(diamond.path, 45);
203
242
 
204
243
  const mark = {
@@ -221,19 +260,34 @@ const arrows = {
221
260
  mark: mark
222
261
  };
223
262
 
263
+ const fillArrows = {
264
+ triangle: triangleFill,
265
+ "triangle-flip": triangleFlipFill,
266
+ arrow: arrowFill,
267
+ circle: circleFill,
268
+ square: squareFill,
269
+ diamond: diamondFill
270
+ };
271
+
224
272
  function getArrowPath(ui, arrow, from, to, size, connectOffset, hasDashPattern) {
225
- let pathData, scale;
273
+ let pathData, scale = 1, rotation = 0, fill;
226
274
  const {strokeCap: strokeCap, strokeJoin: strokeJoin} = ui.__;
227
275
  if (draw.isObject(arrow)) {
228
276
  if (arrow.type) {
229
- scale = arrow.scale;
277
+ scale = arrow.scale || 1;
278
+ rotation = arrow.rotation || 0;
230
279
  pathData = arrows[arrow.type];
280
+ if (scale > 1) {
281
+ const fillData = fillArrows[arrow.type];
282
+ if (fillData) pathData = fillData, fill = true;
283
+ }
231
284
  } else pathData = arrow;
232
285
  } else {
233
286
  pathData = arrows[arrow];
234
287
  }
288
+ if (!fill) fill = pathData.fill;
235
289
  const {offset: offset, connect: connect, path: path, dashPath: dashPath} = pathData;
236
- let connectX = connect ? connect.x : 0;
290
+ let connectX = connect ? connect.x * scale : 0;
237
291
  let offsetX = offset ? offset.x : 0;
238
292
  const data = [ ...path ];
239
293
  if (hasDashPattern && dashPath) data.push(...dashPath);
@@ -241,28 +295,38 @@ function getArrowPath(ui, arrow, from, to, size, connectOffset, hasDashPattern)
241
295
  if (offset) {
242
296
  if (strokeJoin === "round" && offset.roundJoin) offsetX += offset.roundJoin; else if (strokeJoin === "bevel" && offset.bevelJoin) offsetX += offset.bevelJoin;
243
297
  }
244
- if (scale) layout(data, 0, 0, scale, scale);
298
+ if (scale !== 1) layout(data, 0, 0, scale, scale);
245
299
  if (offsetX) layout(data, offsetX, 0);
246
- layout(data, to.x, to.y, size, size, getAngle(from, to));
300
+ layout(data, to.x, to.y, size, size, getAngle(from, to) + rotation);
247
301
  connectOffset.x = (connectX + offsetX) * size;
248
- return data;
302
+ const arrowData = {
303
+ data: data
304
+ };
305
+ if (fill) arrowData.fill = fill;
306
+ return arrowData;
249
307
  }
250
308
 
251
309
  const {M: M, L: L, C: C, Q: Q, Z: Z, N: N, D: D, X: X, G: G, F: F, O: O, P: P, U: U} = draw.PathCommandMap;
252
310
 
253
- const {copy: copy, copyFrom: copyFrom, getDistancePoint: getDistancePoint} = draw.PointHelper;
311
+ const {copy: copy, copyFrom: copyFrom, getDistancePoint: getDistancePoint, isSame: isSame} = draw.PointHelper;
312
+
313
+ const {stintSet: stintSet} = draw.DataHelper;
254
314
 
255
315
  const connectPoint = {};
256
316
 
257
317
  const first = {}, second = {};
258
318
 
259
- const last = {}, now = {};
319
+ const old = {}, last = {}, now = {};
260
320
 
261
321
  const PathArrowModule = {
262
322
  list: arrows,
263
- addArrows(ui) {
264
- const {startArrow: startArrow, endArrow: endArrow, strokeWidth: strokeWidth, dashPattern: dashPattern, __pathForRender: data, cornerRadius: cornerRadius} = ui.__;
323
+ fillList: fillArrows,
324
+ addArrows(ui, updateCache) {
325
+ const uData = ui.__;
326
+ const {startArrow: startArrow, endArrow: endArrow, __strokeWidth: strokeWidth, dashPattern: dashPattern, __pathForRender: data, cornerRadius: cornerRadius} = uData;
265
327
  const clonePathForArrow = !cornerRadius;
328
+ if (!updateCache) uData.__strokeWidthCache = strokeWidth;
329
+ let startArrowPath, singleStartArrow, endArrowPath, singleEndArrow;
266
330
  let command, i = 0, len = data.length, count = 0, useStartArrow = startArrow && startArrow !== "none";
267
331
  while (i < len) {
268
332
  command = data[i];
@@ -270,6 +334,7 @@ const PathArrowModule = {
270
334
  case M:
271
335
  case L:
272
336
  if (count < 2 || i + 6 >= len) {
337
+ copy(old, now);
273
338
  copyFrom(now, data[i + 1], data[i + 2]);
274
339
  if (!count && useStartArrow) copy(first, now);
275
340
  }
@@ -277,12 +342,18 @@ const PathArrowModule = {
277
342
  break;
278
343
 
279
344
  case C:
280
- if (count === 1 || i + 7 >= len - 3) copyPoints(data, last, now, i + 3);
345
+ if (count === 1 || i + 7 >= len - 3) {
346
+ copyPoints(data, last, now, i + 3);
347
+ old.x = data[i + 1], old.y = data[i + 2];
348
+ }
281
349
  i += 7;
282
350
  break;
283
351
 
284
352
  case Q:
285
- if (count === 1 || i + 5 >= len - 3) copyPoints(data, last, now, i + 1);
353
+ if (count === 1 || i + 5 >= len - 3) {
354
+ copyPoints(data, last, now, i + 1);
355
+ copy(old, last);
356
+ }
286
357
  i += 5;
287
358
  break;
288
359
 
@@ -320,6 +391,7 @@ const PathArrowModule = {
320
391
  case U:
321
392
  if (count === 1 || i + 6 >= len - 3) {
322
393
  copyPoints(data, last, now, i + 1);
394
+ copy(old, last);
323
395
  if (i + 6 !== len) {
324
396
  now.x -= (now.x - last.x) / 10;
325
397
  now.y -= (now.y - last.y) / 10;
@@ -330,13 +402,13 @@ const PathArrowModule = {
330
402
  }
331
403
  count++;
332
404
  if (count === 1 && command !== M) return;
333
- if (count === 2 && useStartArrow) copy(second, command === L ? now : last);
405
+ if (count === 2 && useStartArrow) copy(second, command === L ? now : isSame(old, first) ? last : old);
334
406
  if (i === len) {
335
- const path = ui.__.__pathForRender = clonePathForArrow ? [ ...data ] : data;
336
- const pathForArrow = ui.__.__pathForArrow = [];
407
+ const path = uData.__pathForRender = clonePathForArrow ? [ ...data ] : data;
337
408
  if (useStartArrow) {
338
- const startArrowPath = getArrowPath(ui, startArrow, second, first, strokeWidth, connectPoint, !!dashPattern);
339
- dashPattern ? pathForArrow.push(...startArrowPath) : path.push(...startArrowPath);
409
+ startArrowPath = getArrowPath(ui, startArrow, second, first, strokeWidth, connectPoint, !!dashPattern);
410
+ singleStartArrow = startArrowPath.fill || dashPattern;
411
+ if (!singleStartArrow) path.push(...startArrowPath.data);
340
412
  if (connectPoint.x) {
341
413
  getDistancePoint(first, second, -connectPoint.x, true);
342
414
  path[1] = second.x;
@@ -344,8 +416,10 @@ const PathArrowModule = {
344
416
  }
345
417
  }
346
418
  if (endArrow && endArrow !== "none") {
347
- const endArrowPath = getArrowPath(ui, endArrow, last, now, strokeWidth, connectPoint, !!dashPattern);
348
- dashPattern ? pathForArrow.push(...endArrowPath) : path.push(...endArrowPath);
419
+ if (isSame(last, now)) copy(last, old);
420
+ endArrowPath = getArrowPath(ui, endArrow, last, now, strokeWidth, connectPoint, !!dashPattern);
421
+ singleEndArrow = endArrowPath.fill || dashPattern;
422
+ if (!singleEndArrow) path.push(...endArrowPath.data);
349
423
  if (connectPoint.x) {
350
424
  getDistancePoint(now, last, -connectPoint.x, true);
351
425
  let index;
@@ -372,10 +446,21 @@ const PathArrowModule = {
372
446
  } else {
373
447
  copy(last, now);
374
448
  }
449
+ stintSet(uData, "__startArrowPath", singleStartArrow && startArrowPath);
450
+ stintSet(uData, "__endArrowPath", singleEndArrow && endArrowPath);
451
+ }
452
+ },
453
+ updateArrow(ui) {
454
+ const data = ui.__;
455
+ if (data.strokeScaleFixed) {
456
+ if (data.__strokeWidthCache !== data.__strokeWidth) {
457
+ ui.__updateRenderPath(true);
458
+ }
375
459
  }
376
460
  },
377
- register(name, data) {
461
+ register(name, data, fillData) {
378
462
  this.list[name] = data;
463
+ if (fillData) this.fillList[name] = data;
379
464
  },
380
465
  get(name) {
381
466
  return this.list[name];
@@ -401,12 +486,26 @@ draw.UI.addAttr("endArrow", "none", arrowType);
401
486
  Object.assign(draw.PathArrow, PathArrowModule);
402
487
 
403
488
  Object.assign(draw.Paint, {
404
- strokeArrow(_stroke, ui, canvas, _renderOptions) {
405
- if (ui.__.dashPattern) {
489
+ strokeArrow(stroke, ui, canvas, _renderOptions) {
490
+ const {__startArrowPath: __startArrowPath, __endArrowPath: __endArrowPath, dashPattern: dashPattern} = ui.__;
491
+ if (dashPattern) canvas.dashPattern = null;
492
+ if (__startArrowPath) {
493
+ canvas.beginPath();
494
+ ui.__drawPathByData(canvas, __startArrowPath.data);
495
+ canvas.stroke();
496
+ if (__startArrowPath.fill) {
497
+ canvas.fillStyle = stroke;
498
+ canvas.fill();
499
+ }
500
+ }
501
+ if (__endArrowPath) {
406
502
  canvas.beginPath();
407
- ui.__drawPathByData(canvas, ui.__.__pathForArrow);
408
- canvas.dashPattern = null;
503
+ ui.__drawPathByData(canvas, __endArrowPath.data);
409
504
  canvas.stroke();
505
+ if (__endArrowPath.fill) {
506
+ canvas.fillStyle = stroke;
507
+ canvas.fill();
508
+ }
410
509
  }
411
510
  }
412
511
  });