欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

先來看看運行效果:

布局文件index.wxml:

<view class="container">
  <!--畫布區(qū)域-->
  <view class="canvas_area">
    <!--注意:同一頁面中的 canvas-id 不可重復(fù),如果使用一個已經(jīng)出現(xiàn)過的 canvas-id,該 canvas 標簽對應(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, //畫筆粗細默認值
  color : '#cc0033' //畫筆顏色默認值
 },
 startX: 0, //保存X坐標軸變量
 startY: 0, //保存X坐標軸變量
 isClear : false, //是否啟用橡皮擦標記
 //手指觸摸動作開始
 touchStart: function (e) {
   //得到觸摸點的坐標
   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è)置為畫布的背景顏色 橡皮擦原理就是:利用擦過的地方被填充為畫布的背景顏色一致 從而達到橡皮擦的效果 
     this.context.setLineCap('round') //設(shè)置線條端點的樣式
     this.context.setLineJoin('round') //設(shè)置兩線相交處的樣式
     this.context.setLineWidth(20) //設(shè)置線條寬度
     this.context.save(); //保存當(dāng)前坐標軸的縮放、旋轉(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)前路徑進行填充
     this.context.restore(); //恢復(fù)之前保存過的坐標軸的縮放、旋轉(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)前坐標軸的縮放、旋轉(zhuǎn)、平移信息
    this.context.moveTo(this.startX,this.startY); //把路徑移動到畫布中的指定點,但不創(chuàng)建線條
    this.context.lineTo(startX1,startY1); //添加一個新點,然后在畫布中創(chuàng)建從該點到最后指定點的線條
    this.context.stroke(); //對當(dāng)前路徑進行描邊
    this.context.restore() //恢復(fù)之前保存過的坐標軸的縮放、旋轉(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;
 }
})

附:完整實例代碼點擊此處本站下載。

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

相關(guān)文章

最新評論