小程序刮刮卡來了

如何實現小程序刮刮卡呢?

思路是:

  1. 先將中獎的圖片或者文字位置和大小確定

  2. 開始畫canvas,將位置和大小跟之前中獎的文案的位置保持一致。

  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. 開始做刮刮卡的動作,在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

快去試試吧,也許能刮到一等獎哦~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章