如何實現小程序刮刮卡呢?
思路是:
-
先將中獎的圖片或者文字位置和大小確定
-
開始畫canvas,將位置和大小跟之前中獎的文案的位置保持一致。
-
在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()
-
開始做刮刮卡的動作,在canvas定義bindtouchstart和bindtouchmove兩個觸發的動作
bindtouchstart是開始落手指的第一個位置,bindtouchmove是手指移動的位置
其中重要一個canvas屬性是clearRect,清除畫布上的內容
clearRect(清除位置的X坐標,清除位置的Y坐標,清除的寬度,清除的高度)
具體代碼如下:
this.data.awardCanvas.clearRect(x,y,15,15);
this.data.awardCanvas.draw(true)
具體的代碼在https://github.com/zhaodengping/scratch-mini
快去試試吧,也許能刮到一等獎哦~~