5f024cd2631fcbb21d2fbdcef028578182fa4695da8f8f336381971336fad836bd5eb305ce6310ffd59fe8f9fd38cfcb98e2df5f5c19a175ab0a0e3298ce0d 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * 矩形
  3. * @module zrender/graphic/shape/Rect
  4. */
  5. import Path, { PathProps } from '../Path';
  6. import * as roundRectHelper from '../helper/roundRect';
  7. import {subPixelOptimizeRect} from '../helper/subPixelOptimize';
  8. export class RectShape {
  9. // 左上、右上、右下、左下角的半径依次为r1、r2、r3、r4
  10. // r缩写为1 相当于 [1, 1, 1, 1]
  11. // r缩写为[1] 相当于 [1, 1, 1, 1]
  12. // r缩写为[1, 2] 相当于 [1, 2, 1, 2]
  13. // r缩写为[1, 2, 3] 相当于 [1, 2, 3, 2]
  14. r?: number | number[]
  15. x = 0
  16. y = 0
  17. width = 0
  18. height = 0
  19. }
  20. export interface RectProps extends PathProps {
  21. shape?: Partial<RectShape>
  22. }
  23. // Avoid create repeatly.
  24. const subPixelOptimizeOutputShape = {};
  25. class Rect extends Path<RectProps> {
  26. shape: RectShape
  27. constructor(opts?: RectProps) {
  28. super(opts);
  29. }
  30. getDefaultShape() {
  31. return new RectShape();
  32. }
  33. buildPath(ctx: CanvasRenderingContext2D, shape: RectShape) {
  34. let x: number;
  35. let y: number;
  36. let width: number;
  37. let height: number;
  38. if (this.subPixelOptimize) {
  39. const optimizedShape = subPixelOptimizeRect(subPixelOptimizeOutputShape, shape, this.style);
  40. x = optimizedShape.x;
  41. y = optimizedShape.y;
  42. width = optimizedShape.width;
  43. height = optimizedShape.height;
  44. optimizedShape.r = shape.r;
  45. shape = optimizedShape;
  46. }
  47. else {
  48. x = shape.x;
  49. y = shape.y;
  50. width = shape.width;
  51. height = shape.height;
  52. }
  53. if (!shape.r) {
  54. ctx.rect(x, y, width, height);
  55. }
  56. else {
  57. roundRectHelper.buildPath(ctx, shape);
  58. }
  59. }
  60. isZeroArea() {
  61. return !this.shape.width || !this.shape.height;
  62. }
  63. }
  64. Rect.prototype.type = 'rect';
  65. export default Rect;