Commit 61f23af3 by 谢中龙

刮刮乐

parent 2ce448be
...@@ -46,7 +46,9 @@ class Scratch { ...@@ -46,7 +46,9 @@ class Scratch {
this.maskColor = opts.maskColor this.maskColor = opts.maskColor
// this.canvasId = opts.canvasId // this.canvasId = opts.canvasId
this.r = opts.r || 4 this.r = opts.r || 4
this.endCallBack = opts.callback this.endCallBack = opts.callback,
this.initOkCallBack = opts.initOkCallBack,
this.lastX = 0 this.lastX = 0
this.lastY = 0 this.lastY = 0
this.minX = '' this.minX = ''
...@@ -86,15 +88,29 @@ class Scratch { ...@@ -86,15 +88,29 @@ class Scratch {
success: res => { success: res => {
self.ctx.drawImage(res.tempFilePath, 0, 0, canvasWidth, canvasHeight) self.ctx.drawImage(res.tempFilePath, 0, 0, canvasWidth, canvasHeight)
self.ctx.draw() self.ctx.draw()
setTimeout(() => {
self.initOkCallBack && self.initOkCallBack();
},100);
} }
}) })
} else { } else {
self.ctx.setFillStyle(maskColor) self.ctx.setFillStyle(maskColor)
self.ctx.fillRect(0, 0, canvasWidth, canvasHeight) self.ctx.fillRect(0, 0, canvasWidth, canvasHeight)
self.ctx.draw() self.ctx.draw()
setTimeout(() => {
self.initOkCallBack && self.initOkCallBack();
},100);
} }
} }
//设置中奖文案
setAwardText(awardTxt){
this.page.data.scratch.awardTxt = awardTxt;
this.page.setData({
scratch : this.page.data.scratch
});
}
drawRect (x, y) { drawRect (x, y) {
const { r, minX, minY, maxX, maxY } = this const { r, minX, minY, maxX, maxY } = this
const x1 = x - r > 0 ? x - r : 0 const x1 = x - r > 0 ? x - r : 0
...@@ -132,8 +148,10 @@ class Scratch { ...@@ -132,8 +148,10 @@ class Scratch {
this.maxX = '' this.maxX = ''
this.maxY = '' this.maxY = ''
this.isStart = true this.isStart = true
this.page.data.scratch.awardTxt = '开奖中...';
this.page.setData({ this.page.setData({
'isScroll': false 'isScroll': false,
scratch : this.page.data.scratch,
}) })
} }
......
<template name="scratch" > <template name="scratch" >
<view class="scratch_wp" style="position:relative;width: {{scratch.width}}px; height: {{scratch.height}}px;margin: 0 auto;" > <view class="scratch_wp" style="position:relative;width: {{scratch.width}}px; height: {{scratch.height}}px;margin: 0 auto;" >
<canvas style="margin: 0 auto;width: {{scratch.width}}px; height: {{scratch.height}}px; background: transparent" canvas-id="scratch" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" disable-scroll ="{{isScroll}}"></canvas> <canvas style="position:absolute;top:0;left:0;margin: 0 auto;width: {{scratch.width}}px; height: {{scratch.height}}px; background: transparent;z-index:1;" canvas-id="scratch" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" disable-scroll ="{{isScroll}}"></canvas>
<view class="scratch_txt" style="position:absolute;top:0;left:0;width: 100%;height:100%;text-align: center;line-height: {{scratch.awardTxtLineHeight}}px;color: {{scratch.awardTxtColor}};font-size: {{scratch.awardTxtFontSize}};pointer-events:none;z-index: -1">{{scratch.awardTxt}}</view> <view class="scratch_txt"
style="position:absolute;top:0;left:0;width: 100%;height:100%;display: flex;align-items: center;justify-content: center;color: {{scratch.awardTxtColor}};font-size: {{scratch.awardTxtFontSize}};pointer-events:none;z-index: 0">
{{scratch.awardTxt}}</view>
</view> </view>
</template> </template>
\ No newline at end of file
...@@ -2,14 +2,16 @@ ...@@ -2,14 +2,16 @@
const wxService = require('../../../../utils/wxService') const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util') const utils = require('../../../../utils/util')
import Scratch from '../../components/scratch/scratch.js' import Scratch from '../../components/scratch/scratch.js'
const app = getApp();
wxService.page({ wxService.page({
/** /**
* 页面的初始数据 * 页面的初始数据
*/ */
data: { data: {
loading : true,
btnName : '开始刮奖',
leftNum : 2,
}, },
/** /**
...@@ -19,36 +21,55 @@ wxService.page({ ...@@ -19,36 +21,55 @@ wxService.page({
this.initScratch(); this.initScratch();
}, },
//开始刮奖或重新刮奖
onClickStartScratch(){
if(this.data.leftNum <= 0){
wx.showToast({
title: '对不起,您没有刮奖机会了',
icon : 'none'
});
return;
}
if(this.data.btnName == '开始刮奖'){
this.scratch.start();
this.setData({
btnName : '重新刮奖'
});
}
else{
this.scratch.restart();
}
},
//初始化刮刮卡 //初始化刮刮卡
initScratch(){ initScratch(){
let width = app.globalData.systemInfo.screenWidth;
let self = this;
this.scratch = new Scratch(this, { this.scratch = new Scratch(this, {
canvasWidth: 197, canvasWidth: width*0.9 - 30,
canvasHeight: 72, canvasHeight: 130,
imageResource: 'https://img3.bigaka.com/test/1002/202004/20200413/100208b0fbce-b73d-4af9-bd74-47d1ea5311d5.png', imageResource: 'https://img3.bigaka.com/test/1002/202004/20200413/100208b0fbce-b73d-4af9-bd74-47d1ea5311d5.png',
maskColor: 'red', // maskColor: 'red',
r: 4, r: 10,
awardTxt: '中大奖', awardTxt: '开奖中...',
awardTxtColor: '#3985ff', awardTxtColor: '#333333',
awardTxtFontSize: '24px', awardTxtFontSize: '22px',
callback: () => { callback: () => {
wx.showModal({ console.log('提示语')
title: '提示', this.scratch.setAwardText('恭喜您,中了100积分')
content: `您中奖了`, this.data.leftNum --;
showCancel: false, this.setData({
success: res => { leftNum : this.data.leftNum
// this.scratch.reset() });
if (res.confirm) { },
console.log('用户点击确定') initOkCallBack(){
} else if (res.cancel) { self.setData({
console.log('用户点击取消') loading : false
}
}
}) })
} },
}); });
//this.scratch.start() // this.scratch.start()
this.scratch.start()
//this.scratch.restart() //this.scratch.restart()
}, },
......
{ {
"disableScroll": true,
"navigationBarTitleText": "刮刮卡", "navigationBarTitleText": "刮刮卡",
"usingComponents": {} "usingComponents": {}
} }
\ No newline at end of file
<!--subPackageMarketing/page/pages/scratch/scratch.wxml--> <!--subPackageMarketing/page/pages/scratch/scratch.wxml-->
<import src="../../components/scratch/scratch.wxml" /> <import src="../../components/scratch/scratch.wxml" />
<!-- 布局 --> <!-- 布局 -->
<view class="acticity-bg"></view> <!-- style="background-image:url(https://img3.bigaka.com/test/1002/202004/20200413/10021096c17c-fbc6-49a8-a53c-8bbf0502df44.jpg)" -->
<view class="acticity-bg" style="background-image:url(https://img3.bigaka.com/test/1002/202004/20200413/10021096c17c-fbc6-49a8-a53c-8bbf0502df44.jpg)">
<view class="index"> <!-- 展示还有几次 -->
<view class="index-hd"> <view class="scratch-count">您还有{{leftNum}}次刮奖机会</view>
<view class="index-desc">以下是小程序刮刮乐组件,组件样式仅供参考,开发者可根据自身需求自定义组件样式,具体属性参数详见开发文档。</view> <view class="scratch-area" style="background-image:url(https://img3.bigaka.com/test/1002/202004/20200413/1002cc8ea3b2-7697-48e7-85e5-76e937f611e8.png)">
<view class="index">
<template is = "scratch" data = "{{scratch, isScroll}}"></template>
</view>
</view> </view>
<template is = "scratch" data = "{{scratch, isScroll}}"></template> <view class="scratch-btn" bindtap="onClickStartScratch">{{btnName}}</view>
<button bindtap = "onStart">{{txt}}</button>
</view> </view>
<!-- 查看规则 -->
<view class="view-rule">查看规则</view>
<!-- loading -->
<loading wx:if="{{loading}}"/>
...@@ -11,27 +11,70 @@ view{ ...@@ -11,27 +11,70 @@ view{
-webkit-box-sizing: border-box; -webkit-box-sizing: border-box;
} }
.view-rule{
position: fixed;
z-index: 2;
right: 0;
top: 5vh;
background: #ffffff;
height: 60rpx;
width: auto;
padding: 0 20rpx;
display: flex;
align-items: center;
font-size: 24rpx;
color: #333333;
border-top-left-radius: 30rpx;
border-bottom-left-radius: 30rpx;
}
.acticity-bg{ .acticity-bg{
width: 100vw; width: 100vw;
height: 100vh; height: 100vh;
background-position: center; background-position: center;
background-repeat: no-repeat; background-repeat: no-repeat;
background-size: cover; background-size: cover;
/* position: relative; */
padding-top: 35vh;
} }
.index-hd { .scratch-area{
padding: 80rpx; width: 90vw;
text-align: center; height: 330rpx;
padding: 20rpx 26rpx 40rpx; margin: auto;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
/* background-color: transparent; */
padding: 50rpx 30rpx;
} }
.index-desc { .scratch-count{
margin-top: 20rpx; background: rgba(0,0,0,0.5);
color: #888888; color: #ffffff;
font-size: 28rpx; font-size: 24rpx;
height: 56rpx;
margin: auto;
width: 50vw;
display: flex;
justify-content: center;
align-items: center;
border-radius: 28rpx;
margin-bottom: 20rpx;
} }
button{ .scratch-btn{
margin-top: 40rpx; width: 35vw;
width: 400rpx; height: 70rpx;
background: #ffffff;
font-size: 28rpx;
display: flex;
align-items: center;
justify-content: center;
margin: auto;
margin-top: 60rpx;
border-radius: 35rpx;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment