這篇文章將為大家詳細(xì)講解有關(guān)怎么在微信小程序中實(shí)現(xiàn)一個(gè)涂鴉功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
目前成都創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、鎮(zhèn)平網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
布局文件index.wxml:
<view class="container"> <!--畫布區(qū)域--> <view class="canvas_area"> <!--注意:同一頁面中的 canvas-id 不可重復(fù),如果使用一個(gè)已經(jīng)出現(xiàn)過的 canvas-id,該 canvas 標(biāo)簽對(duì)應(yīng)的畫布將被隱藏并不再正常工作--> <canvas canvas-id="myCanvas" class="myCanvas" disable-scroll="false" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd"> </canvas> </view> <!--畫布工具區(qū)域--> <view class="canvas_tools"> <view class="box box1" bindtap="penSelect" data-param="5"></view> <view class="box box2" bindtap="penSelect" data-param="15"></view> <view class="box box3" bindtap="colorSelect" data-param="#cc0033"></view> <view class="box box4" bindtap="colorSelect" data-param="#ff9900"></view> <view class="box box5" bindtap="clearCanvas"></view> </view> </view>
邏輯功能文件index.js:
Page({ data:{ pen : 3, //畫筆粗細(xì)默認(rèn)值 color : '#cc0033' //畫筆顏色默認(rèn)值 }, startX: 0, //保存X坐標(biāo)軸變量 startY: 0, //保存X坐標(biāo)軸變量 isClear : false, //是否啟用橡皮擦標(biāo)記 //手指觸摸動(dòng)作開始 touchStart: function (e) { //得到觸摸點(diǎn)的坐標(biāo) this.startX = e.changedTouches[0].x this.startY = e.changedTouches[0].y this.context = wx.createContext() if(this.isClear){ //判斷是否啟用的橡皮擦功能 ture表示清除 false表示畫畫 this.context.setStrokeStyle('#F8F8F8') //設(shè)置線條樣式 此處設(shè)置為畫布的背景顏色 橡皮擦原理就是:利用擦過的地方被填充為畫布的背景顏色一致 從而達(dá)到橡皮擦的效果 this.context.setLineCap('round') //設(shè)置線條端點(diǎn)的樣式 this.context.setLineJoin('round') //設(shè)置兩線相交處的樣式 this.context.setLineWidth(20) //設(shè)置線條寬度 this.context.save(); //保存當(dāng)前坐標(biāo)軸的縮放、旋轉(zhuǎn)、平移信息 this.context.beginPath() //開始一個(gè)路徑 this.context.arc(this.startX,this.startY,5,0,2*Math.PI,true); //添加一個(gè)弧形路徑到當(dāng)前路徑,順時(shí)針繪制 這里總共畫了360度 也就是一個(gè)圓形 this.context.fill(); //對(duì)當(dāng)前路徑進(jìn)行填充 this.context.restore(); //恢復(fù)之前保存過的坐標(biāo)軸的縮放、旋轉(zhuǎn)、平移信息 }else{ this.context.setStrokeStyle(this.data.color) this.context.setLineWidth(this.data.pen) this.context.setLineCap('round') // 讓線條圓潤 this.context.beginPath() } }, //手指觸摸后移動(dòng) touchMove: function (e) { var startX1 = e.changedTouches[0].x var startY1 = e.changedTouches[0].y if(this.isClear){ //判斷是否啟用的橡皮擦功能 ture表示清除 false表示畫畫 this.context.save(); //保存當(dāng)前坐標(biāo)軸的縮放、旋轉(zhuǎn)、平移信息 this.context.moveTo(this.startX,this.startY); //把路徑移動(dòng)到畫布中的指定點(diǎn),但不創(chuàng)建線條 this.context.lineTo(startX1,startY1); //添加一個(gè)新點(diǎn),然后在畫布中創(chuàng)建從該點(diǎn)到最后指定點(diǎn)的線條 this.context.stroke(); //對(duì)當(dāng)前路徑進(jìn)行描邊 this.context.restore() //恢復(fù)之前保存過的坐標(biāo)軸的縮放、旋轉(zhuǎn)、平移信息 this.startX = startX1; this.startY = startY1; }else{ this.context.moveTo(this.startX, this.startY) this.context.lineTo(startX1, startY1) this.context.stroke() this.startX = startX1; this.startY = startY1; } //只是一個(gè)記錄方法調(diào)用的容器,用于生成記錄繪制行為的actions數(shù)組。context跟<canvas/>不存在對(duì)應(yīng)關(guān)系,一個(gè)context生成畫布的繪制動(dòng)作數(shù)組可以應(yīng)用于多個(gè)<canvas/> wx.drawCanvas({ canvasId: 'myCanvas', reserve: true, actions: this.context.getActions() // 獲取繪圖動(dòng)作數(shù)組 }) }, //手指觸摸動(dòng)作結(jié)束 touchEnd: function () { }, //啟動(dòng)橡皮擦方法 clearCanvas: function(){ if(this.isClear){ this.isClear = false; }else{ this.isClear = true; } }, penSelect: function(e){ //更改畫筆大小的方法 console.log(e.currentTarget); this.setData({pen:parseInt(e.currentTarget.dataset.param)}); this.isClear = false; }, colorSelect: function(e){ //更改畫筆顏色的方法 console.log(e.currentTarget); this.setData({color:e.currentTarget.dataset.param}); this.isClear = false; } })
關(guān)于怎么在微信小程序中實(shí)現(xiàn)一個(gè)涂鴉功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:怎么在微信小程序中實(shí)現(xiàn)一個(gè)涂鴉功能
網(wǎng)頁URL:http://jinyejixie.com/article36/iepssg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、用戶體驗(yàn)、網(wǎng)站排名、網(wǎng)站制作、商城網(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)