如何實(shí)現(xiàn)刮刮卡的效果呢?
思路是:
1、先將中獎(jiǎng)的圖片或者文字位置和大小確定
2、開始畫canvas,將位置和大小跟之前中獎(jiǎng)的文案的位置保持一致。
3、在canvas上覆蓋一層灰色的蒙層,作出刮刮卡未刮之前的效果
具體代碼如下
let left=0; this.data.awardCanvas.moveTo(left,0) this.data.awardCanvas.lineTo(left+400,0); this.data.awardCanvas.lineTo(left+400,150); this.data.awardCanvas.lineTo(left,150); this.data.awardCanvas.stroke() this.data.awardCanvas.setFillStyle('#ddd') this.data.awardCanvas.fill() this.data.awardCanvas.draw()
4、開始做刮刮卡的動(dòng)作,在canvas定義bindtouchstart和bindtouchmove兩個(gè)觸發(fā)的動(dòng)作
bindtouchstart是開始落手指的第一個(gè)位置,bindtouchmove是手指移動(dòng)的位置
其中重要一個(gè)canvas屬性是clearRect,清除畫布上的內(nèi)容
clearRect(清除位置的X坐標(biāo),清除位置的Y坐標(biāo),清除的寬度,清除的高度)
具體代碼如下:
this.data.awardCanvas.clearRect(x,y,15,15); this.data.awardCanvas.draw(true)
具體的代碼在https://github.com/zhaodengping/scratch-mini
快去試試吧,也許能刮到一等獎(jiǎng)哦~~
推薦教程:《微信小程序》