亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序?qū)崿F(xiàn)的涂鴉功能示例【附源碼下載】

 更新時(shí)間:2018年01月12日 15:16:22   作者:yaoohfox  
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)的涂鴉功能,涉及微信小程序事件響應(yīng)及畫筆的相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下

本文實(shí)例講述了微信小程序?qū)崿F(xiàn)的涂鴉功能。分享給大家供大家參考,具體如下:

先來看看運(yùn)行效果:

布局文件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;
 }
})

附:完整實(shí)例代碼點(diǎn)擊此處本站下載

希望本文所述對(duì)大家微信小程序開發(fā)有所幫助。

相關(guān)文章

最新評(píng)論