uni开发手写板功能 弹框 完整代码 舒适应用!!!

这个
成品展示如上图所示

首先在项目的componts文件夹里引入cc-signature.vue


```javascript
<template>
	<view v-if="canvasVisiable">
		<view class="canvas-container">
			<canvas canvas-id="canvas" id="canvas" :disable-scroll="true" style="width: 100%; height: 200px;background-color: #FFFFFF;"
				@touchstart="handleTouchStart($event)" @touchmove="handleTouchMove($event)" @touchend="handleTouchEnd($event)"
				@touchcancel="handleEnd($event)"></canvas>
		</view>
		<view class="btn-container">
			<button @click="reset()">重置</button>
			<button @click="handleConfirm()">确定</button>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			canvasVisiable: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				context: '',
				canvasData: []
			};
		},
		watch: {
			canvasVisiable() {
				this.context = uni.createCanvasContext('canvas');
				this.context.setLineWidth(3);
				this.context.setStrokeStyle("#000000");
				this.reset()
			},
			canvasData() {
				if(!this.canvasData.length){
					return
				}
				this.context.moveTo(this.canvasData[0].x, this.canvasData[0].y)
				for (let i = 0; i < this.canvasData.length; i++) {
					this.context.lineTo(this.canvasData[i].x, this.canvasData[i].y)
				}
				this.context.stroke()
				this.context.draw(true)
			}
		},
		methods: {
			reset() {
				this.context.draw()
				this.canvasData = []
			},
			hideModal() {
				this.$emit('update:dialogVisiable', false)
			},
			handleTouchStart(e) {
				console.log(e)
				this.canvasData = []
				const a = e.changedTouches[0]
				this.canvasData.push({
					x: a.x,
					y: a.y
				})
			},
			handleTouchMove(e) {
				const a = e.changedTouches[0]
				this.canvasData.push({
					x: a.x,
					y: a.y
				})
			},
			handleTouchEnd(e) {
				const a = e.changedTouches[0]
				this.canvasData.push({
					x: a.x,
					y: a.y
				})
			},
			handleEnd() {
				this.context.stroke()
				this.context.draw(true)
			},
			handleConfirm() {
				if(this.canvasData.length>0){
					uni.canvasToTempFilePath({
						canvasId: 'canvas',
						success: res => {
							this.$emit('success', res.tempFilePath)
						}
					})
				}else{
					uni.showToast({
						title: "请签字",
						icon: 'none',
						duration:1500
					});
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.canvas-container {
		width: 100%;
	}
	.btn-container {
		padding-bottom: 20rpx;
		display: flex;
		justify-content: space-around;
	}
</style>

之后在全局mian.js引入

//  画板
import signature from './components/cc-signature/cc-signature.vue'
Vue.component('signature',signature)
Vue.config.productionTip = false

之后在我们使用的页面里载入

<!-- 画布弹框 -->
		<uni-popup ref="signaturePopup" type="center" :custom="true" @change="closeSignature">
			<view class="signature-popup" :style="{ width: winWidth }">
				<view class="head">
					<view class="btn-close" @tap="hideSignature"><i class="iconfont iconweibiao45133"></i></view>
					签名
				</view>
				<view class="content"><signature :canvasVisiable="isShowSignature" @success="getImgs"></signature></view>
			</view>
		</uni-popup>

之后我们再加上弹框样式

.signature-popup {
		background-color: @bc-color;
		.head {
			position: relative;
			line-height: 44px;
			border-bottom: 1px solid @border-color;
			text-align: center;
			font-size: @fontSize;
			.btn-close {
				position: absolute;
				right: 0;
				top: 0;
				width: 44px;
				line-height: 44px;
				text-align: center;
			}
		}
	}

在页面需要用到的地方 用一个图片标签来接受

<!--  检查 -->
				<view class="uni-list-cell">
					<view class="uni-list-cell-navigate " @tap.stop="showSignature()">
						<view class="label label-check">检查人(签字)</view>
						<view class="content"><image v-if="signatureImg" :src="imgSrc" mode=""></image></view>
					</view>
				</view>

在data里定义的

//  画布
			signatureImg: '',
			//  控制显示和隐藏
			isShowSignature: false, //是否显示画布
			imgSrc: '',
			signaturePopup: false, //是否显示画布popup
			winWidth: '' // 这个是适应屏幕宽度的
//在onload里写的
onLoad() {
		this.winWidth = this.$SystemInfo.windowWidth - 30 + 'px';
	},

方法的话就是控制与隐藏 没有太大的变化 只是当我们用画布获取到的图片是base64的 所以我们需要转码

	// 显示弹窗
		showSignature() {
			var _self = this;
			this.$refs.signaturePopup.open();
			setTimeout(function() {
				_self.isShowSignature = true;
			}, 1);
		},
		// 隐藏弹窗
		hideSignature() {
			this.isShowSignature = false;
			this.$refs.signaturePopup.close();
		},
		closeSignature(res) {
			if (!res.show) {
				this.isShowSignature = false;
			}
		},
		getImgs(e) {
			this.hideSignature();
			this.signatureImg = e;
			let file = e; // 把整个base64给file
			var type = 'image/png'; // 定义图片类型(canvas转的图片一般都是png,也可以指定其他类型)
			let conversions = this.base64ToBlob(file, type); // 调用base64转图片方法
			this.imgSrc = URL.createObjectURL(conversions);
			console.log(this.imgSrc);
		},

转码方式

		getImgs(e) {
			this.hideSignature();
			this.signatureImg = e;
			let file = e; // 把整个base64给file
			var type = 'image/png'; // 定义图片类型(canvas转的图片一般都是png,也可以指定其他类型)
			let conversions = this.base64ToBlob(file, type); // 调用base64转图片方法
			this.imgSrc = URL.createObjectURL(conversions);
			console.log(this.imgSrc);
		},



	base64ToBlob: function(urlData, type) {
			let arr = urlData.split(',');
			let mime = arr[0].match(/:(.*?);/)[1] || type;
			// 去掉url的头,并转化为byte
			let bytes = window.atob(arr[1]);
			// 处理异常,将ascii码小于0的转换为大于0
			let ab = new ArrayBuffer(bytes.length);
			// 生成视图(直接针对内存):8位无符号整数,长度1个字节
			let ia = new Uint8Array(ab);
			for (let i = 0; i < bytes.length; i++) {
				ia[i] = bytes.charCodeAt(i);
			}
			return new Blob([ab], {
				type: mime
			});
		}

这是完整的一套签名体系 亲测没有任何bug 我们获取到的blob地址 发给后台处理即可 如有使用的小伙伴不懂得直接评论区留言 看到即回复

纵横笔手写板 型号:HS-01A 方便实用的手写文字输入系统 一、硬件参数: 1. 采用最新技术:无线有源电磁笔技术,可实现鼠标、手写笔双重功能; 2. 1024级压感技术 3. 识别区域:161×102mm 4. 外形尺寸:210×185mm 二、产品特点: 1. 感应精度高,使用寿命长; 2. 无线笔可随时插入笔槽充电,每次充电5秒钟,可连续使用10小时,免除更换电池的烦恼; 3. 超大感应区,使用更方便; 4. 感应面板超薄设计,完美体现人体工学理念,充分缓解长时间使用造成的手腕疲劳感; 5. 手写笔、鼠标功能自动切换; 6. 绘写通功能,可以写字、绘画; 7. 软件界面、功能简洁明了,易学易用,极易上手,尤其适合不熟悉电脑的用户。 8. 识别:①中文简体②中文繁体③日文④韩文⑤英文⑥数字; 9. 支持连续书写。 三、应用环境: 1. Pentium以上电脑 2. 64M以上内存 3. 200M以上硬盘 4. USB接口 5. Windows2000/XP/Vista操作系统 6. 杀毒软件建议使用国内、国际知名品牌,且口碑良好的,如:瑞星、金山毒霸、诺顿、卡巴斯基等。推荐使用以下杀毒软件: 永久免费:瑞星、金山毒霸、可牛、安天防线、AVG杀毒 收费软件:诺顿、卡巴斯基、趋势 本公司软件在以上的杀毒软件中均做过测试,可放心使用。 特别说明:360杀毒、360安全卫士会影响本公司手写识别软件的正常运行。不能使用。 四、硬件说明: 1. 手写板中间位置161×102mm区域为有效识别区; 2. 手写笔内置可充电电源系统,无需更换电池,终生免维护。不用时,可插入充电座上。充电约5秒钟,笔可连续使用10小时(首次使用需充电约1分钟); 3. 在使用时,压下笔尖,接触板面,手写笔是书写功能。轻轻放在板面上或稍离开板面1-3毫米为鼠标功能; 4. 当笔内电源电压较低时,显示屏上的笔尖或光标会抖动,这时,要将笔插入笔槽充电; 5. 手写笔相当于一个全功能鼠标:笔尖相当于鼠标左键,靠近笔尖的按键(下键)相当于鼠标中间键(滚轮),靠近上端的按键(上键)相当于鼠标右键; 6. 笔尖双击时(相当于鼠标左键双击),前后两次点击的位置要相同,否则不能起到双击的功能;也可先按上键,再用笔尖点击,这样也可以实现双击的功能; 7. 使用中,如出现连笔现象,请将笔移出感应区,然后再回到感应区,即可解除连笔; 8. 左撇子用户请先将电脑系统里的“鼠标”设置为“右键有效”(在“控制面板”里打开“鼠标”,钩选“鼠标键配置”)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值