SGraphClockItem.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {
  2. SColor,
  3. SLineCapStyle,
  4. SPainter,
  5. SRect,
  6. SSize
  7. } from "@saga-web/draw/lib";
  8. import { SGraphItem } from "../SGraphItem";
  9. /**
  10. * 线Item类
  11. *
  12. * @author 庞利祥(sybotan@126.com)
  13. */
  14. export class SGraphClockItem extends SGraphItem {
  15. /** 大小 */
  16. size: SSize;
  17. /** 宽度 */
  18. get width() {
  19. return this.size.width;
  20. } // Get width
  21. set width(v: number) {
  22. this.size.width = v;
  23. } // Set width
  24. /** 高度 */
  25. get height() {
  26. return this.size.height;
  27. } // Get width
  28. set height(v: number) {
  29. this.size.height = v;
  30. } // Set width
  31. /** 半径 */
  32. get radius() {
  33. return Math.min(this.width, this.height) / 2.0;
  34. } // Get radius
  35. /**
  36. * 构造函数
  37. *
  38. * @param parent 指向父Item
  39. * @param size 大小
  40. */
  41. constructor(parent: SGraphItem | null, size: SSize);
  42. /**
  43. * 构造函数
  44. *
  45. * @param parent 指向父Item
  46. * @param width 宽度
  47. * @param height 高度
  48. */
  49. constructor(parent: SGraphItem | null, width: number, height: number);
  50. /**
  51. * 构造函数
  52. *
  53. * @param parent 指向父Item
  54. * @param width 宽度
  55. * @param height 高度
  56. */
  57. constructor(
  58. parent: SGraphItem | null,
  59. width: number | SSize,
  60. height?: number
  61. ) {
  62. super(parent);
  63. if (width instanceof SSize) {
  64. this.size = new SSize(width.width, width.height);
  65. } else {
  66. this.size = new SSize(width as number, height as number);
  67. }
  68. } // Constructor()
  69. /**
  70. * 对象边界区域
  71. *
  72. * @return 边界区域
  73. */
  74. boundingRect(): SRect {
  75. return new SRect(0, 0, this.width, this.height);
  76. } // Function SRect()
  77. /**
  78. * Item绘制操作
  79. *
  80. * @param painter painter对象
  81. */
  82. onDraw(painter: SPainter): void {
  83. painter.translate(this.width / 2, this.height / 2);
  84. let t = new Date();
  85. this.drawScale(painter);
  86. this.drawHour(painter, t.getHours(), t.getMinutes(), t.getSeconds());
  87. this.drawMinute(painter, t.getMinutes(), t.getSeconds());
  88. this.drawSecond(painter, t.getSeconds() + t.getMilliseconds() / 1000.0);
  89. } // Function onDraw()
  90. /**
  91. * 绘制表刻度
  92. *
  93. * @param painter painter对象
  94. */
  95. private drawScale(painter: SPainter): void {
  96. let scaleLength = Math.max(this.radius / 10.0, 2.0);
  97. let scaleLength1 = scaleLength * 1.2;
  98. let strokeWidth = Math.max(this.radius / 100.0, 2.0);
  99. let strokeWidth1 = strokeWidth * 2.0;
  100. painter.save();
  101. painter.pen.color = SColor.Blue;
  102. for (let i = 1; i <= 12; i++) {
  103. // 12小时刻度
  104. painter.pen.lineWidth = strokeWidth1;
  105. painter.drawLine(0, -this.radius, 0, -this.radius + scaleLength1);
  106. if (this.radius >= 40) {
  107. // 如果半度大于40显示分钟刻度
  108. painter.rotate((6 * Math.PI) / 180);
  109. for (let j = 1; j <= 4; j++) {
  110. // 分钟刻度
  111. painter.pen.lineWidth = strokeWidth;
  112. painter.drawLine(
  113. 0,
  114. -this.radius,
  115. 0,
  116. -this.radius + scaleLength
  117. );
  118. painter.rotate((6 * Math.PI) / 180);
  119. }
  120. } else {
  121. painter.rotate((30 * Math.PI) / 180);
  122. }
  123. }
  124. painter.restore();
  125. } // Function drawScale()
  126. /**
  127. * 绘制时针
  128. *
  129. * @param painter painter对象
  130. * @param hour 时
  131. * @param minute 分
  132. * @param second 秒
  133. */
  134. private drawHour(
  135. painter: SPainter,
  136. hour: number,
  137. minute: number,
  138. second: number
  139. ): void {
  140. painter.save();
  141. painter.pen.lineCapStyle = SLineCapStyle.Round;
  142. painter.pen.lineWidth = Math.max(this.radius / 30.0, 4.0);
  143. painter.rotate(
  144. ((hour * 30.0 + (minute * 30.0) / 60 + (second * 30.0) / 3600) *
  145. Math.PI) /
  146. 180
  147. );
  148. painter.drawLine(0, this.radius / 10.0, 0, -this.radius / 2.0);
  149. painter.restore();
  150. } // Function drawHour()
  151. /**
  152. * 绘制秒针
  153. *
  154. * @param painter painter对象
  155. * @param minute 分
  156. * @param second 秒
  157. */
  158. private drawMinute(
  159. painter: SPainter,
  160. minute: number,
  161. second: number
  162. ): void {
  163. painter.save();
  164. painter.pen.lineCapStyle = SLineCapStyle.Round;
  165. painter.pen.lineWidth = Math.max(this.radius / 40.0, 4.0);
  166. painter.rotate(((minute * 6 + (second * 6) / 60.0) * Math.PI) / 180);
  167. painter.drawLine(0, this.radius / 10.0, 0, (-this.radius * 2.0) / 3.0);
  168. painter.restore();
  169. } // Function drawMinute()
  170. /**
  171. * 绘制秒针
  172. *
  173. * @param painter painter对象
  174. * @param second 秒
  175. */
  176. private drawSecond(painter: SPainter, second: number): void {
  177. painter.save();
  178. painter.pen.lineCapStyle = SLineCapStyle.Round;
  179. painter.pen.lineWidth = Math.max(this.radius / 100.0, 3.0);
  180. painter.pen.color = SColor.Red;
  181. painter.rotate((second * 6 * Math.PI) / 180);
  182. painter.drawLine(
  183. 0,
  184. this.radius / 5.0,
  185. 0,
  186. -this.radius + this.radius / 10.0
  187. );
  188. painter.restore();
  189. } // Function drawSecond()
  190. } // Class SGraphClockItem