@meta2d/core 1.0.5 → 1.0.7

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/src/pen/render.js CHANGED
@@ -161,11 +161,20 @@ function formatGradient(color) {
161
161
  var _arr = arr[1].split('%,');
162
162
  var colors_1 = [];
163
163
  _arr.forEach(function (stap) {
164
- var _arr = stap.split(') ');
165
- colors_1.push({
166
- color: rgbaToHex(_arr[0] + ')'),
167
- i: parseFloat(_arr[1]) / 100,
168
- });
164
+ if (/rgba?/.test(stap)) {
165
+ var _arr_1 = stap.split(') ');
166
+ colors_1.push({
167
+ color: rgbaToHex(_arr_1[0] + ')'),
168
+ i: parseFloat(_arr_1[1]) / 100,
169
+ });
170
+ }
171
+ else {
172
+ var _arr_2 = stap.split(' ');
173
+ colors_1.push({
174
+ color: _arr_2[0],
175
+ i: parseFloat(_arr_2[1]) / 100,
176
+ });
177
+ }
169
178
  });
170
179
  return {
171
180
  angle: parseFloat(arr[0]),
@@ -252,27 +261,222 @@ function drawLinearGradientLine(ctx, pen, points) {
252
261
  }
253
262
  function ctxDrawLinearGradientPath(ctx, pen) {
254
263
  var anchors = pen.calculative.worldAnchors;
264
+ var smoothLenth = pen.calculative.lineWidth * (pen.calculative.gradientSmooth || 0);
255
265
  for (var i = 0; i < anchors.length - 1; i++) {
256
266
  if ((pen.lineName === 'curve' || pen.lineName === 'mind') &&
257
267
  anchors[i].curvePoints) {
258
- drawLinearGradientLine(ctx, pen, [anchors[i], anchors[i].curvePoints[0]]);
259
- for (var j = 0; j < anchors[i].curvePoints.length - 1; j++) {
268
+ if (i > 0) {
269
+ var lastCurvePoints = anchors[i - 1].curvePoints;
270
+ if (lastCurvePoints) {
271
+ //上一个存在锚点
272
+ smoothTransition(ctx, pen, smoothLenth, lastCurvePoints[lastCurvePoints.length - 1], anchors[i], anchors[i].curvePoints[0]);
273
+ }
274
+ else {
275
+ smoothTransition(ctx, pen, smoothLenth, anchors[i - 1], anchors[i], anchors[i].curvePoints[0]);
276
+ }
277
+ //获取当前相对于0的位置
278
+ var next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i].curvePoints[0]);
279
+ drawLinearGradientLine(ctx, pen, [next, anchors[i].curvePoints[1]]);
280
+ }
281
+ else {
282
+ drawLinearGradientLine(ctx, pen, [
283
+ anchors[i],
284
+ anchors[i].curvePoints[0],
285
+ ]);
286
+ drawLinearGradientLine(ctx, pen, [
287
+ anchors[i].curvePoints[0],
288
+ anchors[i].curvePoints[1],
289
+ ]);
290
+ }
291
+ var len = anchors[i].curvePoints.length - 1;
292
+ for (var j = 1; j < len; j++) {
260
293
  drawLinearGradientLine(ctx, pen, [
261
294
  anchors[i].curvePoints[j],
262
295
  anchors[i].curvePoints[j + 1],
263
296
  ]);
264
297
  }
265
- var index = anchors[i].curvePoints.length - 1;
266
- drawLinearGradientLine(ctx, pen, [
267
- anchors[i].curvePoints[index],
268
- anchors[i + 1],
269
- ]);
298
+ var last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i].curvePoints[len]);
299
+ drawLinearGradientLine(ctx, pen, [anchors[i].curvePoints[len], last]);
270
300
  }
271
301
  else {
272
- drawLinearGradientLine(ctx, pen, [anchors[i], anchors[i + 1]]);
302
+ var _next = anchors[i];
303
+ var _last = anchors[i + 1];
304
+ if (i > 0 && i < anchors.length - 1) {
305
+ //有突兀的地方
306
+ var lastCurvePoints = anchors[i - 1].curvePoints;
307
+ if (lastCurvePoints) {
308
+ smoothTransition(ctx, pen, smoothLenth, lastCurvePoints[lastCurvePoints.length - 1], anchors[i], anchors[i + 1]);
309
+ }
310
+ else {
311
+ smoothTransition(ctx, pen, smoothLenth, anchors[i - 1], anchors[i], anchors[i + 1]);
312
+ }
313
+ }
314
+ if (i > 0 && i < anchors.length - 1) {
315
+ _next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i + 1]);
316
+ }
317
+ if (i < anchors.length - 2) {
318
+ _last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i]);
319
+ }
320
+ drawLinearGradientLine(ctx, pen, [_next, _last]);
273
321
  }
