ClockItem.vue 6.7 KB

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