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

微信小程序實現(xiàn)手寫簽名的示例代碼

 更新時間:2022年02月18日 12:00:00   作者:小小蚊子  
這篇文章主要和大家分享一個微信小程序的示例代碼,可以實現(xiàn)手寫簽名的效果。文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下

在微信小程序上實現(xiàn)手寫簽名,獲取canvascontext新版本和舊版本有點坑,新版本在獲取canvas后如果頁面有滑動,則簽名坐標出現(xiàn)異常(在微信開發(fā)者工具上會出現(xiàn)2022-2-17),但是在真機上即使滑動也不會出現(xiàn)異常,為了防止出現(xiàn)問題,暫時使用舊版本獲取canvascontext

1.效果圖

2.相關代碼

canvas代碼

新版2d canvas

<canvas
  id="canvas"
  class="canvas"
  canvas-id="canvas"
  type="2d"
  :disable-scroll="true"
  @touchstart="handleTouchStart"
  @touchmove="handleTouchMove"
  @touchend="handleTouchEnd"
  @touchcancel="handleTouchCancel"
></canvas>

舊版canvas

<canvas
  class="canvas"
  canvas-id="canvas"
  :disable-scroll="true"
  @touchstart="handleTouchStart"
  @touchmove="handleTouchMove"
  @touchend="handleTouchEnd"
  @touchcancel="handleTouchCancel"
></canvas>

js相關

獲取新版2d canvas對象

const query = uni.createSelectorQuery().in(this);
query.select('.canvas').node(res => {
  const {
      _width,
      _height
  } = res.node;
  
  /* 獲取canvas wxml節(jié)點 */
  this.canvas = res.node;
  this.canvasWidth = _width;
  this.canvasHeight = _height;
  /* 獲取canvas 2dcontext */
  this.canvasContext= this.canvas.getContext('2d');
  
  /* 縮放設置canvas畫布大小,防止筆跡錯位 */
  const ratio = wx.getSystemInfoSync().pixelRatio;
  this.canvas.width = this.canvasWidth * ratio;
  this.canvas.height = this.canvasHeight * ratio;
  this.canvasContext.scale(ratio, ratio);
  
  /* 設置線條顏色 */
  this.canvasContext.strokeStyle = '#2A2A2A';
  /* 設置線條粗細 */
  this.canvasContext.lineWidth = 4;
  /* 設置線條的結束端點樣式 */
  this.canvasContext.lineCap = 'round';
}).exec()

縮放設置canvas畫布大小,防止筆跡錯位,這點和頁面滑動沒有關系,不設置也會導致坐標錯位

const ratio = wx.getSystemInfoSync().pixelRatio;
this.canvas.width = this.canvasWidth * ratio;
this.canvas.height = this.canvasHeight * ratio;
this.canvasContext.scale(ratio, ratio);

舊版本獲取canvas

this.canvasContext = uni.createCanvasContext('canvas', this);
/* 設置線條顏色 */
this.canvasContext.setStrokeStyle('#2A2A2A');
/* 設置線條粗細 */
this.canvasContext.setLineWidth(4);
/* 設置線條的結束端點樣式 */
this.canvasContext.setLineCap('round');

簽名js方法,新版本和舊版本只有一個draw的區(qū)別,新版本不需要使用draw方法

/* 觸摸開始 */
handleTouchStart(e) {
  this.drawStartX = e.changedTouches[0].x;
  this.drawStartY = e.changedTouches[0].y;
    this.canvasContext.beginPath();
},
/* 觸摸移動 */
handleTouchMove(e) {
    /* 記錄當前位置 */
    const tempX = e.changedTouches[0].x;
    const tempY = e.changedTouches[0].y;

    /* 畫線 */
    this.canvasContext.moveTo(this.drawStartX, this.drawStartY);
    this.canvasContext.lineTo(tempX, tempY);
    this.canvasContext.stroke();

    /* 舊版draw方法,新版本不需要draw */
    this.canvasContext.draw(true);

    /* 重新記錄起始位置 */
    this.drawStartX = tempX;
    this.drawStartY = tempY;
},
/* 觸摸結束 */
handleTouchEnd(e) {
    this.canvasContext.save();
},
/* 觸摸取消 */
handleTouchCancel(e) {
    this.canvasContext.save();
},
/* 清空畫布 */
clearCanvas() {
    this.canvasContext.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
},

canvas生成本地圖片(我這里封裝了組件,需要傳入this防止this指向異常)