274
322
  }
275
323
  }
324
+ function getSmoothAdjacent(smoothLenth, p1, p2) {
325
+ var nexLength = Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
326
+ if (smoothLenth < nexLength) {
327
+ return {
328
+ x: p1.x + ((p2.x - p1.x) * smoothLenth) / nexLength,
329
+ y: p1.y + ((p2.y - p1.y) * smoothLenth) / nexLength,
330
+ };
331
+ }
332
+ else {
333
+ return {
334
+ x: p1.x + (p2.x - p1.x) / nexLength / 2,
335
+ y: p1.y + (p2.y - p1.y) / nexLength / 2,
336
+ };
337
+ }
338
+ }
339
+ function smoothTransition(ctx, pen, smoothLenth, p1, p2, p3) {
340
+ var last = getSmoothAdjacent(smoothLenth, p2, p1);
341
+ var next = getSmoothAdjacent(smoothLenth, p2, p3);
342
+ var contrlPoint = { x: p2.x, y: p2.y };
343
+ var points = getBezierPoints(100, last, contrlPoint, next);
344
+ for (var k = 0; k < points.length - 1; k++) {
345
+ drawLinearGradientLine(ctx, pen, [
346
+ {
347
+ x: points[k].x,
348
+ y: points[k].y,
349
+ },
350
+ {
351
+ x: points[k + 1].x,
352
+ y: points[k + 1].y,
353
+ },
354
+ ]);
355
+ }
356
+ }
357
+ function smoothAnimateTransition(ctx, smoothLenth, p2, p3) {
358
+ var next = getSmoothAdjacent(smoothLenth, p2, p3);
359
+ var contrlPoint = { x: p2.x, y: p2.y };
360
+ ctx.quadraticCurveTo(contrlPoint.x, contrlPoint.y, next.x, next.y);
361
+ }
362
+ function getGradientAnimatePath(pen) {
363
+ var anchors = pen.calculative.worldAnchors;
364
+ var smoothLenth = pen.calculative.lineWidth * (pen.calculative.gradientSmooth || 0);
365
+ //只创建一次
366
+ var _path = new Path2D();
367
+ for (var i = 0; i < anchors.length - 1; i++) {
368
+ var _next = anchors[i];
369
+ var _last = anchors[i + 1];
370
+ if (i == 0) {
371
+ _path.moveTo(anchors[i].x, anchors[i].y);
372
+ }
373
+ if (i > 0 && i < anchors.length - 1) {
374
+ //有突兀的地方
375
+ var lastCurvePoints = anchors[i - 1].curvePoints;
376
+ // const path = new Path2D();
377
+ if (lastCurvePoints) {
378
+ smoothAnimateTransition(_path, smoothLenth, anchors[i], anchors[i + 1]);
379
+ }
380
+ else {
381
+ smoothAnimateTransition(_path, smoothLenth, anchors[i], anchors[i + 1]);
382
+ }
383
+ }
384
+ if (i > 0 && i < anchors.length - 1) {
385
+ _next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i + 1]);
386
+ }
387
+ if (i < anchors.length - 2) {
388
+ _last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i]);
389
+ }
390
+ _path.lineTo(_last.x, _last.y);
391
+ }
392
+ return _path;
393
+ }
394
+ function getAngle(p1, p2, p3) {
395
+ var a = { x: 0, y: 0 }, b = { x: 0, y: 0 };
396
+ a.x = p1.x - p2.x;
397
+ a.y = p1.y - p2.y;
398
+ b.x = p3.x - p2.x;
399
+ b.y = p3.y - p2.y;
400
+ return ((Math.acos((a.x * b.x + a.y * b.y) /
401
+ (Math.sqrt(a.x * a.x + a.y * a.y) * Math.sqrt(b.x * b.x + b.y * b.y))) /
402
+ Math.PI) *
403
+ 180);
404
+ }
405
+ function getBezierPoints(num, p1, p2, p3, p4) {
406
+ if (num === void 0) { num = 100; }
407
+ var func = null;
408
+ var points = [];
409
+ if (!p3 && !p4) {
410
+ func = oneBezier;
411
+ }
412
+ else if (p3 && !p4) {
413
+ func = twoBezier;
414
+ }
415
+ else if (p3 && p4) {
416
+ func = threeBezier;
417
+ }
418
+ for (var i = 0; i < num; i++) {
419
+ points.push(func(i / num, p1, p2, p3, p4));
420
+ }
421
+ if (p4) {
422
+ points.push(p4);
423
+ }
424
+ else if (p3) {
425
+ points.push(p3);
426
+ }
427
+ return points;
428
+ }
429
+ /**
430
+ * @desc 一阶贝塞尔
431
+ * @param t 当前百分比
432
+ * @param p1 起点坐标
433
+ * @param p2 终点坐标
434
+ */
435
+ function oneBezier(t, p1, p2) {
436
+ var x1 = p1.x, y1 = p1.y;
437
+ var x2 = p2.x, y2 = p2.y;
438
+ var x = x1 + (x2 - x1) * t;
439
+ var y = y1 + (y2 - y1) * t;
440
+ return { x: x, y: y };
441
+ }
442
+ /**
443
+ * @desc 二阶贝塞尔
444
+ * @param t 当前百分比
445
+ * @param p1 起点坐标
446
+ * @param p2 终点坐标
447
+ * @param cp 控制点
448
+ */
449
+ function twoBezier(t, p1, cp, p2) {
450
+ var x1 = p1.x, y1 = p1.y;
451
+ var cx = cp.x, cy = cp.y;
452
+ var x2 = p2.x, y2 = p2.y;
453
+ var x = (1 - t) * (1 - t) * x1 + 2 * t * (1 - t) * cx + t * t * x2;
454
+ var y = (1 - t) * (1 - t) * y1 + 2 * t * (1 - t) * cy + t * t * y2;
455
+ return { x: x, y: y };
456
+ }
457
+ /**
458
+ * @desc 三阶贝塞尔
459
+ * @param t 当前百分比
460
+ * @param p1 起点坐标
461
+ * @param p2 终点坐标
462
+ * @param cp1 控制点1
463
+ * @param cp2 控制点2
464
+ */
465
+ function threeBezier(t, p1, cp1, cp2, p2) {
466
+ var x1 = p1.x, y1 = p1.y;
467
+ var x2 = p2.x, y2 = p2.y;
468
+ var cx1 = cp1.x, cy1 = cp1.y;
469
+ var cx2 = cp2.x, cy2 = cp2.y;
470
+ var x = x1 * (1 - t) * (1 - t) * (1 - t) +
471
+ 3 * cx1 * t * (1 - t) * (1 - t) +
472
+ 3 * cx2 * t * t * (1 - t) +
473
+ x2 * t * t * t;
474
+ var y = y1 * (1 - t) * (1 - t) * (1 - t) +
475
+ 3 * cy1 * t * (1 - t) * (1 - t) +
476
+ 3 * cy2 * t * t * (1 - t) +
477
+ y2 * t * t * t;
478
+ return { x: x, y: y };
479
+ }
276
480
  function strokeLinearGradient(ctx, pen) {
277
481
  var _a = pen.calculative, worldRect = _a.worldRect, lineGradientFromColor = _a.lineGradientFromColor, lineGradientToColor = _a.lineGradientToColor, lineGradientAngle = _a.lineGradientAngle;
278
482
  return linearGradient(ctx, worldRect, lineGradientFromColor, lineGradientToColor, lineGradientAngle);
@@ -772,6 +976,7 @@ export function renderPen(ctx, pen) {
772
976
  }
773
977
  if (lineGradientFlag) {
774
978
  ctxDrawLinearGradientPath(ctx, pen);
979
+ ctxDrawLinePath(true, ctx, pen, store);
775
980
  }
776
981
  else {
777
982
  ctxDrawPath(true, ctx, pen, store, fill);
@@ -1032,11 +1237,55 @@ export function ctxDrawPath(canUsePath, ctx, pen, store, fill) {
1032
1237
  }
1033
1238
  }
1034
1239
  }
