ellipse.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Container } from "./container";
  2. import { Control } from "./control";
  3. import { Measure } from "../measure";
  4. /** Class used to create 2D ellipse containers */
  5. export class Ellipse extends Container {
  6. private _thickness = 1;
  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. /**
  19. * Creates a new Ellipse
  20. * @param name defines the control name
  21. */
  22. constructor(public name?: string) {
  23. super(name);
  24. }
  25. protected _getTypeName(): string {
  26. return "Ellipse";
  27. }
  28. protected _localDraw(context: CanvasRenderingContext2D): void {
  29. context.save();
  30. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  31. context.shadowColor = this.shadowColor;
  32. context.shadowBlur = this.shadowBlur;
  33. context.shadowOffsetX = this.shadowOffsetX;
  34. context.shadowOffsetY = this.shadowOffsetY;
  35. }
  36. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  37. this._currentMeasure.width / 2 - this._thickness / 2, this._currentMeasure.height / 2 - this._thickness / 2, context);
  38. if (this._background) {
  39. context.fillStyle = this._background;
  40. context.fill();
  41. }
  42. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  43. context.shadowBlur = 0;
  44. context.shadowOffsetX = 0;
  45. context.shadowOffsetY = 0;
  46. }
  47. if (this._thickness) {
  48. if (this.color) {
  49. context.strokeStyle = this.color;
  50. }
  51. context.lineWidth = this._thickness;
  52. context.stroke();
  53. }
  54. context.restore();
  55. }
  56. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  57. super._additionalProcessing(parentMeasure, context);
  58. this._measureForChildren.width -= 2 * this._thickness;
  59. this._measureForChildren.height -= 2 * this._thickness;
  60. this._measureForChildren.left += this._thickness;
  61. this._measureForChildren.top += this._thickness;
  62. }
  63. protected _clipForChildren(context: CanvasRenderingContext2D) {
  64. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2, this._currentMeasure.width / 2, this._currentMeasure.height / 2, context);
  65. context.clip();
  66. }
  67. }