gridDisplay.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Control } from ".";
  2. import { Measure } from "..";
  3. /** Class used to render a grid */
  4. export class GridDisplay extends Control {
  5. private _cellWidth = 10;
  6. private _cellHeight = 10;
  7. private _minorLineTickness = 1;
  8. private _minorLineColor = "DarkGray";
  9. private _background = "Black";
  10. /** Gets or sets background color (Black by default) */
  11. public get background(): string {
  12. return this._background;
  13. }
  14. public set background(value: string) {
  15. if (this._background === value) {
  16. return;
  17. }
  18. this._background = value;
  19. this._markAsDirty();
  20. }
  21. /** Gets or sets the width of each cell (10 by default) */
  22. public get cellWidth(): number {
  23. return this._cellWidth;
  24. }
  25. public set cellWidth(value: number) {
  26. this._cellWidth = value;
  27. this._markAsDirty();
  28. }
  29. /** Gets or sets the height of each cell (10 by default) */
  30. public get cellHeight(): number {
  31. return this._cellHeight;
  32. }
  33. public set cellHeight(value: number) {
  34. this._cellHeight = value;
  35. this._markAsDirty();
  36. }
  37. /** Gets or sets the tickness of minor lines (1 by default) */
  38. public get minorLineTickness(): number {
  39. return this._minorLineTickness;
  40. }
  41. public set minorLineTickness(value: number) {
  42. this._minorLineTickness = value;
  43. this._markAsDirty();
  44. }
  45. /** Gets or sets the color of minor lines (DarkGray by default) */
  46. public get minorLineColor(): string {
  47. return this._minorLineColor;
  48. }
  49. public set minorLineColor(value: string) {
  50. this._minorLineColor = value;
  51. this._markAsDirty();
  52. }
  53. /**
  54. * Creates a new GridDisplayRectangle
  55. * @param name defines the control name
  56. */
  57. constructor(public name?: string) {
  58. super(name);
  59. }
  60. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  61. context.save();
  62. this._applyStates(context);
  63. if (this._processMeasures(parentMeasure, context)) {
  64. context.fillStyle = this._background;
  65. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  66. const cellCountX = this._currentMeasure.width / this._cellWidth;
  67. const cellCountY = this._currentMeasure.height / this._cellHeight;
  68. context.strokeStyle = this._minorLineColor;
  69. context.lineWidth = this._minorLineTickness;
  70. const left = this._currentMeasure.left + this._currentMeasure.width / 2;
  71. for (var x = -cellCountX / 2; x < cellCountX / 2; x++) {
  72. const cellX = left + x * this.cellWidth;
  73. context.moveTo(cellX, this._currentMeasure.top);
  74. context.lineTo(cellX, this._currentMeasure.top + this._currentMeasure.height);
  75. context.stroke();
  76. }
  77. const top = this._currentMeasure.top + this._currentMeasure.height / 2;
  78. for (var y = -cellCountY / 2; y < cellCountY / y; x++) {
  79. const cellY = top + y * this.cellHeight;
  80. context.moveTo(this._currentMeasure.left, cellY);
  81. context.lineTo(this._currentMeasure.left + this._currentMeasure.width, cellY);
  82. context.stroke();
  83. }
  84. }
  85. context.restore();
  86. }
  87. protected _getTypeName(): string {
  88. return "GridDisplay";
  89. }
  90. }