sector.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 bbox = require('../util/bbox');
  10. var Sector = /*#__PURE__*/function (_Shape) {
  11. (0, _inheritsLoose2["default"])(Sector, _Shape);
  12. var _super = _createSuper(Sector);
  13. function Sector() {
  14. return _Shape.apply(this, arguments) || this;
  15. }
  16. var _proto = Sector.prototype;
  17. _proto._initProperties = function _initProperties() {
  18. _Shape.prototype._initProperties.call(this);
  19. this._attrs.canFill = true;
  20. this._attrs.canStroke = true;
  21. this._attrs.type = 'sector';
  22. };
  23. _proto.getDefaultAttrs = function getDefaultAttrs() {
  24. return {
  25. x: 0,
  26. y: 0,
  27. lineWidth: 0,
  28. r: 0,
  29. r0: 0,
  30. startAngle: 0,
  31. endAngle: Math.PI * 2,
  32. anticlockwise: false
  33. };
  34. };
  35. _proto.createPath = function createPath(context) {
  36. var attrs = this.get('attrs');
  37. var x = attrs.x,
  38. y = attrs.y,
  39. startAngle = attrs.startAngle,
  40. endAngle = attrs.endAngle,
  41. r = attrs.r,
  42. r0 = attrs.r0,
  43. anticlockwise = attrs.anticlockwise;
  44. context.beginPath();
  45. var unitX = Math.cos(startAngle);
  46. var unitY = Math.sin(startAngle);
  47. context.moveTo(unitX * r0 + x, unitY * r0 + y);
  48. context.lineTo(unitX * r + x, unitY * r + y); // 当扇形的角度非常小的时候,就不进行弧线的绘制;或者整个只有1个扇形时,会出现end<0的情况不绘制
  49. if (Math.abs(endAngle - startAngle) > 0.0001 || startAngle === 0 && endAngle < 0) {
  50. context.arc(x, y, r, startAngle, endAngle, anticlockwise);
  51. context.lineTo(Math.cos(endAngle) * r0 + x, Math.sin(endAngle) * r0 + y);
  52. if (r0 !== 0) {
  53. context.arc(x, y, r0, endAngle, startAngle, !anticlockwise);
  54. }
  55. }
  56. context.closePath();
  57. };
  58. _proto.calculateBox = function calculateBox() {
  59. var attrs = this.get('attrs');
  60. var x = attrs.x,
  61. y = attrs.y,
  62. r = attrs.r,
  63. r0 = attrs.r0,
  64. startAngle = attrs.startAngle,
  65. endAngle = attrs.endAngle,
  66. anticlockwise = attrs.anticlockwise;
  67. var outerBBox = bbox.getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise);
  68. var innerBBox = bbox.getBBoxFromArc(x, y, r0, startAngle, endAngle, anticlockwise);
  69. return {
  70. minX: Math.min(outerBBox.minX, innerBBox.minX),
  71. minY: Math.min(outerBBox.minY, innerBBox.minY),
  72. maxX: Math.max(outerBBox.maxX, innerBBox.maxX),
  73. maxY: Math.max(outerBBox.maxY, innerBBox.maxY)
  74. };
  75. };
  76. return Sector;
  77. }(Shape);
  78. Shape.Sector = Sector;
  79. module.exports = Sector;