polyline.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  4. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  5. var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
  6. function _createSuper(Derived) { return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
  7. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  8. var Shape = require('../shape');
  9. var Smooth = require('../util/smooth');
  10. var bbox = require('../util/bbox'); // filter the point which x or y is NaN
  11. function _filterPoints(points) {
  12. var filteredPoints = [];
  13. for (var i = 0, len = points.length; i < len; i++) {
  14. var point = points[i];
  15. if (!isNaN(point.x) && !isNaN(point.y)) {
  16. filteredPoints.push(point);
  17. }
  18. }
  19. return filteredPoints;
  20. }
  21. var Polyline = /*#__PURE__*/function (_Shape) {
  22. (0, _inheritsLoose2["default"])(Polyline, _Shape);
  23. var _super = _createSuper(Polyline);
  24. function Polyline() {
  25. return _Shape.apply(this, arguments) || this;
  26. }
  27. var _proto = Polyline.prototype;
  28. _proto._initProperties = function _initProperties() {
  29. _Shape.prototype._initProperties.call(this);
  30. this._attrs.canFill = true;
  31. this._attrs.canStroke = true;
  32. this._attrs.type = 'polyline';
  33. };
  34. _proto.getDefaultAttrs = function getDefaultAttrs() {
  35. return {
  36. points: null,
  37. lineWidth: 1,
  38. smooth: false
  39. };
  40. };
  41. _proto.createPath = function createPath(context) {
  42. var self = this;
  43. var attrs = self.get('attrs');
  44. var points = attrs.points,
  45. smooth = attrs.smooth;
  46. var filteredPoints = _filterPoints(points);
  47. context.beginPath();
  48. if (filteredPoints.length) {
  49. context.moveTo(filteredPoints[0].x, filteredPoints[0].y);
  50. if (smooth) {
  51. var constaint = [[0, 0], [1, 1]];
  52. var sps = Smooth.smooth(filteredPoints, false, constaint);
  53. for (var i = 0, n = sps.length; i < n; i++) {
  54. var sp = sps[i];
  55. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  56. }
  57. } else {
  58. var _i;
  59. var l;
  60. for (_i = 1, l = filteredPoints.length - 1; _i < l; _i++) {
  61. context.lineTo(filteredPoints[_i].x, filteredPoints[_i].y);
  62. }
  63. context.lineTo(filteredPoints[l].x, filteredPoints[l].y);
  64. }
  65. }
  66. };
  67. _proto.calculateBox = function calculateBox() {
  68. var attrs = this.get('attrs');
  69. var points = attrs.points,
  70. smooth = attrs.smooth,
  71. lineWidth = attrs.lineWidth;
  72. var filteredPoints = _filterPoints(points);
  73. if (smooth) {
  74. var newPoints = [];
  75. var constaint = [[0, 0], [1, 1]];
  76. var sps = Smooth.smooth(filteredPoints, false, constaint);
  77. for (var i = 0, n = sps.length; i < n; i++) {
  78. var sp = sps[i];
  79. if (i === 0) {
  80. newPoints.push([filteredPoints[0].x, filteredPoints[0].y, sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  81. } else {
  82. var lastPoint = sps[i - 1];
  83. newPoints.push([lastPoint[5], lastPoint[6], sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  84. }
  85. }
  86. return bbox.getBBoxFromBezierGroup(newPoints, lineWidth);
  87. }
  88. return bbox.getBBoxFromPoints(filteredPoints, lineWidth);
  89. };
  90. return Polyline;
  91. }(Shape);
  92. Shape.Polyline = Polyline;
  93. module.exports = Polyline;