polyline.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. var Shape = require('../shape');
  3. var Smooth = require('../util/smooth');
  4. var bbox = require('../util/bbox');
  5. var Polyline =
  6. /*#__PURE__*/
  7. function (_Shape) {
  8. _inheritsLoose(Polyline, _Shape);
  9. function Polyline() {
  10. return _Shape.apply(this, arguments) || this;
  11. }
  12. var _proto = Polyline.prototype;
  13. _proto._initProperties = function _initProperties() {
  14. _Shape.prototype._initProperties.call(this);
  15. this._attrs.canFill = true;
  16. this._attrs.canStroke = true;
  17. this._attrs.type = 'polyline';
  18. };
  19. _proto.getDefaultAttrs = function getDefaultAttrs() {
  20. return {
  21. points: null,
  22. lineWidth: 1,
  23. smooth: false
  24. };
  25. };
  26. _proto.createPath = function createPath(context) {
  27. var self = this;
  28. var attrs = self.get('attrs');
  29. var points = attrs.points,
  30. smooth = attrs.smooth;
  31. var filteredPoints = []; // filter the point which x or y is NaN
  32. for (var i = 0, len = points.length; i < len; i++) {
  33. var point = points[i];
  34. if (!isNaN(point.x) && !isNaN(point.y)) {
  35. filteredPoints.push(point);
  36. }
  37. }
  38. context.beginPath();
  39. if (filteredPoints.length) {
  40. context.moveTo(filteredPoints[0].x, filteredPoints[0].y);
  41. if (smooth) {
  42. var constaint = [[0, 0], [1, 1]];
  43. var sps = Smooth.smooth(filteredPoints, false, constaint);
  44. for (var _i = 0, n = sps.length; _i < n; _i++) {
  45. var sp = sps[_i];
  46. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  47. }
  48. } else {
  49. var _i2;
  50. var l;
  51. for (_i2 = 1, l = filteredPoints.length - 1; _i2 < l; _i2++) {
  52. context.lineTo(filteredPoints[_i2].x, filteredPoints[_i2].y);
  53. }
  54. context.lineTo(filteredPoints[l].x, filteredPoints[l].y);
  55. }
  56. }
  57. };
  58. _proto.calculateBox = function calculateBox() {
  59. var attrs = this.get('attrs');
  60. var points = attrs.points,
  61. smooth = attrs.smooth,
  62. lineWidth = attrs.lineWidth;
  63. if (smooth) {
  64. var newPoints = [];
  65. var constaint = [[0, 0], [1, 1]];
  66. var sps = Smooth.smooth(points, false, constaint);
  67. for (var i = 0, n = sps.length; i < n; i++) {
  68. var sp = sps[i];
  69. if (i === 0) {
  70. newPoints.push([points[0].x, points[0].y, sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  71. } else {
  72. var lastPoint = sps[i - 1];
  73. newPoints.push([lastPoint[5], lastPoint[6], sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  74. }
  75. }
  76. return bbox.getBBoxFromBezierGroup(newPoints, lineWidth);
  77. }
  78. return bbox.getBBoxFromPoints(points, lineWidth);
  79. };
  80. return Polyline;
  81. }(Shape);
  82. Shape.Polyline = Polyline;
  83. module.exports = Polyline;