Rect.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var Path = require("../Path");
  2. var roundRectHelper = require("../helper/roundRect");
  3. var _subPixelOptimize = require("../helper/subPixelOptimize");
  4. var subPixelOptimizeRect = _subPixelOptimize.subPixelOptimizeRect;
  5. /**
  6. * 矩形
  7. * @module zrender/graphic/shape/Rect
  8. */
  9. // Avoid create repeatly.
  10. var subPixelOptimizeOutputShape = {};
  11. var _default = Path.extend({
  12. type: 'rect',
  13. shape: {
  14. // 左上、右上、右下、左下角的半径依次为r1、r2、r3、r4
  15. // r缩写为1 相当于 [1, 1, 1, 1]
  16. // r缩写为[1] 相当于 [1, 1, 1, 1]
  17. // r缩写为[1, 2] 相当于 [1, 2, 1, 2]
  18. // r缩写为[1, 2, 3] 相当于 [1, 2, 3, 2]
  19. r: 0,
  20. x: 0,
  21. y: 0,
  22. width: 0,
  23. height: 0
  24. },
  25. buildPath: function (ctx, shape) {
  26. var x;
  27. var y;
  28. var width;
  29. var height;
  30. if (this.subPixelOptimize) {
  31. subPixelOptimizeRect(subPixelOptimizeOutputShape, shape, this.style);
  32. x = subPixelOptimizeOutputShape.x;
  33. y = subPixelOptimizeOutputShape.y;
  34. width = subPixelOptimizeOutputShape.width;
  35. height = subPixelOptimizeOutputShape.height;
  36. subPixelOptimizeOutputShape.r = shape.r;
  37. shape = subPixelOptimizeOutputShape;
  38. } else {
  39. x = shape.x;
  40. y = shape.y;
  41. width = shape.width;
  42. height = shape.height;
  43. }
  44. if (!shape.r) {
  45. ctx.rect(x, y, width, height);
  46. } else {
  47. roundRectHelper.buildPath(ctx, shape);
  48. }
  49. ctx.closePath();
  50. return;
  51. }
  52. });
  53. module.exports = _default;