当前位置:   article > 正文

微信小程序刮刮乐_小程序刮刮乐逻辑

小程序刮刮乐逻辑

网上找了个插件,scratch.js下载地址:https://github.com/FrontendMing/scratch-in-wx-miniprogram

个人对scratch.js做了一些修改,设置清除面积达到一定值时(if (clearNum > .2) ),使刮卡刮干净,并触发(结果交互)方法;

测试时 调试基础库(project.config.json 文件中参数:"libVersion": "2.2.4",)设置成 2.2.4及以上,否则刮奖操作时没有反应,以下贴出代码


scratch.js代码:

  1. class Scratch {
  2. constructor(page, opts) {
  3. opts = opts || {};
  4. this.page = page;
  5. this.canvasId = opts.canvasId || 'canvas';
  6. this.width = opts.width || 300;
  7. this.height = opts.height || 300;
  8. this.maskColor = opts.maskColor || '#dddddd';
  9. this.size = opts.size || 15,
  10. //this.r = this.size * 2;
  11. this.r = this.size;
  12. this.area = this.r * this.r;
  13. //this.scale = opts.scale || 0.7;
  14. this.scale = opts.scale || .5;
  15. this.totalArea = this.width * this.height;
  16. this.init();
  17. }
  18. init() {
  19. this.show = false;
  20. this.clearPoints = [];
  21. this.ctx = wx.createCanvasContext(this.canvasId, this);
  22. this.drawMask();
  23. this.bindTouch();
  24. }
  25. drawMask() {
  26. this.ctx.setFillStyle(this.maskColor);
  27. this.ctx.fillRect(0, 0, this.width, this.height);
  28. this.ctx.draw();
  29. }
  30. bindTouch() {
  31. this.page.touchStart = (e) => {
  32. this.eraser(e, true);
  33. }
  34. this.page.touchMove = (e) => {
  35. this.eraser(e, false);
  36. }
  37. this.page.touchEnd = (e) => {
  38. if (this.show) {
  39. this.page.clearCanvas();
  40. this.ctx.clearRect(0, 0, this.width, this.height);
  41. this.ctx.draw();
  42. }
  43. }
  44. }
  45. eraser(e, bool) {
  46. let len = this.clearPoints.length;
  47. let count = 0;
  48. let x = e.touches[0].x,
  49. y = e.touches[0].y;
  50. let x1 = x - this.size;
  51. let y1 = y - this.size;
  52. if (bool) {
  53. this.clearPoints.push({
  54. x1: x1,
  55. y1: y1,
  56. x2: x1 + this.r,
  57. y2: y1 + this.r
  58. })
  59. }
  60. for (let item of this.clearPoints) {
  61. if (item.x1 > x || item.y1 > y || item.x2 < x || item.y2 < y) {
  62. count++;
  63. } else {
  64. break;
  65. }
  66. }
  67. if (len === count) {
  68. this.clearPoints.push({
  69. x1: x1,
  70. y1: y1,
  71. x2: x1 + this.r,
  72. y2: y1 + this.r
  73. });
  74. }
  75. //添加计算已清除的面积,达到标准值后,设置刮卡区域刮干净
  76. let clearNum = parseFloat(this.r * this.r * len) / parseFloat(this.scale * this.totalArea);
  77. if (!this.show) {
  78. this.page.setData({
  79. clearNum: parseFloat(this.r * this.r * len) / parseFloat(this.scale * this.totalArea)
  80. })
  81. }
  82. if (clearNum > .2) {
  83. //if (len && this.r * this.r * len > this.scale * this.totalArea) {
  84. this.show = true;
  85. }
  86. this.clearArcFun(x, y, this.r, this.ctx);
  87. this.ctx.draw(true);
  88. }
  89. clearArcFun(x, y, r, ctx) {
  90. let stepClear = 1;
  91. clearArc(x, y, r);
  92. function clearArc(x, y, radius) {
  93. let calcWidth = radius - stepClear;
  94. let calcHeight = Math.sqrt(radius * radius - calcWidth * calcWidth);
  95. let posX = x - calcWidth;
  96. let posY = y - calcHeight;
  97. let widthX = 2 * calcWidth;
  98. let heightY = 2 * calcHeight;
  99. if (stepClear <= radius) {
  100. ctx.clearRect(posX, posY, widthX, heightY);
  101. stepClear += 1;
  102. clearArc(x, y, radius);
  103. }
  104. }
  105. }
  106. }
  107. export default Scratch


