這篇文章主要介紹了微信小程序canvas怎么實(shí)現(xiàn)刮刮樂效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括會(huì)寧網(wǎng)站建設(shè)、會(huì)寧網(wǎng)站制作、會(huì)寧網(wǎng)頁制作以及會(huì)寧網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,會(huì)寧網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到會(huì)寧省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
效果圖
設(shè)計(jì)流程
設(shè)計(jì)思路
canvas設(shè)置背景圖,作為中獎(jiǎng)圖片;
在canvas上繪制刮的灰色涂層;
通過綁定的事件,清除對(duì)應(yīng)區(qū)域的涂層;
最后判斷涂層清除區(qū)域是否超過設(shè)置的可見百分比,如果超過則全部涂層清除,否則不清楚。
1、全局常量
獲取用戶傳入的canvas的ID,設(shè)置的canvas的寬高,canvas涂層的顏色,清除當(dāng)前坐標(biāo)的半徑和直徑,計(jì)算當(dāng)前清除的面積,全部清除百分比,canvas的面積。
constructor(page,opts){ opts = opts || {}; this.page = page; this.canvasId = opts.canvasId || 'luck'; this.width = opts.width || 300; this.height = opts.height || 150; this.maskColor = opts.maskColor || '#dddddd'; this.size = opts.size || 8; this.r = this.size * 2; this.area = this.r * this.r; this.scale = opts.scale || 0.75; this.totalArea = this.width * this.height; this.init(); }
2、初始化全局變量
1、變量:判斷清除全部涂層的布爾值,記錄清除坐標(biāo)的數(shù)組。
2、API:調(diào)用創(chuàng)建canvas繪圖上下文API。
3、方法:調(diào)用涂層繪制函數(shù),調(diào)用事件綁定函數(shù)。
init(){ this.show = false; this.clearPoints = []; this.ctx = wx.createCanvasContext(this.canvasId, this); this.drawMask(); this.bindTouch(); }
3、涂層繪制函數(shù)的實(shí)現(xiàn)
drawMask(){ this.ctx.setFillStyle(this.maskColor); this.ctx.fillRect(0, 0, this.width, this.height); this.ctx.draw(); }
4、事件綁定函數(shù)的實(shí)現(xiàn)
1 、touchstart事件只是清除當(dāng)前位置的坐標(biāo)點(diǎn)半徑的涂層。
2 、touchmove事件清除移動(dòng)過程個(gè)坐標(biāo)點(diǎn)半徑內(nèi)的涂層。
3 、touchend事件判斷當(dāng)前次清除是否超過總面積的75%,超過則全部清除,否則不做處理。
bindTouch(){ const _this = this; _this.page.onTouchStart = function(e){ _this.eraser(e,true); } _this.page.onTouchMove = function (e) { _this.eraser(e); } _this.page.onTouchEnd = function (e) { if(_this.show){ _this.ctx.clearRect(0, 0, _this.width, _this.height); _this.ctx.draw(); } } }
5、eraser橡皮擦函數(shù)的實(shí)現(xiàn)
1、獲取記錄清除坐標(biāo)點(diǎn)數(shù)組的長度,當(dāng)前位置的x,y坐標(biāo),計(jì)算清除塊的起點(diǎn),聲明計(jì)數(shù)變量。
2、判斷是否是第一次進(jìn)入,是則直接記錄該坐標(biāo)。
3、判斷當(dāng)前點(diǎn)在記錄數(shù)組中是否存在,如果存在,直接返回,如果不存在,在記錄入數(shù)組。
4、是否滿足清除全部涂層,滿足show賦值為true,不滿足,直接清除當(dāng)前坐標(biāo)涂層。
eraser(e,bool){ let len = this.clearPoints.length; let count = 0 let x = e.touches[0].x, y = e.touches[0].y; let x1 = x - this.size; let y1 = y - this.size; if(bool){ this.clearPoints.push({ x1: x1, y1: y1, x2: x1 + this.r, y2: y1 + this.r }) } for (let val of this.clearPoints){ if(val.x1 > x || val.y1 > y || val.x2 < x || val.y2 < y){ count++; }else{ break; } } if(len === count){ this.clearPoints.push({ x1: x1, y1: y1, x2: x1 + this.r, y2: y1 + this.r }) } if (this.clearPoints.length && this.r * this.r * this.clearPoints.length > this.scale * this.totalArea){ this.show = true; } this.ctx.clearRect(x1, y1, this.r, this.r); this.ctx.draw(true); }
提供的其他實(shí)現(xiàn)思路
方法一:本文提供的方法是將中獎(jiǎng)圖片作為背景設(shè)置給canvas,然后清除canvas涂層來實(shí)現(xiàn)刮刮樂效果。
方法二:canvas繪制涂層,然后取圖片在canvas相同坐標(biāo)的像素,最后將該處圖片像素繪制到canvas。
方法三:類似方法一,只是將背景直接用圖片img.定位在canvas的下邊。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信小程序canvas怎么實(shí)現(xiàn)刮刮樂效果”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
網(wǎng)站名稱:微信小程序canvas怎么實(shí)現(xiàn)刮刮樂效果
網(wǎng)頁地址:http://www.rwnh.cn/article8/ghcoip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、虛擬主機(jī)、定制開發(fā)、App開發(fā)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)