怎么在微信小程序中實現(xiàn)一個涂鴉功能

這篇文章將為大家詳細(xì)講解有關(guān)怎么在微信小程序中實現(xiàn)一個涂鴉功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

目前成都創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管運營、企業(yè)網(wǎng)站設(shè)計、鎮(zhèn)平網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

布局文件index.wxml:

<view class="container">
  <!--畫布區(qū)域-->
  <view class="canvas_area">
    <!--注意:同一頁面中的 canvas-id 不可重復(fù),如果使用一個已經(jīng)出現(xiàn)過的 canvas-id,該 canvas 標(biāo)簽對應(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)記
 //手指觸摸動作開始
 touchStart: function (e) {
   //得到觸摸點的坐標(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è)置線條端點的樣式
     this.context.setLineJoin('round') //設(shè)置兩線相交處的樣式
     this.context.setLineWidth(20) //設(shè)置線條寬度
     this.context.save(); //保存當(dāng)前坐標(biāo)軸的縮放、旋轉(zhuǎn)、平移信息
     this.context.beginPath() //開始一個路徑 
     this.context.arc(this.startX,this.startY,5,0,2*Math.PI,true); //添加一個弧形路徑到當(dāng)前路徑,順時針繪制 這里總共畫了360度 也就是一個圓形 
     this.context.fill(); //對當(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()
   }
 },
 //手指觸摸后移動
 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); //把路徑移動到畫布中的指定點,但不創(chuàng)建線條
    this.context.lineTo(startX1,startY1); //添加一個新點,然后在畫布中創(chuàng)建從該點到最后指定點的線條
    this.context.stroke(); //對當(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;
   }
   //只是一個記錄方法調(diào)用的容器,用于生成記錄繪制行為的actions數(shù)組。context跟<canvas/>不存在對應(yīng)關(guān)系,一個context生成畫布的繪制動作數(shù)組可以應(yīng)用于多個<canvas/>
   wx.drawCanvas({
     canvasId: 'myCanvas',
     reserve: true,
     actions: this.context.getActions() // 獲取繪圖動作數(shù)組
   })
 },
 //手指觸摸動作結(jié)束
 touchEnd: function () {
 },
 //啟動橡皮擦方法
 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)于怎么在微信小程序中實現(xiàn)一個涂鴉功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)站標(biāo)題:怎么在微信小程序中實現(xiàn)一個涂鴉功能
網(wǎng)頁URL:http://muchs.cn/article36/iepssg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站用戶體驗、網(wǎng)站排名、網(wǎng)站制作、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司