var Line = require("./Line"); var polyHelper = require("../../utils/poly_util"); var dataUtil = require("../../utils/data_structure_util"); function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } /** * @class qrenderer.graphic.line.Polyline * Polyline. * * * 折线。 * @docauthor 大漠穷秋 <damoqiongqiu@126.com> */ var Polyline = /*#__PURE__*/ function (_Line) { _inherits(Polyline, _Line); /** * @method constructor Polyline * @param {Object} options */ function Polyline(options) { var _this; _classCallCheck(this, Polyline); _this = _possibleConstructorReturn(this, _getPrototypeOf(Polyline).call(this, dataUtil.merge({ shape: { points: null, smooth: false, smoothConstraint: null }, style: { stroke: '#000', fill: null } }, options, true))); /** * @property {String} type */ _this.type = 'polyline'; return _this; } /** * @method buildPath * Build the path of current line, the data structure is like the path attribute in SVG. * * * 构建当前线条的路径,数据结构类似 SVG 中的 path 属性。 * @param {Object} ctx * @param {String} shape */ _createClass(Polyline, [{ key: "buildPath", value: function buildPath(ctx, shape) { polyHelper.buildPath(ctx, shape, false); } /** * @method pointAt * Get point at percent, this value is between 0 and 1. * * * 按照比例获取线条上的点,取值范围在 0 到 1 之间。 * @param {Number} percent * @return {Array<Number>} */ }, { key: "pointAt", value: function pointAt(p) { var points = this.shape.points; if (!points || points.length <= 1) { return [0, 0]; } if (p <= 0.5) { return _toConsumableArray(points[0]); } else { return _toConsumableArray(points[points.length - 1]); } } }, { key: "firstPoint", value: function firstPoint() { return this.shape.points[0]; } }, { key: "firstTwoPoints", value: function firstTwoPoints() { return [_toConsumableArray(this.shape.points[0]), _toConsumableArray(this.shape.points[1])]; } }, { key: "lastPoint", value: function lastPoint() { return this.shape.points[this.shape.points.length - 1]; } }, { key: "lastTwoPoints", value: function lastTwoPoints() { var index1 = this.shape.points.length - 1; var index2 = this.shape.points.length - 2; return [_toConsumableArray(this.shape.points[index1]), _toConsumableArray(this.shape.points[index2])]; } }, { key: "setStartPoint", value: function setStartPoint(x, y) { this.shape.points[0] = [x, y]; } }, { key: "setEndPoint", value: function setEndPoint(x, y) { var index = this.shape.points.length - 1; this.shape.points[index] = [x, y]; } }]); return Polyline; }(Line); module.exports = Polyline;