vue 使用smooth-signature完成簽名

需求:用戶需要手寫簽名(需要帶筆鋒)

思路:可以使用smooth-signature實現(xiàn),越用力寫出的字越粗

如下是效果圖:


PixPin_2024-10-18_15-51-19.png
PixPin_2024-10-18_16-02-43.png

一排监、安裝 smooth-signature

yarn add smooth-signature或者npm install smooth-signature

二涉兽、封裝組件,方便復(fù)用

通過finally事件將簽字內(nèi)容傳遞出去脆侮。

<template>
  <div class="sign-finish">
    <div class="wrap1" v-show="isHorizontal">
      <p class="sign-title">請在區(qū)域內(nèi)簽字</p>
      <canvas class="canvas1" ref="canvas1" />
      <div class="actions">
        <van-button class="danger" @click="handleClear(1)">清除</van-button>
        <van-button class="warning" @click="revoked(1)">撤銷</van-button>
        <van-button class="primary" @click="handleFull">橫屏</van-button>
        <van-button class="success" @click="save(1)">保存</van-button>
      </div>
    </div>
    <div class="wrap2" v-show="!isHorizontal">
      <div class="actionsWrap">
        <div class="actions">
          <van-button class="danger" @click="handleClear(2)">清除</van-button>
          <van-button class="warning" @click="revoked(2)">撤銷</van-button>
          <van-button class="primary" @click="handleFull">豎屏</van-button>
          <van-button class="success" @click="save(2)">保存</van-button>
        </div>
      </div>
      <canvas  class="canvas" ref="canvas2" />
    </div>
  </div>
</template>
 
<script>
import SmoothSignature from "smooth-signature";
import { Toast } from "vant";
export default {
  name: "signatureMenu",
  data() {
    return {
      isHorizontal: false,
      signature1: null,
      signature2: null,
    };
  },
  mounted() {
    this.initSignature();
  },
  methods: {
    initSignature() {
      const canvas1 = this.$refs.canvas1;
      const canvas2 = this.$refs.canvas2;
      const options1 = {
        width: window.innerWidth - 30,
        height: 200,
        minWidth: 2,
        maxWidth: 6,
        openSmooth: true,
        bgColor: "#f6f6f6",
      };
      const options2 = {
        width: window.innerWidth - 120,
        height: window.innerHeight - 80,
        minWidth: 3,
        maxWidth: 10,
        openSmooth: true,
        bgColor: "#f6f6f6",
      };
      this.signature1 = new SmoothSignature(canvas1, options1);
      this.signature2 = new SmoothSignature(canvas2, options2);
    },
 
    handleClear() {
      this.signature1.clear();
      this.signature2.clear();
    },
    revoked(type = 1) {
      this[`signature${type}`].undo();
    },
    handleFull() {
      this.isHorizontal = !this.isHorizontal;
      this.handleClear();
    },
    save(type = 1) {
      const isEmpty = this[`signature${type}`].isEmpty();
      if (isEmpty) {
        Toast.fail(`請先完成簽字`);
        return;
      }
      // const pngUrl = this[`signature${type}`].getPNG();
      const pngUrl = this[`signature${type}`].toDataURL();
      this.$emit("finally", pngUrl);
      console.log(pngUrl);
    },
  },
};
</script>
 
<style lang="scss" scoped>
.sign-finish {
  text-align: center;
  height: 100%;
  box-sizing: border-box;
  width: 100%;
  button {
    height: 32px;
    padding: 0 8px;
    font-size: 12px;
    border-radius: 2px;
  }
  .danger {
    color: #fff;
    background: #ee0a24;
    border: 1px solid #ee0a24;
  }
  .warning {
    color: #fff;
    background: #ff976a;
    border: 1px solid #ff976a;
  }
  .primary {
    color: #fff;
    background: #1989fa;
    border: 1px solid #1989fa;
  }
  .success {
    color: #fff;
    background: #07c160;
    border: 1px solid #07c160;
  }
  canvas {
    border-radius: 10px;
    border: 2px dashed #ccc;
  }
  .wrap1 {
    height: 100%;
    width: 96%;
    margin: auto;
    margin-top: 100px;
    .actions {
      display: flex;
      justify-content: space-around;
    }
  }
  .wrap2 {
    padding: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    .actionsWrap {
      width: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .canvas {
      flex: 1;
      margin: auto;
    }
    .actions {
      margin-right: 10px;
      white-space: nowrap;
      transform: rotate(90deg);
      button {
        margin-right: 20px;
      }
    }
  }
}
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末瞎领,一起剝皮案震驚了整個濱河市泌辫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌九默,老刑警劉巖震放,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異驼修,居然都是意外死亡殿遂,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進(jìn)店門乙各,熙熙樓的掌柜王于貴愁眉苦臉地迎上來墨礁,“玉大人,你說我怎么就攤上這事耳峦《骶玻” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵蹲坷,是天一觀的道長驶乾。 經(jīng)常有香客問我,道長循签,這世上最難降的妖魔是什么级乐? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮县匠,結(jié)果婚禮上唇牧,老公的妹妹穿的比我還像新娘。我一直安慰自己聚唐,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布腔召。 她就那樣靜靜地躺著杆查,像睡著了一般。 火紅的嫁衣襯著肌膚如雪臀蛛。 梳的紋絲不亂的頭發(fā)上亲桦,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天崖蜜,我揣著相機(jī)與錄音,去河邊找鬼客峭。 笑死豫领,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的舔琅。 我是一名探鬼主播等恐,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼备蚓!你這毒婦竟也來了课蔬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤郊尝,失蹤者是張志新(化名)和其女友劉穎二跋,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體流昏,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扎即,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了况凉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谚鄙。...
    茶點(diǎn)故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖茎刚,靈堂內(nèi)的尸體忽然破棺而出襟锐,到底是詐尸還是另有隱情,我是刑警寧澤膛锭,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布粮坞,位于F島的核電站,受9級特大地震影響初狰,放射性物質(zhì)發(fā)生泄漏莫杈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一奢入、第九天 我趴在偏房一處隱蔽的房頂上張望筝闹。 院中可真熱鬧,春花似錦腥光、人聲如沸关顷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽议双。三九已至,卻和暖如春捉片,著一層夾襖步出監(jiān)牢的瞬間平痰,已是汗流浹背汞舱。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宗雇,地道東北人昂芜。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像赔蒲,于是被迫代替她去往敵國和親泌神。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容