poly.js 979 B

12345678910111213141516171819202122232425
  1. import smoothBezier from './smoothBezier.js';
  2. export function buildPath(ctx, shape, closePath) {
  3. var smooth = shape.smooth;
  4. var points = shape.points;
  5. if (points && points.length >= 2) {
  6. if (smooth) {
  7. var controlPoints = smoothBezier(points, smooth, closePath, shape.smoothConstraint);
  8. ctx.moveTo(points[0][0], points[0][1]);
  9. var len = points.length;
  10. for (var i = 0; i < (closePath ? len : len - 1); i++) {
  11. var cp1 = controlPoints[i * 2];
  12. var cp2 = controlPoints[i * 2 + 1];
  13. var p = points[(i + 1) % len];
  14. ctx.bezierCurveTo(cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]);
  15. }
  16. }
  17. else {
  18. ctx.moveTo(points[0][0], points[0][1]);
  19. for (var i = 1, l = points.length; i < l; i++) {
  20. ctx.lineTo(points[i][0], points[i][1]);
  21. }
  22. }
  23. closePath && ctx.closePath();
  24. }
  25. }