@meta2d/core 1.0.6 → 1.0.8

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
@@ -170,10 +170,18 @@ function formatGradient(color) {
170
170
  }
171
171
  else {
172
172
  var _arr_2 = stap.split(' ');
173
- colors_1.push({
174
- color: _arr_2[0],
175
- i: parseFloat(_arr_2[1]) / 100,
176
- });
173
+ if (_arr_2.length > 2) {
174
+ colors_1.push({
175
+ color: _arr_2[1],
176
+ i: parseFloat(_arr_2[2]) / 100,
177
+ });
178
+ }
179
+ else {
180
+ colors_1.push({
181
+ color: _arr_2[0],
182
+ i: parseFloat(_arr_2[1]) / 100,
183
+ });
184
+ }
177
185
  }
178
186
  });
179
187
  return {
@@ -261,27 +269,222 @@ function drawLinearGradientLine(ctx, pen, points) {
261
269
  }
262
270
  function ctxDrawLinearGradientPath(ctx, pen) {
263
271
  var anchors = pen.calculative.worldAnchors;
272
+ var smoothLenth = pen.calculative.lineWidth * (pen.calculative.gradientSmooth || 0);
264
273
  for (var i = 0; i < anchors.length - 1; i++) {
265
274
  if ((pen.lineName === 'curve' || pen.lineName === 'mind') &&
266
275
  anchors[i].curvePoints) {
267
- drawLinearGradientLine(ctx, pen, [anchors[i], anchors[i].curvePoints[0]]);
268
- for (var j = 0; j < anchors[i].curvePoints.length - 1; j++) {
276
+ if (i > 0) {
277
+ var lastCurvePoints = anchors[i - 1].curvePoints;
278
+ if (lastCurvePoints) {
279
+ //上一个存在锚点
280
+ smoothTransition(ctx, pen, smoothLenth, lastCurvePoints[lastCurvePoints.length - 1], anchors[i], anchors[i].curvePoints[0]);
281
+ }
282
+ else {
283
+ smoothTransition(ctx, pen, smoothLenth, anchors[i - 1], anchors[i], anchors[i].curvePoints[0]);
284
+ }
285
+ //获取当前相对于0的位置
286
+ var next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i].curvePoints[0]);
287
+ drawLinearGradientLine(ctx, pen, [next, anchors[i].curvePoints[1]]);
288
+ }
289
+ else {
290
+ drawLinearGradientLine(ctx, pen, [
291
+ anchors[i],
292
+ anchors[i].curvePoints[0],
293
+ ]);
294
+ drawLinearGradientLine(ctx, pen, [
295
+ anchors[i].curvePoints[0],
296
+ anchors[i].curvePoints[1],
297
+ ]);
298
+ }
299
+ var len = anchors[i].curvePoints.length - 1;
300
+ for (var j = 1; j < len; j++) {
269
301
  drawLinearGradientLine(ctx, pen, [
270
302
  anchors[i].curvePoints[j],
271
303
  anchors[i].curvePoints[j + 1],
272
304
  ]);
273
305
  }
274
- var index = anchors[i].curvePoints.length - 1;
275
- drawLinearGradientLine(ctx, pen, [
276
- anchors[i].curvePoints[index],
277
- anchors[i + 1],
278
- ]);
306
+ var last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i].curvePoints[len]);
307
+ drawLinearGradientLine(ctx, pen, [anchors[i].curvePoints[len], last]);
279
308
  }
280
309
  else {
281
- drawLinearGradientLine(ctx, pen, [anchors[i], anchors[i + 1]]);
310
+ var _next = anchors[i];
311
+ var _last = anchors[i + 1];
312
+ if (i > 0 && i < anchors.length - 1) {
313
+ //有突兀的地方
314
+ var lastCurvePoints = anchors[i - 1].curvePoints;
315
+ if (lastCurvePoints) {
316
+ smoothTransition(ctx, pen, smoothLenth, lastCurvePoints[lastCurvePoints.length - 1], anchors[i], anchors[i + 1]);
317
+ }
318
+ else {
319
+ smoothTransition(ctx, pen, smoothLenth, anchors[i - 1], anchors[i], anchors[i + 1]);
320
+ }
321
+ }
322
+ if (i > 0 && i < anchors.length - 1) {
323
+ _next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i + 1]);
324
+ }
325
+ if (i < anchors.length - 2) {
326
+ _last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i]);
327
+ }
328
+ drawLinearGradientLine(ctx, pen, [_next, _last]);
282
329
  }
283
330
  }
284
331
  }
