index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <div id="_C-ColckBox">
  3. <canvas id="_C-Colck" :width="clockWidth" :height="clockHeight"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. // 名称
  9. name: "clock",
  10. // 数据
  11. data() {
  12. return {
  13. L: 0, //基础半径
  14. timer: null,
  15. clockWidth: 0,
  16. clockHeight: 0,
  17. };
  18. },
  19. props: {
  20. width: {
  21. type: Number,
  22. default: 430,
  23. },
  24. height: {
  25. type: Number,
  26. default: 430,
  27. },
  28. },
  29. created() {
  30. this.clockWidth = this.width;
  31. this.clockHeight = this.height;
  32. },
  33. mounted() {
  34. this.drawCoolClock();
  35. this.timer = setInterval(() => {
  36. this.drawCoolClock();
  37. }, 30);
  38. },
  39. unmounted() {
  40. clearInterval(this.timer);
  41. this.timer = null;
  42. },
  43. // 函数
  44. methods: {
  45. drawCoolClock() {
  46. const fixNum = 20;
  47. let canvas = document.getElementById("_C-Colck");
  48. let ctx = canvas.getContext("2d");
  49. let [x0, y0] = [this.clockWidth / 2, this.clockHeight / 2]; //获取圆心x,y
  50. this.L = this.clockWidth / 2 - 10 - fixNum; //设置圆半径
  51. let [hourMaxL, hourMinL] = [this.L, this.L - 28]; //获取时刻度首位距离x0,y0位置
  52. let [minMaxL, minMinL] = [this.L - 2, this.L - 15];
  53. let centerL = 15;
  54. let oDate = new Date(); //绑定当前时间
  55. let oHours = oDate.getHours();
  56. let oMin = oDate.getMinutes();
  57. let oSen = oDate.getSeconds();
  58. let oHoursValue = -90 + oHours * 30 + oMin / 2; //获取小时的角度
  59. let oMinValue = -90 + oMin * 6;
  60. let oSenValue = -90 + oSen * 6;
  61. ctx.clearRect(0, 0, canvas.width, canvas.height); //清除画布
  62. // this.drawHoursScale(ctx, x0, y0, 12, 9, hourMaxL, hourMinL); //绘制表盘的时钟刻度scale
  63. // this.drawHoursScale(ctx, x0, y0, 60, 3, minMaxL, minMinL); //绘制表盘分钟刻度
  64. // this.drawScaleNum(ctx, hourMinL - 25, x0, y0); //绘制12个表盘数字
  65. // this.drawTimeNeedle(ctx, x0, y0, 15, 135, oHoursValue); //绘制时针
  66. // this.drawTimeNeedle(ctx, x0, y0, 8, minMaxL - 15, oMinValue); //绘制分针
  67. // this.drawTimeNeedle(ctx, x0, y0, 3, minMaxL - 2, oSenValue, "#f3a829"); //绘制秒针
  68. // this.drawSenDotted(ctx, "#f3a829", x0, y0, oSenValue - 180, centerL); //绘制秒针尾部
  69. this.drawHoursScale(ctx, x0, y0, 12, 4, hourMaxL, hourMinL); //绘制表盘的时钟刻度scale
  70. this.drawHoursScale(ctx, x0, y0, 60, 2, minMaxL, minMinL); //绘制表盘分钟刻度
  71. this.drawScaleNum(ctx, hourMinL - 18, x0, y0); //绘制12个表盘数字
  72. this.drawTimeNeedle(
  73. ctx,
  74. x0,
  75. y0,
  76. 5,
  77. (minMaxL - 15 - fixNum) / 1.5,
  78. oHoursValue
  79. ); //绘制时针
  80. this.drawTimeNeedle(ctx, x0, y0, 5, minMaxL - 15 - fixNum, oMinValue); //绘制分针
  81. this.drawTimeNeedle(ctx, x0, y0, 2, minMaxL - 2, oSenValue, "#f3a829"); //绘制秒针
  82. this.drawSenDotted(ctx, "#f3a829", x0, y0, oSenValue - 180, 12); //绘制秒针尾部
  83. this.drawCircleCenter(ctx, x0, y0, "#f3a829", 10); //绘制圆心
  84. },
  85. drawHoursScale(ctx, x0, y0, scaleNum, scaleW, maxL, minL) {
  86. for (let i = 0; i < scaleNum; i++) {
  87. let angel = -90 + i * (360 / scaleNum); //角度
  88. let [x1, y1] = [
  89. x0 + Math.cos((angel * Math.PI) / 180) * maxL,
  90. y0 + Math.sin((angel * Math.PI) / 180) * maxL,
  91. ];
  92. let [x2, y2] = [
  93. x0 + Math.cos((angel * Math.PI) / 180) * minL,
  94. y0 + Math.sin((angel * Math.PI) / 180) * minL,
  95. ];
  96. ctx.save();
  97. ctx.beginPath();
  98. ctx.lineWidth = scaleW;
  99. ctx.lineCap = "round";
  100. ctx.moveTo(x1, y1);
  101. ctx.lineTo(x2, y2);
  102. ctx.stroke();
  103. ctx.closePath();
  104. ctx.restore();
  105. }
  106. },
  107. drawScaleNum(ctx, L, x0, y0) {
  108. for (let i = 0; i < 12; i++) {
  109. let angel = -90 + i * (360 / 12); //角度
  110. let [x, y] = [
  111. x0 + Math.cos((angel * Math.PI) / 180) * L,
  112. y0 + Math.sin((angel * Math.PI) / 180) * L,
  113. ];
  114. let num = i == 0 ? 12 : i;
  115. ctx.strokeStyle = "rgb(229,228,227)";
  116. ctx.save();
  117. ctx.beginPath();
  118. // ctx.font = "30px Arial";
  119. ctx.font = "27px Arial";
  120. ctx.textAlign = "center";
  121. ctx.textBaseline = "middle";
  122. ctx.fillText(num, x, y);
  123. ctx.closePath();
  124. ctx.restore();
  125. }
  126. const baseDate = new Date();
  127. const weekDay = [
  128. "星期日",
  129. "星期一",
  130. "星期二",
  131. "星期三",
  132. "星期四",
  133. "星期五",
  134. "星期六",
  135. ];
  136. ctx.fillStyle = "rgb(229,228,227)";
  137. ctx.save();
  138. ctx.beginPath();
  139. ctx.font = "15px Arial";
  140. ctx.textAlign = "center";
  141. ctx.textBaseline = "middle";
  142. ctx.fillText(baseDate.formatDate("yyyy-MM-dd"), 285, 10);
  143. ctx.fillText(baseDate.formatDate("hh:mm:ss"), 293, 30);
  144. ctx.fillText(weekDay[baseDate.getDay()], 300, 50);
  145. ctx.closePath();
  146. ctx.restore();
  147. },
  148. drawTimeNeedle(ctx, x0, y0, lineW, L, angel, color = "rgb(229,228,227)") {
  149. let [x, y] = [
  150. x0 + Math.cos((angel * Math.PI) / 180) * L,
  151. y0 + Math.sin((angel * Math.PI) / 180) * L,
  152. ];
  153. ctx.save();
  154. ctx.beginPath();
  155. ctx.strokeStyle = color;
  156. ctx.lineWidth = lineW;
  157. ctx.lineCap = "round";
  158. ctx.moveTo(x0, y0);
  159. ctx.lineTo(x, y);
  160. ctx.stroke();
  161. ctx.closePath();
  162. ctx.restore();
  163. },
  164. drawCircleCenter(ctx, x0, y0, color, L) {
  165. ctx.save();
  166. ctx.beginPath();
  167. ctx.shadowBlur = 4;
  168. ctx.shadowColor = "#000";
  169. ctx.fillStyle = color;
  170. ctx.arc(x0, y0, L, 0, (360 * Math.PI) / 180);
  171. ctx.fill();
  172. ctx.closePath();
  173. ctx.restore();
  174. },
  175. drawSenDotted(ctx, color, x0, y0, angel, L) {
  176. let [x, y] = [
  177. x0 + Math.cos((angel * Math.PI) / 180) * L,
  178. y0 + Math.sin((angel * Math.PI) / 180) * L,
  179. ];
  180. ctx.save();
  181. ctx.beginPath();
  182. ctx.fillStyle = color;
  183. ctx.arc(x, y, 4, 0, (360 * Math.PI) / 180);
  184. ctx.fill();
  185. ctx.closePath();
  186. ctx.restore();
  187. },
  188. },
  189. watch: {},
  190. };
  191. </script>
  192. <style lang="less" scoped>
  193. #_C-ColckBox {
  194. height: 100%;
  195. // background: radial-gradient(#f8f6f5, #f7f5f4, #e4e3e2, #b9b8b7);
  196. display: flex;
  197. align-items: center;
  198. justify-content: center;
  199. }
  200. </style>