<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="vue.js"></script>
<style>
#canvas {
border: 1px dotted black;
position: relative;
top: 20px;
left: 20px;
}
</style>
</head>
<body>
<div id="app">
<canvas id="canvas" ref="canvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
</div>
<script>
let x, y = 0
let canvas_width = 320
let canvas_height = 180
let vm = new Vue({
el: "#app",
data: {
isDrawing: false,
qm_count: 0,
qm_data: null,
qm_image_url: '',
context: null,
rect: null,
},
methods: {
touchstart(e) {
e.preventDefault();
x = e.changedTouches[0].clientX - this.rect.left;
y = e.changedTouches[0].clientY - this.rect.top;
this.isDrawing = true;
},
touchmove(e) {
e.preventDefault();
this.qm_count++
if (this.isDrawing === true) {
this.drawLine(x, y, e.changedTouches[0].clientX - this.rect.left, e.changedTouches[0].clientY - this.rect.top);
x = e.changedTouches[0].clientX - this.rect.left;
y = e.changedTouches[0].clientY - this.rect.top;
}
},
touchend(e) {
e.preventDefault();
if (this.isDrawing === true) {
this.drawLine(x, y, e.changedTouches[0].clientX - this.rect.left, e.changedTouches[0].clientY - this.rect.top);
x = 0;
y = 0;
this.isDrawing = false;
}
},
drawLine(x1, y1, x2, y2) {
this.context.beginPath();
this.context.strokeStyle = 'black';
this.context.lineWidth = 5;
this.context.moveTo(x1, y1);
this.context.lineTo(x2, y2);
this.context.stroke();
this.context.closePath();
},
clear_canvas() {
this.qm_data = null
this.qm_image_url = ''
this.context.fillStyle = "rgb(128, 128, 128)";
this.context.fillRect (0, 0, canvas_width, canvas_height);
this.qm_count = 0
},
},
mounted() {
let canvas = this.$refs.canvas
canvas.width = canvas_width
canvas.height = canvas_height
this.context = canvas.getContext('2d')
this.rect = canvas.getBoundingClientRect()
this.clear_canvas()
}
})
</script>
</body>
</html>
手写签名(移动端)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 参考链接:https://blog.csdn.net/qq_33270001/article/details/10...
- https://blog.csdn.net/weixin_45895806/article/details/114...
- 效果图 基于vue,可以保存上传项目地址 vue-signature 实现 1.canvas画图,参考 jrain...
- 使用场景: 用户需要扫一个二维码, 弹出输入框,输入用户名, 生成相应用户责任状。 在责任状底下要有能电子签名功能...