332
+ function getSmoothAdjacent(smoothLenth, p1, p2) {
333
+ var nexLength = Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
334
+ if (smoothLenth < nexLength) {
335
+ return {
336
+ x: p1.x + ((p2.x - p1.x) * smoothLenth) / nexLength,
337
+ y: p1.y + ((p2.y - p1.y) * smoothLenth) / nexLength,
338
+ };
339
+ }
340
+ else {
341
+ return {
342
+ x: p1.x + (p2.x - p1.x) / nexLength / 2,
343
+ y: p1.y + (p2.y - p1.y) / nexLength / 2,
344
+ };
345
+ }
346
+ }
347
+ function smoothTransition(ctx, pen, smoothLenth, p1, p2, p3) {
348
+ var last = getSmoothAdjacent(smoothLenth, p2, p1);
349
+ var next = getSmoothAdjacent(smoothLenth, p2, p3);
350
+ var contrlPoint = { x: p2.x, y: p2.y };
351
+ var points = getBezierPoints(100, last, contrlPoint, next);
352
+ for (var k = 0; k < points.length - 1; k++) {
353
+ drawLinearGradientLine(ctx, pen, [
354
+ {
355
+ x: points[k].x,
356
+ y: points[k].y,
357
+ },
358
+ {
359
+ x: points[k + 1].x,
360
+ y: points[k + 1].y,
361
+ },
362
+ ]);
363
+ }
364
+ }
365
+ function smoothAnimateTransition(ctx, smoothLenth, p2, p3) {
366
+ var next = getSmoothAdjacent(smoothLenth, p2, p3);
367
+ var contrlPoint = { x: p2.x, y: p2.y };
368
+ ctx.quadraticCurveTo(contrlPoint.x, contrlPoint.y, next.x, next.y);
369
+ }
370
+ function getGradientAnimatePath(pen) {
371
+ var anchors = pen.calculative.worldAnchors;
372
+ var smoothLenth = pen.calculative.lineWidth * (pen.calculative.gradientSmooth || 0);
373
+ //只创建一次
374
+ var _path = new Path2D();
375
+ for (var i = 0; i < anchors.length - 1; i++) {
376
+ var _next = anchors[i];
377
+ var _last = anchors[i + 1];
378
+ if (i == 0) {
379
+ _path.moveTo(anchors[i].x, anchors[i].y);
380
+ }
381
+ if (i > 0 && i < anchors.length - 1) {
382
+ //有突兀的地方
383
+ var lastCurvePoints = anchors[i - 1].curvePoints;
384
+ // const path = new Path2D();
385
+ if (lastCurvePoints) {
386
+ smoothAnimateTransition(_path, smoothLenth, anchors[i], anchors[i + 1]);
387
+ }
388
+ else {
389
+ smoothAnimateTransition(_path, smoothLenth, anchors[i], anchors[i + 1]);
390
+ }
391
+ }
392
+ if (i > 0 && i < anchors.length - 1) {
393
+ _next = getSmoothAdjacent(smoothLenth, anchors[i], anchors[i + 1]);
394
+ }
395
+ if (i < anchors.length - 2) {
396
+ _last = getSmoothAdjacent(smoothLenth, anchors[i + 1], anchors[i]);
397
+ }
398
+ _path.lineTo(_last.x, _last.y);
399
+ }
400
+ return _path;
401
+ }
402
+ function getAngle(p1, p2, p3) {
403
+ var a = { x: 0, y: 0 }, b = { x: 0, y: 0 };
404
+ a.x = p1.x - p2.x;
405
+ a.y = p1.y - p2.y;
406
+ b.x = p3.x - p2.x;
407
+ b.y = p3.y - p2.y;
408
+ return ((Math.acos((a.x * b.x + a.y * b.y) /
409
+ (Math.sqrt(a.x * a.x + a.y * a.y) * Math.sqrt(b.x * b.x + b.y * b.y))) /
410
+ Math.PI) *
411
+ 180);
412
+ }
413
+ function getBezierPoints(num, p1, p2, p3, p4) {
414
+ if (num === void 0) { num = 100; }
415
+ var func = null;
416
+ var points = [];
417
+ if (!p3 && !p4) {
418
+ func = oneBezier;
419
+ }
420
+ else if (p3 && !p4) {
421
+ func = twoBezier;
422
+ }
423
+ else if (p3 && p4) {
424
+ func = threeBezier;
425
+ }
426
+ for (var i = 0; i < num; i++) {
427
+ points.push(func(i / num, p1, p2, p3, p4));
428
+ }
429
+ if (p4) {
430
+ points.push(p4);
431
+ }
432
+ else if (p3) {
433
+ points.push(p3);
434
+ }
435
+ return points;
436
+ }
437
+ /**
438
+ * @desc 一阶贝塞尔
439
+ * @param t 当前百分比
440
+ * @param p1 起点坐标
441
+ * @param p2 终点坐标
442
+ */
443
+ function oneBezier(t, p1, p2) {
444
+ var x1 = p1.x, y1 = p1.y;
445
+ var x2 = p2.x, y2 = p2.y;
446
+ var x = x1 + (x2 - x1) * t;
447
+ var y = y1 + (y2 - y1) * t;
448
+ return { x: x, y: y };
449
+ }
450
+ /**
451
+ * @desc 二阶贝塞尔
452
+ * @param t 当前百分比
453
+ * @param p1 起点坐标
454
+ * @param p2 终点坐标
455
+ * @param cp 控制点
456
+ */
457
+ function twoBezier(t, p1, cp, p2) {
458
+ var x1 = p1.x, y1 = p1.y;
459
+ var cx = cp.x, cy = cp.y;
460
+ var x2 = p2.x, y2 = p2.y;
461
+ var x = (1 - t) * (1 - t) * x1 + 2 * t * (1 - t) * cx + t * t * x2;
462
+ var y = (1 - t) * (1 - t) * y1 + 2 * t * (1 - t) * cy + t * t * y2;
463
+ return { x: x, y: y };
464
+ }
465
+ /**
466
+ * @desc 三阶贝塞尔
467
+ * @param t 当前百分比
468
+ * @param p1 起点坐标
469
+ * @param p2 终点坐标
470
+ * @param cp1 控制点1
471
+ * @param cp2 控制点2
472
+ */
473
+ function threeBezier(t, p1, cp1, cp2, p2) {
474
+ var x1 = p1.x, y1 = p1.y;
475
+ var x2 = p2.x, y2 = p2.y;
476
+ var cx1 = cp1.x, cy1 = cp1.y;
477
+ var cx2 = cp2.x, cy2 = cp2.y;
478
+ var x = x1 * (1 - t) * (1 - t) * (1 - t) +
479
+ 3 * cx1 * t * (1 - t) * (1 - t) +
480
+ 3 * cx2 * t * t * (1 - t) +
481
+ x2 * t * t * t;
482
+ var y = y1 * (1 - t) * (1 - t) * (1 - t) +
483
+ 3 * cy1 * t * (1 - t) * (1 - t) +
484
+ 3 * cy2 * t * t * (1 - t) +
485
+ y2 * t * t * t;
486
+ return { x: x, y: y };
487
+ }
285
488
  function strokeLinearGradient(ctx, pen) {
286
489
  var _a = pen.calculative, worldRect = _a.worldRect, lineGradientFromColor = _a.lineGradientFromColor, lineGradientToColor = _a.lineGradientToColor, lineGradientAngle = _a.lineGradientAngle;
287
490
  return linearGradient(ctx, worldRect, lineGradientFromColor, lineGradientToColor, lineGradientAngle);
@@ -712,7 +915,7 @@ export function renderPen(ctx, pen) {
712
915
  }
713
916
  }
