ClockItem.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <canvas id="clockItem1" width="400" height="400"/>
  3. </template>
  4. <script lang="ts">
  5. import { SGraphItem, SGraphScene, SGraphView } from "@persagy-web/graph";
  6. import { Component, Vue } from "vue-property-decorator";
  7. import { SColor, SLineCapStyle, SPainter, SRect, SSize } from "@persagy-web/draw";
  8. /**
  9. * 时钟
  10. *
  11. * @author 郝洁 <haojie@persagy.com>
  12. */
  13. class SGraphClockItem extends SGraphItem {
  14. /** 大小 */
  15. size: SSize;
  16. /** 宽度 */
  17. get width() {
  18. return this.size.width;
  19. } // Get width
  20. set width(v: number) {
  21. this.size.width = v;
  22. } // Set width
  23. /** 高度 */
  24. get height() {
  25. return this.size.height;
  26. } // Get width
  27. set height(v: number) {
  28. this.size.height = v;
  29. } // Set width
  30. /** 半径 */
  31. get radius() {
  32. return Math.min(this.width, this.height) / 2.0;
  33. } // Get radius
  34. /**
  35. * 构造函数
  36. *
  37. * @param parent 指向父Item
  38. * @param size 大小
  39. */
  40. constructor(parent: SGraphItem | null, size: SSize);
  41. /**
  42. * 构造函数
  43. *
  44. * @param parent 指向父Item
  45. * @param width 宽度
  46. * @param height 高度
  47. */
  48. constructor(parent: SGraphItem | null, width: number, height: number);
  49. /**
  50. * 构造函数
  51. *
  52. * @param parent 指向父Item
  53. * @param width 宽度
  54. * @param height 高度
  55. */
  56. constructor(
  57. parent: SGraphItem | null,
  58. width: number | SSize,
  59. height?: number
  60. ) {
  61. super(parent);
  62. if (width instanceof SSize) {
  63. this.size = new SSize(width.width, width.height);
  64. } else {
  65. this.size = new SSize(width as number, height as number);
  66. }
  67. } // Constructor()
  68. /**
  69. * 对象边界区域
  70. *
  71. * @return 边界区域
  72. */
  73. boundingRect(): SRect {
  74. return new SRect(0, 0, this.width, this.height);
  75. } // Function SRect()
  76. /**
  77. * Item 绘制操作
  78. *
  79. * @param painter painter对象
  80. */
  81. onDraw(painter: SPainter): void {
  82. painter.translate(this.width / 2, this.height / 2);
  83. let t = new Date();
  84. this.drawScale(painter);
  85. this.drawHour(painter, t.getHours(), t.getMinutes(), t.getSeconds());
  86. this.drawMinute(painter, t.getMinutes(), t.getSeconds());
  87. this.drawSecond(painter, t.getSeconds() + t.getMilliseconds() / 1000.0);
  88. this.update()
  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);
  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);
  119. }
  120. } else {
  121. painter.rotate(30);
  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. );
  146. painter.drawLine(0, this.radius / 10.0, 0, -this.radius / 2.0);
  147. painter.restore();
  148. } // Function drawHour()
  149. /**
  150. * 绘制秒针
  151. *
  152. * @param painter painter对象
  153. * @param minute 分
  154. * @param second 秒
  155. */
  156. private drawMinute(
  157. painter: SPainter,
  158. minute: number,
  159. second: number
  160. ): void {
  161. painter.save();
  162. painter.pen.lineCapStyle = SLineCapStyle.Round;
  163. painter.pen.lineWidth = Math.max(this.radius / 40.0, 4.0);
  164. painter.rotate(minute * 6 + (second * 6) / 60.0);
  165. painter.drawLine(0, this.radius / 10.0, 0, (-this.radius * 2.0) / 3.0);
  166. painter.restore();
  167. } // Function drawMinute()
  168. /**
  169. * 绘制秒针
  170. *
  171. * @param painter painter对象
  172. * @param second 秒
  173. */
  174. private drawSecond(painter: SPainter, second: number): void {
  175. painter.save();
  176. painter.pen.lineCapStyle = SLineCapStyle.Round;
  177. painter.pen.lineWidth = Math.max(this.radius / 100.0, 3.0);
  178. painter.pen.color = SColor.Red;
  179. painter.rotate(second * 6);
  180. painter.drawLine(
  181. 0,
  182. this.radius / 5.0,
  183. 0,
  184. -this.radius + this.radius / 10.0
  185. );
  186. painter.restore();
  187. } // Function drawSecond()
  188. } // Class SGraphClockItem
  189. class TestView extends SGraphView {
  190. clock1 = new SGraphClockItem(null, 300, 300);
  191. /**
  192. * 构造函数
  193. */
  194. constructor() {
  195. super("clockItem1");
  196. this.scene = new SGraphScene();
  197. this.scene.addItem(this.clock1);
  198. }
  199. }
  200. @Component
  201. export default class ClockCanvas extends Vue {
  202. /**
  203. * 页面挂载
  204. */
  205. mounted(): void {
  206. new TestView();
  207. }
  208. }
  209. </script>
  210. <style scoped>
  211. </style>