displayGrid.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { Control } from ".";
  2. import { Measure } from "..";
  3. /** Class used to render a grid */
  4. export class DisplayGrid extends Control {
  5. private _cellWidth = 20;
  6. private _cellHeight = 20;
  7. private _minorLineTickness = 1;
  8. private _minorLineColor = "DarkGray";
  9. private _majorLineTickness = 2;
  10. private _majorLineColor = "White";
  11. private _majorLineFrequency = 5;
  12. private _background = "Black";
  13. /** Gets or sets background color (Black by default) */
  14. public get background(): string {
  15. return this._background;
  16. }
  17. public set background(value: string) {
  18. if (this._background === value) {
  19. return;
  20. }
  21. this._background = value;
  22. this._markAsDirty();
  23. }
  24. /** Gets or sets the width of each cell (20 by default) */
  25. public get cellWidth(): number {
  26. return this._cellWidth;
  27. }
  28. public set cellWidth(value: number) {
  29. this._cellWidth = value;
  30. this._markAsDirty();
  31. }
  32. /** Gets or sets the height of each cell (20 by default) */
  33. public get cellHeight(): number {
  34. return this._cellHeight;
  35. }
  36. public set cellHeight(value: number) {
  37. this._cellHeight = value;
  38. this._markAsDirty();
  39. }
  40. /** Gets or sets the tickness of minor lines (1 by default) */
  41. public get minorLineTickness(): number {
  42. return this._minorLineTickness;
  43. }
  44. public set minorLineTickness(value: number) {
  45. this._minorLineTickness = value;
  46. this._markAsDirty();
  47. }
  48. /** Gets or sets the color of minor lines (DarkGray by default) */
  49. public get minorLineColor(): string {
  50. return this._minorLineColor;
  51. }
  52. public set minorLineColor(value: string) {
  53. this._minorLineColor = value;
  54. this._markAsDirty();
  55. }
  56. /** Gets or sets the tickness of major lines (2 by default) */
  57. public get majorLineTickness(): number {
  58. return this._majorLineTickness;
  59. }
  60. public set majorLineTickness(value: number) {
  61. this._majorLineTickness = value;
  62. this._markAsDirty();
  63. }
  64. /** Gets or sets the color of major lines (White by default) */
  65. public get majorLineColor(): string {
  66. return this._majorLineColor;
  67. }
  68. public set majorLineColor(value: string) {
  69. this._majorLineColor = value;
  70. this._markAsDirty();
  71. }
  72. /** Gets or sets the frequency of major lines (default is 1 every 5 minor lines)*/
  73. public get majorLineFrequency(): number {
  74. return this._majorLineFrequency;
  75. }
  76. public set majorLineFrequency(value: number) {
  77. this._majorLineFrequency = value;
  78. this._markAsDirty();
  79. }
  80. /**
  81. * Creates a new GridDisplayRectangle
  82. * @param name defines the control name
  83. */
  84. constructor(public name?: string) {
  85. super(name);
  86. }
  87. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  88. context.save();
  89. this._applyStates(context);
  90. if (this._processMeasures(parentMeasure, context)) {
  91. if (this._background) {
  92. context.fillStyle = this._background;
  93. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  94. }
  95. let cellCountX = this._currentMeasure.width / this._cellWidth;
  96. let cellCountY = this._currentMeasure.height / this._cellHeight;
  97. // Minor lines
  98. context.strokeStyle = this._minorLineColor;
  99. context.lineWidth = this._minorLineTickness;
  100. const left = this._currentMeasure.left + this._currentMeasure.width / 2;
  101. for (var x = -cellCountX / 2; x < cellCountX / 2; x++) {
  102. const cellX = left + x * this.cellWidth;
  103. context.beginPath();
  104. context.moveTo(cellX, this._currentMeasure.top);
  105. context.lineTo(cellX, this._currentMeasure.top + this._currentMeasure.height);
  106. context.stroke();
  107. }
  108. const top = this._currentMeasure.top + this._currentMeasure.height / 2;
  109. for (var y = -cellCountY / 2; y < cellCountY / 2; y++) {
  110. const cellY = top + y * this.cellHeight;
  111. context.beginPath();
  112. context.moveTo(this._currentMeasure.left, cellY);
  113. context.lineTo(this._currentMeasure.left + this._currentMeasure.width, cellY);
  114. context.stroke();
  115. }
  116. // Major lines
  117. context.strokeStyle = this._majorLineColor;
  118. context.lineWidth = this._majorLineTickness;
  119. for (var x = -cellCountX / 2 + this._majorLineFrequency; x < cellCountX / 2; x += this._majorLineFrequency) {
  120. let cellX = left + x * this.cellWidth;
  121. context.beginPath();
  122. context.moveTo(cellX, this._currentMeasure.top);
  123. context.lineTo(cellX, this._currentMeasure.top + this._currentMeasure.height);
  124. context.stroke();
  125. }
  126. for (var y = -cellCountY / 2 + this._majorLineFrequency; y < cellCountY / 2; y += this._majorLineFrequency) {
  127. let cellY = top + y * this.cellHeight;
  128. context.moveTo(this._currentMeasure.left, cellY);
  129. context.lineTo(this._currentMeasure.left + this._currentMeasure.width, cellY);
  130. context.closePath();
  131. context.stroke();
  132. }
  133. }
  134. context.restore();
  135. }
  136. protected _getTypeName(): string {
  137. return "DisplayGrid";
  138. }
  139. }