rectangle.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Container } from "./container";
  2. import { Measure } from "../measure";
  3. /** Class used to create rectangle container */
  4. export class Rectangle extends Container {
  5. private _thickness = 1;
  6. private _cornerRadius = 0;
  7. /** Gets or sets border thickness */
  8. public get thickness(): number {
  9. return this._thickness;
  10. }
  11. public set thickness(value: number) {
  12. if (this._thickness === value) {
  13. return;
  14. }
  15. this._thickness = value;
  16. this._markAsDirty();
  17. }
  18. /** Gets or sets the corner radius angle */
  19. public get cornerRadius(): number {
  20. return this._cornerRadius;
  21. }
  22. public set cornerRadius(value: number) {
  23. if (value < 0) {
  24. value = 0;
  25. }
  26. if (this._cornerRadius === value) {
  27. return;
  28. }
  29. this._cornerRadius = value;
  30. this._markAsDirty();
  31. }
  32. /**
  33. * Creates a new Rectangle
  34. * @param name defines the control name
  35. */
  36. constructor(public name?: string) {
  37. super(name);
  38. }
  39. protected _getTypeName(): string {
  40. return "Rectangle";
  41. }
  42. protected _localDraw(context: CanvasRenderingContext2D): void {
  43. context.save();
  44. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  45. context.shadowColor = this.shadowColor;
  46. context.shadowBlur = this.shadowBlur;
  47. context.shadowOffsetX = this.shadowOffsetX;
  48. context.shadowOffsetY = this.shadowOffsetY;
  49. }
  50. if (this._background) {
  51. context.fillStyle = this._background;
  52. if (this._cornerRadius) {
  53. this._drawRoundedRect(context, this._thickness / 2);
  54. context.fill();
  55. } else {
  56. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  57. }
  58. }
  59. if (this._thickness) {
  60. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  61. context.shadowBlur = 0;
  62. context.shadowOffsetX = 0;
  63. context.shadowOffsetY = 0;
  64. }
  65. if (this.color) {
  66. context.strokeStyle = this.color;
  67. }
  68. context.lineWidth = this._thickness;
  69. if (this._cornerRadius) {
  70. this._drawRoundedRect(context, this._thickness / 2);
  71. context.stroke();
  72. } else {
  73. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  74. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  75. }
  76. }
  77. context.restore();
  78. }
  79. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  80. super._additionalProcessing(parentMeasure, context);
  81. this._measureForChildren.width -= 2 * this._thickness;
  82. this._measureForChildren.height -= 2 * this._thickness;
  83. this._measureForChildren.left += this._thickness;
  84. this._measureForChildren.top += this._thickness;
  85. }
  86. private _drawRoundedRect(context: CanvasRenderingContext2D, offset: number = 0): void {
  87. var x = this._currentMeasure.left + offset;
  88. var y = this._currentMeasure.top + offset;
  89. var width = this._currentMeasure.width - offset * 2;
  90. var height = this._currentMeasure.height - offset * 2;
  91. var radius = Math.min(height / 2 - 2, Math.min(width / 2 - 2, this._cornerRadius));
  92. context.beginPath();
  93. context.moveTo(x + radius, y);
  94. context.lineTo(x + width - radius, y);
  95. context.quadraticCurveTo(x + width, y, x + width, y + radius);
  96. context.lineTo(x + width, y + height - radius);
  97. context.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  98. context.lineTo(x + radius, y + height);
  99. context.quadraticCurveTo(x, y + height, x, y + height - radius);
  100. context.lineTo(x, y + radius);
  101. context.quadraticCurveTo(x, y, x + radius, y);
  102. context.closePath();
  103. }
  104. protected _clipForChildren(context: CanvasRenderingContext2D) {
  105. if (this._cornerRadius) {
  106. this._drawRoundedRect(context, this._thickness);
  107. context.clip();
  108. }
  109. }
  110. }