基于Vue實(shí)現(xiàn)手勢(shì)簽名
本文實(shí)例為大家分享了基于Vue實(shí)現(xiàn)手勢(shì)簽名的具體代碼,供大家參考,具體內(nèi)容如下
廢話不多說,直接上效果圖&源碼

代碼如下
1. template
<template>
? <main class="hand-sign-page">
? ? <header class="sign-head">請(qǐng)?jiān)谙路絽^(qū)域內(nèi)簽名</header>
? ? <div id="signContain" :style="{'--back': background}"></div>
? ? <footer class="sign-foot">
? ? ? <button @click="clearCanvas">清除</button>
? ? ? <button @click="saveCanvas">保存</button>
? ? </footer>
? ? <img v-show="vaildSign" class="sign-img" :src="vaildSign" alt=" "/>
? </main>
</template>2. js
<script>
? export default {
? ? name: "hand-sign",
? ? data() {
? ? ? return {
? ? ? ? domEl: null,//繪制canvas的父級(jí)div
? ? ? ? canvas: null,//畫布
? ? ? ? cxt: null,//繪畫環(huán)境
? ? ? ? linewidth:1,//線條粗細(xì),選填
? ? ? ? color:"black",//線條顏色,選填
? ? ? ? background:"aliceblue",//線條背景,選填
? ? ? ? vaildSign: null
? ? ? }
? ? },
? ??
? ? mounted() {
? ? ? this.initCanvas();
? ? },
? ? methods: {
? ? ? initCanvas() {
? ? ? ? this.domEl = document.getElementById("signContain");
? ? ? ? this.canvas = document.createElement("canvas");
? ? ? ? this.domEl.appendChild(this.canvas);
? ? ? ? this.cxt = this.canvas.getContext("2d");
? ? ? ? this.canvas.width = this.domEl.clientWidth;
? ? ? ? this.canvas.height = this.domEl.clientHeight;
? ? ? ? this.cxt.fillStyle = this.background;
? ? ? ? this.cxt.fillRect(0, 0, this.canvas.width, this.canvas.height);
? ? ? ? this.cxt.strokeStyle = this.color;
? ? ? ? this.cxt.lineWidth = this.linewidth;
? ? ? ? //this.cxt.lineCap = "round";
? ? ? ? this.clearCanvas();//先清理下
? ? ? ? //開始繪制
? ? ? ? this.canvas.addEventListener("touchstart", (e) => {
? ? ? ? ? this.cxt.beginPath();
? ? ? ? ? this.cxt.moveTo(e.changedTouches[0].pageX - e.target.offsetLeft, e.changedTouches[0].pageY - e.target.offsetTop);
? ? ? ? }, false);
? ? ? ? //繪制中
? ? ? ? this.canvas.addEventListener("touchmove", (e)=> {
? ? ? ? ? this.cxt.lineTo(e.changedTouches[0].pageX - e.target.offsetLeft, e.changedTouches[0].pageY - e.target.offsetTop);
? ? ? ? ? this.cxt.stroke();
? ? ? ? }, false);
? ? ? ??
? ? ? ? //結(jié)束繪制
? ? ? ? this.canvas.addEventListener("touchend", (e)=> {
? ? ? ? ? this.cxt.closePath();
? ? ? ? }, false);
? ? ? },
? ? ? //清除畫布
? ? ? clearCanvas() {
? ? ? ? this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
? ? ? },
? ? ? //保存畫布
? ? ? saveCanvas() {
? ? ? ? console.log(this.blankCanvas());//檢查畫布是否為空白
? ? ? ? if(this.blankCanvas()) {
? ? ? ? ? window.alert('請(qǐng)簽名');
? ? ? ? }else {
? ? ? ? ? this.vaildSign = this.canvas.toDataURL();
? ? ? ? }
? ? ? },
? ? ? //canvas非空驗(yàn)證
? ? ? blankCanvas() {
? ? ? ? let blank = document.createElement('canvas');//系統(tǒng)獲取一個(gè)空canvas對(duì)象
? ? ? ? blank.width = this.canvas.width;
? ? ? ? blank.height = this.canvas.height;
? ? ? ? return this.canvas.toDataURL() == blank.toDataURL();//比較值相等則為空
? ? ? },
? ? }
? }
</script>3. css
<style lang="less" scoped>
? .hw(@h, @w: @h) {
? ? height: @h;
? ? width: @w
? }
? .hand-sign-page{
? ? background-color: #fff;
? ? .sign-head {
? ? ? text-align: center;
? ? ? padding: 10px;
? ? ? border-bottom: 1px solid #ebebeb;
? ? ? color: #666;
? ? ? font-size: 14px;
? ? }
? ? #signContain {
? ? ? .hw(400px, 100%);
? ? ? background-color: var(--back);
? ? }
? ? .sign-foot{
? ? ? display: flex;
? ? ? justify-content: center;
? ? ? margin: 15px;
? ? ? button {
? ? ? ? margin: 0 15px;
? ? ? ? padding: 10px 20px;
? ? ? ? background-color: #ddd;
? ? ? ? border-radius: 5px;
? ? ? ? &:active {
? ? ? ? ? background-color: #efefef;
? ? ? ? }
? ? ? }
? ? }
? ? .sign-img {
? ? ? .hw(100px, 200px);
? ? ? display: block;
? ? ? margin: 10px auto;
? ? }
? }
</style>以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue?vue-touch移動(dòng)端手勢(shì)詳解
- atom-design(Vue.js移動(dòng)端組件庫)手勢(shì)組件使用教程
- 詳解IOS微信上Vue單頁面應(yīng)用JSSDK簽名失敗解決方案
- vue+element加入簽名效果(移動(dòng)端可用)
- 使用vue實(shí)現(xiàn)手寫簽名功能
- vue 使用 canvas 實(shí)現(xiàn)手寫電子簽名
- vue移動(dòng)端使用canvas簽名的實(shí)現(xiàn)
- VUE解決微信簽名及SPA微信invalid signature問題(完美處理)
- 使用vue實(shí)現(xiàn)一個(gè)電子簽名組件的示例代碼
- 詳解Vue開發(fā)微信H5微信分享簽名失敗問題解決方案
相關(guān)文章
詳解Vue實(shí)戰(zhàn)指南之依賴注入(provide/inject)
這篇文章主要介紹了詳解Vue實(shí)戰(zhàn)指南之依賴注入(provide/inject),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Vue結(jié)合SignalR實(shí)現(xiàn)前后端實(shí)時(shí)消息同步
這篇文章主要為大家詳細(xì)介紹了Vue結(jié)合SignalR實(shí)現(xiàn)前后端實(shí)時(shí)消息同步,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
element的el-table自定義最后一行的實(shí)現(xiàn)代碼
最后一行要顯示一些其他結(jié)果,用的是element? ui 自帶的數(shù)據(jù)總計(jì)的屬性;返回一個(gè)數(shù)組,會(huì)按下標(biāo)進(jìn)行展示,這篇文章主要介紹了element的el-table自定義最后一行的實(shí)現(xiàn)代碼,需要的朋友可以參考下2024-03-03
Vue.js實(shí)現(xiàn)一個(gè)自定義分頁組件vue-paginaiton
這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)一個(gè)自定義分頁組件vue-paginaiton的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
vue上傳文件formData入?yún)榭?接口請(qǐng)求500的解決
這篇文章主要介紹了vue上傳文件formData入?yún)榭?接口請(qǐng)求500的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Vue v-for中:key中item.id與Index使用的區(qū)別解析
這篇文章主要介紹了Vue v-for中:key中item.id與Index使用的區(qū)別解析,推薦使用【:key="item.id"】而不是將數(shù)組下標(biāo)當(dāng)做唯一標(biāo)識(shí),前者能做到全部復(fù)用,本文給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2024-02-02
vue實(shí)現(xiàn)學(xué)生錄入系統(tǒng)之添加刪除功能
本文給大家?guī)硪粋€(gè)小案例基于vue實(shí)現(xiàn)學(xué)生錄入系統(tǒng)功能,代碼簡單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07

