JavaScript+Canvas模擬實現(xiàn)支付寶畫年兔游戲
接近過年了,支付寶的集福的活動又開始了,集美們的五福集齊了沒有。每年的集福活動都有一些小游戲,今年也不例外,畫年畫就是其中之一,本篇用canvas來寫一個畫年兔的游戲。
動手前的思路
畫年畫游戲規(guī)則是:跟著特定輪廓畫出線條來。
思考1、如何讓鼠標只能在特定區(qū)域內(nèi)畫畫?
首先要獲取到這個輪廓區(qū)域所在畫布上的位置,判斷鼠標繪畫的位置是否在指定范圍內(nèi)。用canvas的getImageData函數(shù)能夠獲取到畫布上有顏色的像素點,然后根據(jù)像素點分布計算出像素點所在位置。
getImageData函數(shù)的用法在我之前的文章canvas文字粒子特效中有詳細介紹,不懂的可以去看看。
getImageData(canvas, ctx) { const data = ctx.getImageData( 0, 0, canvas.width, canvas.height, ).data; const gap = 4; const points = []; const length = data.length; for (let i = 0, wl = canvas.width * gap; i < length; i += gap) { if (data[i + gap - 1] == 255) { // 根據(jù)透明度判斷 const x = (i % wl) / gap; const y = Math.ceil(i / wl); points.push([x, y]); } } return points; }
思考2、如何讓繪制的圖畫動起來
通過定時旋轉(zhuǎn)畫布實現(xiàn)。我選擇用幀動畫requestAnimationFrame函數(shù),比setInterval函數(shù)性能更好一點。
思考3、如何撤銷上一步操作
將每一步繪制的點都記錄到創(chuàng)建的棧中,每一次撤銷都把上一步的繪制點刪除。
思考4、如何判斷線條繪制完畢
想了很久沒有什么太好的辦法,如果你有想法可以分享給我。當mouseup事件執(zhí)行,會判斷當前步驟下繪制的點數(shù)是否>=30,如果滿足條件會延遲半秒執(zhí)行下一步繪制,當mousedown在半秒內(nèi)觸發(fā),延遲函數(shù)會取消,等待下一個mouseup事件。
// mouseup事件 const length = _this.execStack.reduce((prev, next) => { if (next.step == _this.curStep) { prev += next.points.length; } return prev; }, 0) if (length >= 30) { _this.timer = setTimeout(() => { if (_this.curStep == 2) { _this.curStep = 2.5 _this.canEdit = false; _this.animate('ears_1', 3); } else if (_this.curStep == 4) { _this.curStep = 4.5 _this.canEdit = false; _this.animate('shake_head', 5); } else if (_this.curStep == 6) { _this.curStep = 6.5 _this.canEdit = false; _this.animate('shake_body', 7); } else if (_this.curStep <= 6) { _this.canEdit = true; _this.curStep += 1; } _this.execCanvas(); }, 500); }
關(guān)鍵步驟
1、創(chuàng)建一個RabbitPainting類,初始化時監(jiān)聽canvas的鼠標點擊事件。要注意的是移動端和pc端監(jiān)聽的事件不同:
this.pcEvents = ['mousedown', 'mousemove', 'mouseup']; this.mobileEvents = ['touchstart', 'touchmove', 'touchend'];
我的代碼里對兼容的處理比較粗糙,只是將大致功能做出來了,所以大家多看看思路。
鼠標移開之后,需要解除事件監(jiān)聽,當鼠標重新按壓時再綁定事件。mousedown事件監(jiān)聽流程如下:
鼠標移動時,會得到兩個點,鼠標按壓位置和鼠標移動位置,如果繪制的線都是從按壓點到移動點的話,就會畫出:
上圖所示,紅色的線是鼠標移動路徑,黑色的線是canvas畫出的線條,所以mousemove函數(shù)執(zhí)行后要更新初始按壓點,使前后兩個點銜接在一起。
線條繪制函數(shù)如下:
drawLine(point) { const { ctx } = this ctx.beginPath() ctx.moveTo(point.startX, point.startY); ctx.lineTo(point.endX, point.endY); if (point.style) { for (let key in point.style) { ctx[key] = point.style[key] } } ctx.stroke(); ctx.closePath() }
2、兔子輪廓繪制,采用貝塞爾2階函數(shù)繪制圖形
// 外輪廓樣式 const wrapperStyle = { lineWidth: "30", strokeStyle: this.tipPathColor[0] } // 內(nèi)虛線樣式 const innerStyle = { lineWidth: "3", strokeStyle: this.tipPathColor[1], lineDash: [15, 12] }
drawCurve({ list, wrapperStyle, innerStyle }) { const { tempCtx: ctx } = this list.forEach(point => { const { x, y, list } = point; ctx.beginPath(); ctx.moveTo(x, y); ctx.bezierCurveTo(...list); for (let key in wrapperStyle) { ctx[key] = wrapperStyle[key] } ctx.stroke(); ctx.save() ctx.beginPath(); ctx.moveTo(x, y); ctx.bezierCurveTo(...list); for (let key in innerStyle) { if (key == 'lineDash') { ctx.setLineDash(innerStyle[key]); } ctx[key] = innerStyle[key] } ctx.stroke(); ctx.restore(); }) }
貝塞爾曲線的關(guān)鍵在于設置p1和p2兩個控制點,大家自行把握。
我的兔子輪廓總體是這樣的:
3、旋轉(zhuǎn)畫布功能
使用canvas的rotate函數(shù),畫布的默認中心點是(0,0),所以旋轉(zhuǎn)時需要用translate(x,y)函數(shù)將中心點移動到特定位置。要注意旋轉(zhuǎn)后將畫布的中心點還原到(0,0)。
const rotateCanvas = (centerPoints, item) => { ctx.save() ctx.translate(...centerPoints) ctx.rotate(Math.PI / 180 * item.curDeg) ctx.translate(-centerPoints[0], -centerPoints[1]) }
注意ctx.save(),用來記錄畫布旋轉(zhuǎn)之前的狀態(tài),繪制結(jié)束后需要用ctx.restore()將畫布狀態(tài)還原,否則定時函數(shù)執(zhí)行角度旋轉(zhuǎn)時角度會累加。
4、眨眼睛動畫
眨眼睛是用一張精靈圖,因為圖片是我自己畫的,只有六幀,所以動畫看起來不是很好,將就看著吧。
以上就是JavaScript+Canvas模擬實現(xiàn)支付寶畫年兔游戲的詳細內(nèi)容,更多關(guān)于JavaScript Canvas畫年兔游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript+css實現(xiàn)單擊顏色褪去效果
javascript+css實現(xiàn)單擊顏色褪去效果...2007-08-08Bootstrap實現(xiàn)input控件失去焦點時驗證
這篇文章主要介紹了Bootstrap實現(xiàn)input控件失去焦點時驗證的相關(guān)資料,非常不錯,需要的朋友可以參考下,需要的朋友可以參考下2016-08-08