rect.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Util = require('../../util/common');
  9. var Shape = require('../shape');
  10. var Rect = /*#__PURE__*/function (_Shape) {
  11. (0, _inheritsLoose2["default"])(Rect, _Shape);
  12. var _super = _createSuper(Rect);
  13. function Rect() {
  14. return _Shape.apply(this, arguments) || this;
  15. }
  16. var _proto = Rect.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 = 'rect';
  22. };
  23. _proto.getDefaultAttrs = function getDefaultAttrs() {
  24. return {
  25. x: 0,
  26. y: 0,
  27. width: 0,
  28. height: 0,
  29. radius: 0,
  30. lineWidth: 0
  31. };
  32. };
  33. _proto.createPath = function createPath(context) {
  34. var self = this;
  35. var attrs = self.get('attrs');
  36. var x = attrs.x,
  37. y = attrs.y,
  38. width = attrs.width,
  39. height = attrs.height;
  40. context.beginPath();
  41. var radius = attrs.radius;
  42. if (!radius || !(width * height)) {
  43. context.rect(x, y, width, height);
  44. } else {
  45. radius = Util.parsePadding(radius);
  46. context.moveTo(x + radius[0], y);
  47. context.lineTo(x + width - radius[1], y);
  48. context.arc(x + width - radius[1], y + radius[1], radius[1], -Math.PI / 2, 0, false);
  49. context.lineTo(x + width, y + height - radius[2]);
  50. context.arc(x + width - radius[2], y + height - radius[2], radius[2], 0, Math.PI / 2, false);
  51. context.lineTo(x + radius[3], y + height);
  52. context.arc(x + radius[3], y + height - radius[3], radius[3], Math.PI / 2, Math.PI, false);
  53. context.lineTo(x, y + radius[0]);
  54. context.arc(x + radius[0], y + radius[0], radius[0], Math.PI, Math.PI * 3 / 2, false);
  55. context.closePath();
  56. }
  57. };
  58. _proto.calculateBox = function calculateBox() {
  59. var attrs = this.get('attrs');
  60. var x = attrs.x,
  61. y = attrs.y,
  62. width = attrs.width,
  63. height = attrs.height;
  64. return {
  65. minX: x,
  66. minY: y,
  67. maxX: x + width,
  68. maxY: y + height
  69. };
  70. };
  71. return Rect;
  72. }(Shape);
  73. Shape.Rect = Rect;
  74. module.exports = Rect;