绘制微信小程序画布时钟

###这里将介绍下绘制微信小程序画布时钟 Index.js文件里面存放着程序的逻辑层数据,是程序的核心。

1)考虑到适应不同的手机大小,定义了两个变量width和height,当页面加载时(onLoad)获取系统窗口的值作为index页面的大小;

2)页面初次渲染后给index添加时钟函数canvasClock(),并设置定时器,每一秒刷新一次画布,呈现出时钟运动的效果

3)在时钟函数canvasClock()中进行绘制时钟页面所需的元素以其其运动函数,并对其进行调用drawClock(),通过调用小程序wx.drawCanvas方法来指定index为绘制页面和绘制行为

4)当程序退出时,清除计时器

上代码:1.javascript

// 获取应用实例
2.Page({

  1. data:{
  2. width:0,
  3. height:0
  4. },
  5. //onLoad生命周期函数,监听页面加载
  6. onLoad: function(){
  7. //将全局变量Index保存在that中,里面函数调用
  8. var that = this
  9. //获取系统信息
  10. wx.getSystemInfo({
  11. //获取系统信息成功,将系统窗口的宽高赋给页面的宽高
  12. success: function(res) {
  13. that.width = res.windowWidth
  14. that.height = res.windowHeight
  15. }
  16. })
  17. },
  18. //onReady生命周期函数,监听页面初次渲染完成
  19. onReady: function(){
  20. //调用canvasClock函数
  21. this.canvasClock()
  22. //对canvasClock函数循环调用
  23. this.interval = setInterval(this.canvasClock,1000)
  24. },
  25. canvasClock: function(){
  26. var context = wx.createContext()//创建并返回绘图上下文(获取画笔)
  27. //设置宽高
  28. var width = this.width
  29. var height = this.height
  30. var R = width/2-55;//设置文字距离时钟中心点距离
  31. //重置画布函数
  32. function reSet(){
  33. context.height = context.height;//每次清除画布,然后变化后的时间补上
  34. context.translate(width/2, height/2);//设置坐标轴原点
  35. context.save();//保存中点坐标1
  36. }
  37. //绘制中心圆和外面大圆
  38. function circle(){
  39. //外面大圆
  40. context.setLineWidth(2);
  41. context.beginPath();
  42. context.arc(0, 0, width/2-30, 0, 2 * Math.PI,true);
  43. context.closePath();
  44. context.stroke();
  45. //中心圆
  46. context.beginPath();
  47. context.arc(0, 0, 8, 0, 2 * Math.PI, true);
  48. context.closePath();
  49. context.stroke();
  50. }
  51. //绘制字体
  52. function num(){
  53. // var R = width/2-60;//设置文字距离时钟中心点距离
  54. context.setFontSize(20)//设置字体样式
  55. context.textBaseline = “middle”;//字体上下居中,绘制时间
  56. for(var i = 1; i < 13; i++) {
  57. //利用三角函数计算字体坐标表达式
  58. var x = R * Math.cos(i * Math.PI / 6 - Math.PI / 2);
  59. var y = R * Math.sin(i * Math.PI / 6 - Math.PI / 2);
  60. if(i==11   i==12){//调整数字11和12的位置
  61. context.fillText(i, x-12, y+9);
  62. }else {
  63. context.fillText(i, x-6, y+9);
  64. }
  65. }
  66. }
  67. //绘制小格
  68. function smallGrid(){
  69. context.setLineWidth(1);
  70. context.rotate(-Math.PI/2);//时间从3点开始,倒转90度
  71. for(var i = 0; i < 60; i++) {
  72. context.beginPath();
  73. context.rotate(Math.PI / 30);
  74. context.moveTo(width/2-30, 0);
  75. context.lineTo(width/2-40, 0);
  76. context.stroke();
  77. }
  78. }
  79. //绘制大格
  80. function bigGrid(){
  81. context.setLineWidth(5);
  82. for(var i = 0; i < 12; i++) {
  83. context.beginPath();
  84. context.rotate(Math.PI / 6);
  85. context.moveTo(width/2-30, 0);
  86. context.lineTo(width/2-45, 0);
  87. context.stroke();
  88. }
  89. }
  90. //指针运动函数
  91. function move(){
  92. var t = new Date();//获取当前时间
  93. var h = t.getHours();//获取小时
  94. h = h>12?(h-12):h;//将24小时制转化为12小时制
  95. var m = t.getMinutes();//获取分针
  96. var s = t.getSeconds();//获取秒针
  97. context.save();//再次保存2
  98. context.setLineWidth(7);
  99. //旋转角度=30度*(h+m/60+s/3600)
  100. //分针旋转角度=6度*(m+s/60)
  101. //秒针旋转角度=6度*s
  102. context.beginPath();
  103. //绘制时针
  104. context.rotate((Math.PI/6)*(h+m/60+s/3600));
  105. context.moveTo(-20,0);
  106. context.lineTo(width/4.5-20,0);
  107. context.stroke();
  108. context.restore();//恢复到2,(最初未旋转状态)避免旋转叠加
  109. context.save();//3
  110. //画分针
  111. context.setLineWidth(5);
  112. context.beginPath();
  113. context.rotate((Math.PI/30)*(m+s/60));
  114. context.moveTo(-20,0);
  115. context.lineTo(width/3.5-20,0);
  116. context.stroke();
  117. context.restore();//恢复到3,(最初未旋转状态)避免旋转叠加
  118. context.save();
  119. //绘制秒针
  120. context.setLineWidth(2);
  121. context.beginPath();
  122. context.rotate((Math.PI/30)*s);
  123. context.moveTo(-20,0);
  124. context.lineTo(width/3-20,0);
  125. context.stroke();
  126. }
  127. //调用
  128. function drawClock(){
  129. reSet();
  130. circle();
  131. num();
  132. smallGrid();
  133. bigGrid();
  134. move();
  135. }
  136. drawClock()//调用运动函数
  137. // 调用 wx.drawCanvas,通过 canvasId 指定在哪张画布上绘制,通过 actions 指定绘制行为
  138. wx.drawCanvas({
  139. canvasId:’myCanvas’,
  140. actions: context.getActions()
  141. })
  142. },
  143. //页面卸载,清除画布绘制计时器
  144. onUnload:function(){
  145. clearInterval(this.interval)
  146. }
    149.})

2.[html

3.css

.canvas{

  1. width: 100%;
  2. height: 100%;
  3. position: fixed;
    5.}

[plain]

{

  1. “pages”: [
  2. “pages/index”
  3. ],
  4. “window”: {
  5. “navigationBarTextStyle”: “light”,
  6. “navigationBarTitleText”: “画布时钟”,
  7. “navigationBarBackgroundColor”: “#000”,
  8. “backgroundColor”: “#fbf9fe”
  9. }
    11.}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