1240
+ /**
1241
+ * 连线配置线条渐进后,动画效果、起始点、终点的绘制
1242
+ */
1243
+ export function ctxDrawLinePath(canUsePath, ctx, pen, store) {
1244
+ if (canUsePath === void 0) { canUsePath = true; }
1245
+ var path = canUsePath
1246
+ ? store.path2dMap.get(pen)
1247
+ : globalStore.path2dDraws[pen.name];
1248
+ if (path) {
1249
+ if (pen.type) {
1250
+ if (pen.calculative.animatePos) {
1251
+ ctx.save();
1252
+ setCtxLineAnimate(ctx, pen, store);
1253
+ ctx.beginPath();
1254
+ if (path instanceof Path2D) {
1255
+ //是否设置了平滑度
1256
+ if (pen.calculative.gradientSmooth &&
1257
+ (pen.lineName === 'polyline' || pen.lineName === 'line')) {
1258
+ if (!pen.calculative.gradientAnimatePath) {
1259
+ pen.calculative.gradientAnimatePath = getGradientAnimatePath(pen);
1260
+ }
1261
+ ctx.stroke(pen.calculative.gradientAnimatePath);
1262
+ }
1263
+ else {
1264
+ ctx.stroke(path);
1265
+ }
1266
+ }
1267
+ else {
1268
+ path(pen, ctx);
1269
+ ctx.stroke();
1270
+ }
1271
+ ctx.restore();
1272
+ }
1273
+ pen.fromArrow && renderFromArrow(ctx, pen, store);
1274
+ pen.toArrow && renderToArrow(ctx, pen, store);
1275
+ //TODO 锚点处渐进色的过渡
1276
+ if (pen.calculative.active && !pen.calculative.pencil) {
1277
+ renderLineAnchors(ctx, pen);
1278
+ }
1279
+ }
1280
+ }
1281
+ }
1035
1282
  /**
1036
1283
  * 设置线条动画,ctx 的 strokeStyle lineDash 等属性更改
1037
1284
  */
