79d9f9050d3530be587c457245f4f4be7e922523a7d2cfdf60182e4416c430b858ecd4eb77a7c06a73737c2cb340e5ca6c8e4bd11fa9b226e2bd2930f78625 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import smoothBezier from './smoothBezier';
  2. import { VectorArray } from '../../core/vector';
  3. import PathProxy from '../../core/PathProxy';
  4. export function buildPath(
  5. ctx: CanvasRenderingContext2D | PathProxy,
  6. shape: {
  7. points: VectorArray[],
  8. smooth?: number
  9. smoothConstraint?: VectorArray[]
  10. },
  11. closePath: boolean
  12. ) {
  13. const smooth = shape.smooth;
  14. let points = shape.points;
  15. if (points && points.length >= 2) {
  16. if (smooth) {
  17. const controlPoints = smoothBezier(
  18. points, smooth, closePath, shape.smoothConstraint
  19. );
  20. ctx.moveTo(points[0][0], points[0][1]);
  21. const len = points.length;
  22. for (let i = 0; i < (closePath ? len : len - 1); i++) {
  23. const cp1 = controlPoints[i * 2];
  24. const cp2 = controlPoints[i * 2 + 1];
  25. const p = points[(i + 1) % len];
  26. ctx.bezierCurveTo(
  27. cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]
  28. );
  29. }
  30. }
  31. else {
  32. ctx.moveTo(points[0][0], points[0][1]);
  33. for (let i = 1, l = points.length; i < l; i++) {
  34. ctx.lineTo(points[i][0], points[i][1]);
  35. }
  36. }
  37. closePath && ctx.closePath();
  38. }
  39. }