使用demo:
.js:

  1. var app = getApp();
  2. import Scratch from '../../utils/scratch.js'
  3. let cjIn = false; //防止多次点击
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. gjEnd: false, //是否刮奖结束
  10. showGjBth: true, //是否显示刮奖按钮
  11. again: false, //是否显示再来一次按钮
  12. cjNum: '', //抽奖机会
  13. resaultSrc: '', //中奖显示图
  14. hasPrize: '', //是否中奖
  15. prizeName: '', //奖品名称
  16. },
  17. //刮奖设置
  18. gj() {
  19. let This = this;
  20. if (!This.data.cjNum) {
  21. wx.showModal({
  22. title: '很抱歉',
  23. content: '没有机会啦~',
  24. showCancel: false,
  25. success(res) {
  26. }
  27. })
  28. return;
  29. }
  30. if (cjIn) {
  31. return;
  32. } else {
  33. cjIn = true;
  34. }
  35. // let dataJson = {
  36. //
  37. // };
  38. // wx.showLoading({
  39. // title: '页面加载中',
  40. // })
  41. //获取抽奖结果
  42. // app._post_form(
  43. // '获取抽奖结果',
  44. // dataJson,
  45. // (res) => {
  46. // res = res.data;
  47. //
  48. // console.log(res);
  49. let timeOutTime = 0; //再次 刮奖时要先显示CANVAS,如果显示CANVAS 与 初始化刮奖界面同时进行的话,则不能重置刮奖层状态
  50. if (This.data.gjEnd) {
  51. //先显示CANVAS
  52. This.setData({
  53. gjEnd: false, //是否刮奖结束
  54. });
  55. timeOutTime = 100;
  56. }
  57. setTimeout(function() {
  58. //初始化刮奖界面 ,重置刮奖层状态
  59. new Scratch(This, {
  60. canvasId: 'canvas-demo',
  61. width:570,
  62. height:213,
  63. //maskColor:'#dddddd',
  64. //size:15,
  65. //scale:1,
  66. scale:.5,
  67. })
  68. setTimeout(function() {
  69. //重置刮奖层状态后,再绑定 获取到的结果 数据,否则会出现结果先出来导致“闪动”的状态
  70. This.setData({
  71. showGjBth: false, //是否显示刮奖按钮
  72. again: false, //是否显示再来一次按钮
  73. //cjNum: 0, //抽奖机会
  74. cjNum: 1, //抽奖机会
  75. resaultSrc: '', //中奖显示图
  76. hasPrize: true, //是否中奖
  77. prizeName: '奖品名称XXX', //奖品名称
  78. });
  79. }, 100)
  80. }, timeOutTime)
  81. // wx.hideLoading();
  82. // },
  83. // (res) => {
  84. // //失败
  85. // console.log(res)
  86. //
  87. // cjIn = false;
  88. //
  89. // wx.hideLoading();
  90. // },
  91. // )
  92. },
  93. //刮卡已刮干净
  94. clearCanvas() {
  95. let This = this;
  96. setTimeout(function() {
  97. //隐藏CANVAS
  98. This.setData({
  99. gjEnd: true, //是否刮奖结束
  100. });
  101. }, 100)
  102. if (!This.data.again) {
  103. cjIn = false;
  104. console.log('刮卡已刮干净啦!');
  105. //刮卡刮干净后 显示的界面 ,显示再刮一次按钮(页面中已注释)
  106. This.setData({
  107. again: true,
  108. });
  109. }
  110. },
  111. //获取抽奖机会次数
  112. getNum() {
  113. let This = this;
  114. This.setData({
  115. cjNum: 1,
  116. });
  117. },
  118. /**
  119. * 生命周期函数--监听页面加载
  120. */
  121. onLoad: function(options) {
  122. },
  123. /**
  124. * 生命周期函数--监听页面初次渲染完成
  125. */
  126. onReady: function() {
  127. },
  128. /**
  129. * 生命周期函数--监听页面显示
  130. */
  131. onShow: function() {
  132. cjIn = false;
  133. let This = this;
  134. This.setData({
  135. gjEnd: false, //是否刮奖结束
  136. showGjBth: true, //是否显示刮奖按钮
  137. again: false,//是否显示再来一次按钮
  138. resaultSrc: '', //中奖显示图
  139. hasPrize: '', //是否中奖
  140. });
  141. This.getNum(); //获取抽奖机会次数
  142. },
  143. /**
  144. * 生命周期函数--监听页面隐藏
  145. */
  146. onHide: function() {
  147. },
  148. /**
  149. * 生命周期函数--监听页面卸载
  150. */
  151. onUnload: function() {
  152. },
  153. /**
  154. * 页面相关事件处理函数--监听用户下拉动作
  155. */
  156. onPullDownRefresh: function() {
  157. },
  158. /**
  159. * 页面上拉触底事件的处理函数
  160. */
  161. onReachBottom: function() {
  162. },
  163. /**
  164. * 用户点击右上角分享
  165. */
  166. onShareAppMessage: function() {
  167. }
  168. })

