国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

微信小程序實(shí)現簡(jiǎn)單手寫(xiě)簽名組件的方法實(shí)例

發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:dawnlion 欄目: JavaScript 歡迎投稿:712375056

目錄

    背景:

    在做項目過(guò)程中,需要在微信小程序中實(shí)現手寫(xiě)簽名組件。在網(wǎng)上找了微信小程序手寫(xiě)簽名實(shí)現,但是都是不太理想。在實(shí)際運用中,會(huì )因為實(shí)時(shí)計算較多的貝塞爾曲線(xiàn)而產(chǎn)生卡頓。效果不理想。所以退一步,不需要筆鋒以及筆跡模擬效果。只需要簡(jiǎn)單的手寫(xiě)簽名實(shí)現。

    需求:

    可以實(shí)現用戶(hù)在微信小程序上手寫(xiě)簽名。

    需要組件化。

    效果

    一、思路

    在微信小程序中,我們使用canvas組件實(shí)現。將用戶(hù)的輸入想象成為一只筆。我們要畫(huà)的簽名是由很多點(diǎn)構成的。但是單純的點(diǎn)是不能很好地構成線(xiàn)。點(diǎn)與點(diǎn)之間還要由線(xiàn)連接。下面是實(shí)現過(guò)程代碼。

    二、實(shí)現

    1. 頁(yè)面與樣式

    wxml

    這里的canvas組件是最新的用法。

    <view class="dashbox">
      <view class="btnList">
        <van-button size="small" bind:click="clearCanvas">清空</van-button>
      </view>
      <view class="handCenter">
        <canvas 
          class="handWriting" 
          disable-scroll="{{true}}" 
          id="handWriting"
          bindtouchstart="scaleStart"
          bindtouchmove="scaleMove" 
          bindtouchend="scaleEnd"
          bindtap="mouseDown"
          type="2d"
        >
        </canvas>
      </view>
    </view>
    

    wxss

    .btnList{
        width: 95%;
        margin:0 auto;
    }
    .handWriting{
        background: #fff;
        width: 95%;
        height: 80vh;
        margin:0 auto
    }
    

    2. 初始化

    由于是在自定義組件中使用,所以要注意獲取canvas的時(shí)候的this指向問(wèn)題。如果不調用SelectorQuery的In方法,那么就在自定義組件獲取不到canvas,因為這個(gè)時(shí)候指向的父組件。

    Component({
     /**
     * 組件的初始數據
     */
        data: {
            canvasName:'#handWriting',
            ctx:'',
            canvasWidth:0,
            canvasHeight:0,
            startPoint:{
                x:0,
                y:0,
            },
            selectColor: 'black',
            lineColor: '#1A1A1A', // 顏色
            lineSize: 1.5,  // 筆記倍數
            radius:5,//畫(huà)圓的半徑
        }, 
        ready(){
            let canvasName = this.data.canvasName;
            let query = wx.createSelectorQuery().in(this);//獲取自定義組件的SelectQuery對象
            query.select(canvasName)
            .fields({ node: true, size: true })
            .exec((res) => {
              const canvas = res[0].node;
              const ctx = canvas.getContext('2d');
              //獲取設備像素比
              const dpr = wx.getSystemInfoSync().pixelRatio;
              //縮放設置canvas畫(huà)布大小,防止筆跡錯位
              canvas.width = res[0].width * dpr;
              canvas.height = res[0].height * dpr;
              ctx.scale(dpr, dpr);
              ctx.lineJoin="round";
              this.setData({ctx});
            });
      
            query.select('.handCenter').boundingClientRect(rect => {
                console.log('rect', rect);
                this.setData({
                    canvasWidth:rect.width,
                    canvasHeight:rect.height
                });
            }).exec();
        },
       //省略以下代碼......
    });
    

    3. 點(diǎn)擊時(shí)

    Component({
     //省略以上代碼...
     methods: {
                scaleStart(event){
                    if (event.type != 'touchstart') return false;
                    let currentPoint = {
                        x: event.touches[0].x,
                        y: event.touches[0].y
                    }
                    this.drawCircle(currentPoint);
                    this.setData({startPoint:currentPoint});
              },
                drawCircle(point){//這里負責點(diǎn)
                    let ctx = this.data.ctx;
                    ctx.beginPath();
                    ctx.fillStyle = this.data.lineColor;
                //筆跡粗細由圓的大小決定
                    ctx.arc(point.x, point.y, this.data.radius, 0 , 2 * Math.PI);
                    ctx.fill();
                    ctx.closePath();
              },
              //省略以下代碼...
     }
    })
    

    4. 簽名時(shí)

    Component({
      //省略以上代碼
      methods:{
     drawLine(sourcePoint, targetPoint){
                let ctx = this.data.ctx;
                this.drawCircle(targetPoint);
                ctx.beginPath();
                ctx.strokeStyle = this.data.lineColor;
                ctx.lineWidth = this.data.radius * 2;//這里乘2是因為線(xiàn)條的粗細要和圓的直徑相等
                ctx.moveTo(sourcePoint.x, sourcePoint.y);
                ctx.lineTo(targetPoint.x, targetPoint.y);
                ctx.stroke();
                ctx.closePath();
              },
              clearCanvas(){//清空畫(huà)布
                let ctx = this.data.ctx;
                ctx.rect(0, 0, this.data.canvasWidth, this.data.canvasHeight);
                ctx.fillStyle = '#FFFFFF';
                ctx.fill();
              }
      }
    })
    

    三、總結

    這個(gè)手寫(xiě)簽名僅僅是為了業(yè)務(wù)應急使用。如果要優(yōu)化的話(huà),可以從筆鋒模擬和筆跡模擬中入手。只不過(guò)要解決在實(shí)時(shí)模擬過(guò)程中卡頓的問(wèn)題。

    到此這篇關(guān)于微信小程序實(shí)現簡(jiǎn)單手寫(xiě)簽名組件的文章就介紹到這了,更多相關(guān)微信小程序手寫(xiě)簽名組件內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

    dem
    国语自产精品视频在线区| 亚洲中文久久精品无码照片| 啊轻点灬大巴太粗太长视频| JAPANESE高潮喷水| 午夜精品影视国产一区在线麻豆| 国产卡一卡二卡三|