ellipse.ts 2.9 KB

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