1038
1285
  export function setCtxLineAnimate(ctx, pen, store) {
1039
1286
  ctx.strokeStyle = pen.animateColor || store.options.animateColor;
1287
+ pen.calculative.animateLineWidth &&
1288
+ (ctx.lineWidth = pen.calculative.animateLineWidth * store.data.scale);
1040
1289
  var len = 0;
1041
1290
  switch (pen.lineAnimateType) {
1042
1291
  case LineAnimateType.Beads:
@@ -1066,7 +1315,8 @@ export function setCtxLineAnimate(ctx, pen, store) {
1066
1315
  if (len < 6) {
1067
1316
  len = 6;
1068
1317
  }
1069
- ctx.lineWidth = len;
1318
+ ctx.lineWidth =
1319
+ (pen.calculative.animateLineWidth || len) * store.data.scale;
1070
1320
  ctx.setLineDash([0.1, pen.length]);
1071
1321
  break;
1072
1322
  default:
@@ -1270,6 +1520,7 @@ export function calcWorldAnchors(pen) {
1270
1520
  a.id === pen.calculative.activeAnchor.id;
1271
1521
  });
1272
1522
  }
1523
+ pen.calculative.gradientAnimatePath = undefined;
1273
1524
  }
1274
1525
  export function calcWorldPointOfPen(pen, pt) {
1275
1526
  var p = __assign({}, pt);