slider.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { BaseSlider } from "./baseSlider";
  2. /**
  3. * Class used to create slider controls
  4. */
  5. export class Slider extends BaseSlider {
  6. private _background = "black";
  7. private _borderColor = "white";
  8. private _isThumbCircle = false;
  9. protected _displayValueBar = true;
  10. /** Gets or sets a boolean indicating if the value bar must be rendered */
  11. public get displayValueBar(): boolean {
  12. return this._displayValueBar;
  13. }
  14. public set displayValueBar(value: boolean) {
  15. if (this._displayValueBar === value) {
  16. return;
  17. }
  18. this._displayValueBar = value;
  19. this._markAsDirty();
  20. }
  21. /** Gets or sets border color */
  22. public get borderColor(): string {
  23. return this._borderColor;
  24. }
  25. public set borderColor(value: string) {
  26. if (this._borderColor === value) {
  27. return;
  28. }
  29. this._borderColor = value;
  30. this._markAsDirty();
  31. }
  32. /** Gets or sets background color */
  33. public get background(): string {
  34. return this._background;
  35. }
  36. public set background(value: string) {
  37. if (this._background === value) {
  38. return;
  39. }
  40. this._background = value;
  41. this._markAsDirty();
  42. }
  43. /** Gets or sets a boolean indicating if the thumb should be round or square */
  44. public get isThumbCircle(): boolean {
  45. return this._isThumbCircle;
  46. }
  47. public set isThumbCircle(value: boolean) {
  48. if (this._isThumbCircle === value) {
  49. return;
  50. }
  51. this._isThumbCircle = value;
  52. this._markAsDirty();
  53. }
  54. /**
  55. * Creates a new Slider
  56. * @param name defines the control name
  57. */
  58. constructor(public name?: string) {
  59. super(name);
  60. }
  61. protected _getTypeName(): string {
  62. return "Slider";
  63. }
  64. public _draw(context: CanvasRenderingContext2D): void {
  65. context.save();
  66. this._applyStates(context);
  67. this._prepareRenderingData(this.isThumbCircle ? "circle" : "rectangle");
  68. var left = this._renderLeft;
  69. var top = this._renderTop;
  70. var width = this._renderWidth;
  71. var height = this._renderHeight;
  72. var radius = 0;
  73. if (this.isThumbClamped && this.isThumbCircle) {
  74. if (this.isVertical) {
  75. top += (this._effectiveThumbThickness / 2);
  76. }
  77. else {
  78. left += (this._effectiveThumbThickness / 2);
  79. }
  80. radius = this._backgroundBoxThickness / 2;
  81. }
  82. else {
  83. radius = (this._effectiveThumbThickness - this._effectiveBarOffset) / 2;
  84. }
  85. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  86. context.shadowColor = this.shadowColor;
  87. context.shadowBlur = this.shadowBlur;
  88. context.shadowOffsetX = this.shadowOffsetX;
  89. context.shadowOffsetY = this.shadowOffsetY;
  90. }
  91. const thumbPosition = this._getThumbPosition();
  92. context.fillStyle = this._background;
  93. if (this.isVertical) {
  94. if (this.isThumbClamped) {
  95. if (this.isThumbCircle) {
  96. context.beginPath();
  97. context.arc(left + this._backgroundBoxThickness / 2, top, radius, Math.PI, 2 * Math.PI);
  98. context.fill();
  99. context.fillRect(left, top, width, height);
  100. }
  101. else {
  102. context.fillRect(left, top, width, height + this._effectiveThumbThickness);
  103. }
  104. }
  105. else {
  106. context.fillRect(left, top, width, height);
  107. }
  108. }
  109. else {
  110. if (this.isThumbClamped) {
  111. if (this.isThumbCircle) {
  112. context.beginPath();
  113. context.arc(left + this._backgroundBoxLength, top + (this._backgroundBoxThickness / 2), radius, 0, 2 * Math.PI);
  114. context.fill();
  115. context.fillRect(left, top, width, height);
  116. }
  117. else {
  118. context.fillRect(left, top, width + this._effectiveThumbThickness, height);
  119. }
  120. }
  121. else {
  122. context.fillRect(left, top, width, height);
  123. }
  124. }
  125. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  126. context.shadowBlur = 0;
  127. context.shadowOffsetX = 0;
  128. context.shadowOffsetY = 0;
  129. }
  130. // Value bar
  131. context.fillStyle = this.color;
  132. if (this._displayValueBar) {
  133. if (this.isVertical) {
  134. if (this.isThumbClamped) {
  135. if (this.isThumbCircle) {
  136. context.beginPath();
  137. context.arc(left + this._backgroundBoxThickness / 2, top + this._backgroundBoxLength, radius, 0, 2 * Math.PI);
  138. context.fill();
  139. context.fillRect(left, top + thumbPosition, width, height - thumbPosition);
  140. }
  141. else {
  142. context.fillRect(left, top + thumbPosition, width, height - thumbPosition + this._effectiveThumbThickness);
  143. }
  144. }
  145. else {
  146. context.fillRect(left, top + thumbPosition, width, height - thumbPosition);
  147. }
  148. }
  149. else {
  150. if (this.isThumbClamped) {
  151. if (this.isThumbCircle) {
  152. context.beginPath();
  153. context.arc(left, top + this._backgroundBoxThickness / 2, radius, 0, 2 * Math.PI);
  154. context.fill();
  155. context.fillRect(left, top, thumbPosition, height);
  156. }
  157. else {
  158. context.fillRect(left, top, thumbPosition, height);
  159. }
  160. }
  161. else {
  162. context.fillRect(left, top, thumbPosition, height);
  163. }
  164. }
  165. }
  166. // Thumb
  167. if (this.displayThumb) {
  168. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  169. context.shadowColor = this.shadowColor;
  170. context.shadowBlur = this.shadowBlur;
  171. context.shadowOffsetX = this.shadowOffsetX;
  172. context.shadowOffsetY = this.shadowOffsetY;
  173. }
  174. if (this._isThumbCircle) {
  175. context.beginPath();
  176. if (this.isVertical) {
  177. context.arc(left + this._backgroundBoxThickness / 2, top + thumbPosition, radius, 0, 2 * Math.PI);
  178. }
  179. else {
  180. context.arc(left + thumbPosition, top + (this._backgroundBoxThickness / 2), radius, 0, 2 * Math.PI);
  181. }
  182. context.fill();
  183. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  184. context.shadowBlur = 0;
  185. context.shadowOffsetX = 0;
  186. context.shadowOffsetY = 0;
  187. }
  188. context.strokeStyle = this._borderColor;
  189. context.stroke();
  190. }
  191. else {
  192. if (this.isVertical) {
  193. context.fillRect(left - this._effectiveBarOffset, this._currentMeasure.top + thumbPosition, this._currentMeasure.width, this._effectiveThumbThickness);
  194. }
  195. else {
  196. context.fillRect(this._currentMeasure.left + thumbPosition, this._currentMeasure.top, this._effectiveThumbThickness, this._currentMeasure.height);
  197. }
  198. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  199. context.shadowBlur = 0;
  200. context.shadowOffsetX = 0;
  201. context.shadowOffsetY = 0;
  202. }
  203. context.strokeStyle = this._borderColor;
  204. if (this.isVertical) {
  205. context.strokeRect(left - this._effectiveBarOffset, this._currentMeasure.top + thumbPosition, this._currentMeasure.width, this._effectiveThumbThickness);
  206. }
  207. else {
  208. context.strokeRect(this._currentMeasure.left + thumbPosition, this._currentMeasure.top, this._effectiveThumbThickness, this._currentMeasure.height);
  209. }
  210. }
  211. }
  212. context.restore();
  213. }
  214. }