poly.js 1.0 KB

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