.wxss:

  1. page{ box-sizing: border-box; padding-top: 50rpx; background-color: #3b0a8e;}
  2. .scratch_body{ width: 630rpx; padding-top: 24rpx; margin: 0 auto; position: relative;}
  3. .scratch_body_bg{ width: 100%; position: absolute; left: 0; top: 0;}
  4. .scratch_time{ width: 440rpx; height: 64rpx; margin: 0 auto 20rpx auto; text-align: center; color: #e81903; font-size: 28rpx; position: relative;}
  5. .scratch_time text{ font-size: 42rpx;}
  6. .scratch_do{ width: 570rpx; height: 213rpx;/* margin: 0 auto 160rpx auto; */ margin: 0 auto 110rpx auto; position: relative;}
  7. .scratch_do_after , .scratch_do_after_box , .scratch_do_after_box image , .scratch_do_in , .scratch_do_before{ width: 100%; height: 100%; position: absolute; left: 0; top: 0;}
  8. .scratch_do_after_box{ padding-top: 25rpx; line-height: 82rpx; color: #7a7a7a; font-size: 40rpx; text-align: center;}
  9. .scratch_do_after_box view{ position: relative;}
  10. .scratch_do_after_btn{ width: 210rpx; height: 54rpx; line-height: 54rpx; margin: 0 auto; color: #fff; font-size: 30rpx; border-radius: 27rpx; background-color: #ff3c9d;}
  11. .scratch_btn{ width: 310rpx; height: 64rpx; margin: 34rpx auto 0 auto; border: #fff solid 2rpx; border-radius: 32rpx; position: relative;
  12. display: flex; justify-content: center; align-items: center;
  13. box-shadow: 0 0 15px 5px rgba(102,12,164,.8);
  14. }
  15. .scratch_btn view{ width: 300rpx; height: 52rpx; line-height: 52rpx; text-align: center; color: #3b0a8e; font-size: 30rpx; font-weight: bold; background-color: #fff; border-radius: 26rpx;}

.wxml:

  1. <view class="scratch_body">
  2. <image class="scratch_body_bg" mode="widthFix" src="https://img-blog.csdnimg.cn/2022010702543295939.png"></image>
  3. <view class="scratch_time">您有 <text>{{cjNum}}</text> 次刮奖机会</view>
  4. <view class="scratch_do">
  5. <view class="scratch_do_after">
  6. <view wx:if="{{hasPrize}}" class="scratch_do_after_box">
  7. <image mode="widthFix" src="https://img-blog.csdnimg.cn/2022010702543242350.png"></image>
  8. <view>恭喜你刮中{{prizeName}}</view>
  9. <view class="scratch_do_after_btn" bindtap="gj">再来一次</view>
  10. </view>
  11. <view wx:else class="scratch_do_after_box">
  12. <view>很遗憾未中奖</view>
  13. <view class="scratch_do_after_btn" bindtap="gj">再来一次</view>
  14. </view>
  15. </view>
  16. <canvas wx:if="{{!gjEnd}}" class="scratch_do_in" disable-scroll="false" canvas-id='canvas-demo' bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd"></canvas>
  17. <cover-image wx:if="{{showGjBth}}" class="scratch_do_before" src="https://img-blog.csdnimg.cn/2022010702543256236.png" bindtap="gj"></cover-image>
  18. <!-- <view wx:if="{{again}}" class="scratch_btn again_btn" bindtap="gj">
  19. <view>再刮一次</view>
  20. </view> -->
  21. </view>
  22. <view class="scratch_btn">
  23. <view>我的奖品</view>
  24. </view>
  25. <view class="scratch_btn">
  26. <view>活动规则</view>
  27. </view>
  28. </view>


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/571078
推荐阅读
相关标签
  

闽ICP备14008679号