714
917
  else {
715
- stroke = pen.calculative.color;
918
+ stroke = pen.calculative.color || getGlobalColor(store);
716
919
  }
717
920
  ctx.strokeStyle = stroke;
718
921
  }
@@ -1055,8 +1258,19 @@ export function ctxDrawLinePath(canUsePath, ctx, pen, store) {
1055
1258
  if (pen.calculative.animatePos) {
1056
1259
  ctx.save();
1057
1260
  setCtxLineAnimate(ctx, pen, store);
1261
+ ctx.beginPath();
1058
1262
  if (path instanceof Path2D) {
1059
- ctx.stroke(path);
1263
+ //是否设置了平滑度
1264
+ if (pen.calculative.gradientSmooth &&
1265
+ (pen.lineName === 'polyline' || pen.lineName === 'line')) {
1266
+ if (!pen.calculative.gradientAnimatePath) {
1267
+ pen.calculative.gradientAnimatePath = getGradientAnimatePath(pen);
1268
+ }
1269
+ ctx.stroke(pen.calculative.gradientAnimatePath);
1270
+ }
1271
+ else {
1272
+ ctx.stroke(path);
1273
+ }
1060
1274
  }
1061
1275
  else {
1062
1276
  path(pen, ctx);
@@ -1314,6 +1528,7 @@ export function calcWorldAnchors(pen) {
1314
1528
  a.id === pen.calculative.activeAnchor.id;
1315
1529
  });
1316
1530
  }
1531
+ pen.calculative.gradientAnimatePath = undefined;
1317
1532
  }
1318
1533
  export function calcWorldPointOfPen(pen, pt) {
1319
1534
  var p = __assign({}, pt);