rect.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. var Util = require('../../util/common');
  3. var Shape = require('../shape');
  4. var Rect =
  5. /*#__PURE__*/
  6. function (_Shape) {
  7. _inheritsLoose(Rect, _Shape);
  8. function Rect() {
  9. return _Shape.apply(this, arguments) || this;
  10. }
  11. var _proto = Rect.prototype;
  12. _proto._initProperties = function _initProperties() {
  13. _Shape.prototype._initProperties.call(this);
  14. this._attrs.canFill = true;
  15. this._attrs.canStroke = true;
  16. this._attrs.type = 'rect';
  17. };
  18. _proto.getDefaultAttrs = function getDefaultAttrs() {
  19. return {
  20. x: 0,
  21. y: 0,
  22. width: 0,
  23. height: 0,
  24. radius: 0,
  25. lineWidth: 0
  26. };
  27. };
  28. _proto.createPath = function createPath(context) {
  29. var self = this;
  30. var attrs = self.get('attrs');
  31. var x = attrs.x,
  32. y = attrs.y,
  33. width = attrs.width,
  34. height = attrs.height;
  35. context.beginPath();
  36. var radius = attrs.radius;
  37. if (!radius || !(width * height)) {
  38. context.rect(x, y, width, height);
  39. } else {
  40. radius = Util.parsePadding(radius);
  41. context.moveTo(x + radius[0], y);
  42. context.lineTo(x + width - radius[1], y);
  43. context.arc(x + width - radius[1], y + radius[1], radius[1], -Math.PI / 2, 0, false);
  44. context.lineTo(x + width, y + height - radius[2]);
  45. context.arc(x + width - radius[2], y + height - radius[2], radius[2], 0, Math.PI / 2, false);
  46. context.lineTo(x + radius[3], y + height);
  47. context.arc(x + radius[3], y + height - radius[3], radius[3], Math.PI / 2, Math.PI, false);
  48. context.lineTo(x, y + radius[0]);
  49. context.arc(x + radius[0], y + radius[0], radius[0], Math.PI, Math.PI * 3 / 2, false);
  50. context.closePath();
  51. }
  52. };
  53. _proto.calculateBox = function calculateBox() {
  54. var attrs = this.get('attrs');
  55. var x = attrs.x,
  56. y = attrs.y,
  57. width = attrs.width,
  58. height = attrs.height;
  59. return {
  60. minX: x,
  61. minY: y,
  62. maxX: x + width,
  63. maxY: y + height
  64. };
  65. };
  66. return Rect;
  67. }(Shape);
  68. Shape.Rect = Rect;
  69. module.exports = Rect;