/* 生成簽名圖片 */
generateSignImage() {
    return new Promise((resolve, reject) => {
        uni.canvasToTempFilePath({
          x: 0,
          y: 0,
          // canvas: this.canvas, // 新版
          canvasId: 'canvas', // 舊版使用id
          width: this.canvasWidth,
          height: this.canvasHeight,
          destWidth: this.canvasWidth,
          destHeight: this.canvasHeight,
          fileType: 'png',
          quality: 1,
          success: res => {
              resolve(res.tempFilePath)
          },
          fail: err => {
              reject(err);
          }
        }, this)
    })
},

新版本的canvas主要是canvas wxml節(jié)點和canvas context中做了區(qū)分,舊版則只有一個canvas context就可以做全部的操作,在生成圖片時,新版本是傳入wxml對象,舊版本則是傳入唯一canvasId,新版本canvas取消了draw方法

以上就是微信小程序實現(xiàn)手寫簽名的示例代碼的詳細內容,更多關于小程序手寫簽名的資料請關注腳本之家其它相關文章!

相關文章

  • layui之數(shù)據(jù)表格--與后臺交互獲取數(shù)據(jù)的方法

    layui之數(shù)據(jù)表格--與后臺交互獲取數(shù)據(jù)的方法

    今天小編就為大家分享一篇layui之數(shù)據(jù)表格--與后臺交互獲取數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09
  • php對mongodb的擴展(小試牛刀)

    php對mongodb的擴展(小試牛刀)

    本文主要講解php連接、操作mongodb,有需求的朋友可以參考下
    2012-11-11
  • js 鍵盤記錄實現(xiàn)(兼容FireFox和IE)

    js 鍵盤記錄實現(xiàn)(兼容FireFox和IE)

    用js實現(xiàn)鍵盤記錄,要關注瀏覽器的三種按鍵事件類型,即keydown,keypress和keyup,它們分別對應onkeydown、onkeypress和onkeyup這三個事件句柄。一個典型的按鍵會產(chǎn)生所有這三種事件,依次是keydown,keypress,然后是按鍵釋放時候的keyup。
    2010-02-02
  • D3.js實現(xiàn)餅圖,環(huán)圖,玫瑰圖的繪制

    D3.js實現(xiàn)餅圖,環(huán)圖,玫瑰圖的繪制

    這篇文章主要為大家介紹了如何利用D3.js中的d3.pie和d3.arc實現(xiàn)餅圖、環(huán)圖和玫瑰圖的繪制,文中的實現(xiàn)方法講解詳細,感興趣的可以嘗試一下
    2022-11-11
  • 使用Webpack打包的流程分析

    使用Webpack打包的流程分析

    Webpack 是一個前端資源加載/打包工具。它將根據(jù)模塊的依賴關系進行靜態(tài)分析,然后將這些模塊按照指定的規(guī)則生成對應的靜態(tài)資源,這篇文章主要介紹了使用Webpack打包的操作方法,需要的朋友可以參考下
    2022-12-12
  • 在 javascript 中如何快速獲取數(shù)組指定位置的元素

    在 javascript 中如何快速獲取數(shù)組指定位置的元素

    這篇文章主要介紹了在 javascript 中快速獲取數(shù)組指定位置的元素,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • js繪制圓形和矩形的方法

    js繪制圓形和矩形的方法

    這篇文章主要介紹了js繪制圓形和矩形的方法,涉及javascript鼠標事件及頁面元素樣式的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • javascript 繼承實現(xiàn)方法

    javascript 繼承實現(xiàn)方法

    javascript的繼承機制并不是明確規(guī)定的,而是通過模仿實現(xiàn)的,意味著繼承不是由解釋程序處理,開發(fā)者有權決定最適合的繼承方式.
    2009-08-08
  • 基于JavaScript實現(xiàn)的折半查找算法示例

    基于JavaScript實現(xiàn)的折半查找算法示例

    這篇文章主要介紹了基于JavaScript實現(xiàn)的折半查找算法,結合實例形式分析了折半查找的原理、操作步驟及javascript實現(xiàn)折半查找的相關操作技巧與注意事項,需要的朋友可以參考下
    2017-04-04
  • 推薦閱讀的js快速判斷IE瀏覽器(兼容IE10與IE11)

    推薦閱讀的js快速判斷IE瀏覽器(兼容IE10與IE11)

    這篇文章主要介紹了推薦閱讀的js快速判斷IE瀏覽器(兼容IE10與IE11),因為ie11取消了很多低版本的判斷,很多js都需要修改了,這里簡單介紹下需要的朋友可以參考下
    2015-12-12

最新評論