微信小程序圆环

布满荆棘的人生 2023-02-21 11:54 18阅读 0赞

小程序中使用圆形倒计时,效果图:
在这里插入图片描述
实现思路:
1):使用2个canvas,分别是背景圆环和渐变色圆环;
2):使用setInterval让渐变色圆环逐步绘制;

解决方案
1):结构
一个盒子包裹2个canvas以及文字盒子,使用相对定位作为父级,flex布局,设置居中;
另一个canvas,使用相对定位作为进度条,canvas-id=“canvasProgress”
2):数据绑定
3):canvas绘制

  • 在js中封装一个画圆环的函数drawProgressbg,canvas画圆;
  • 在onReady中执行这个函数;
  • 在drawCircle中绘制彩色圆环;
  • 设置一个定时器,在data中定义计算器count,步骤step,封装定时器的函数countinterval

2):代码如下

  1. <view class="container">
  2. <view class='progress_box'>
  3. <canvas class="progress_bg" canvas-id="canvasProgressbg"> </canvas>
  4. <canvas class="progress_canvas" canvas-id="canvasProgress"> </canvas>
  5. <view class="progress_text">
  6. <view class="progress_dot"></view>
  7. <text class='progress_info'> { { progress_txt}}</text>
  8. </view>
  9. </view>
  10. </view>
  11. .progress_box {
  12. position: relative;
  13. width: 220px;
  14. height: 220px;
  15. /* * 这里的宽高是必须大于等于canvas圆环的直径 否则绘制到盒子外面就看不见了 * 一开始设置 width:440rpx; height:440rpx; 发现 在360X640分辨率的设备,下绘制的圆环跑盒子外去了 * 小程序使用rpx单位适配 ,但是canvas绘制的是px单位的。所以只能用px单位绘制的圆环在盒子内显示 */
  16. display: flex;
  17. align-items: center;
  18. justify-content: center;
  19. padding: 65rpx 0;
  20. /* background-color: #130A34; */
  21. }
  22. .progress_bg {
  23. position: absolute;
  24. width: 220px;
  25. height: 220px;
  26. }
  27. .progress_canvas {
  28. width: 220px;
  29. height: 220px;
  30. }
  31. .progress_text {
  32. position: absolute;
  33. display: flex;
  34. align-items: center;
  35. justify-content: center
  36. }
  37. .progress_info {
  38. font-size: 36rpx;
  39. padding-left: 16rpx;
  40. letter-spacing: 2rpx;
  41. color: #000;
  42. }
  43. .progress_dot {
  44. width: 16rpx;
  45. height: 16rpx;
  46. border-radius: 50%;
  47. background-color: #fb9126;
  48. }
  49. Page({
  50. data: {
  51. progress_txt: '正在匹配中...',
  52. count: 0, // 设置 计数器 初始为0
  53. countTimer: null // 设置 定时器 初始为null
  54. }, drawProgressbg: function () {
  55. // 使用 wx.createContext 获取绘图上下文 context
  56. var ctx = wx.createCanvasContext('canvasProgressbg', this)
  57. ctx.setLineWidth(4);// 设置圆环的宽度
  58. ctx.setStrokeStyle('#c1bfbf'); // 设置圆环的颜色
  59. ctx.setLineCap('round') // 设置圆环端点的形状
  60. ctx.beginPath();//开始一个新的路径
  61. ctx.arc(110, 110, 100, 0, 2 * Math.PI, false);
  62. //设置一个原点(110,110),半径为100的圆的路径到当前路径
  63. ctx.stroke();//对当前路径进行描边
  64. ctx.draw();
  65. }, drawCircle: function (step) {
  66. // 使用 wx.createContext 获取绘图上下文 context
  67. var ctx = wx.createCanvasContext('canvasProgress', this)
  68. // 设置渐变
  69. var gradient = ctx.createLinearGradient(200, 100, 100, 200);
  70. gradient.addColorStop("0", "#E90032");
  71. gradient.addColorStop("0.5", "#f87894");
  72. gradient.addColorStop("1.0", "#ecbac5");
  73. ctx.setLineWidth(10);// 设置圆环的宽度
  74. ctx.setStrokeStyle(gradient); // 设置圆环的颜色
  75. ctx.setLineCap('round') // 设置圆环端点的形状
  76. ctx.beginPath();//开始一个新的路径
  77. // 参数step 为绘制的圆环周长,从0到2为一周 。 -Math.PI / 2 将起始角设在12点钟位置 ,结束角 通过改变 step 的值确定
  78. ctx.arc(110, 110, 100, -Math.PI / 2, step * Math.PI - Math.PI / 2, false);
  79. ctx.stroke();//对当前路径进行描边
  80. ctx.draw();
  81. }, countInterval: function () {
  82. // 设置倒计时 定时器 每100毫秒执行一次,计数器count+1 ,耗时6秒绘一圈 this.countTimer = setInterval(() => {
  83. if (this.data.count <= 60) {
  84. /* 绘制彩色圆环进度条 注意此处 传参 step 取值范围是0到2, 所以 计数器 最大值 60 对应 2 做处理,计数器count=60的时候step=2 */
  85. this.drawCircle(this.data.count / (60 / 2))
  86. this.data.count++;
  87. } else {
  88. this.setData({
  89. progress_txt: "匹配成功"
  90. });
  91. clearInterval(this.countTimer);
  92. }
  93. }, 100)
  94. }, onReady: function () {
  95. this.drawProgressbg();
  96. this.drawCircle(1); // 圆环彩色半圆部分
  97. // this.countInterval(); // 调用倒计时完成匹配
  98. },
  99. })

发表评论

表情:
评论列表 (有 0 条评论,18人围观)

还没有评论,来说两句吧...

相关阅读