基于vue如何制作签名组件,代码是什么
Admin 2022-08-09 群英技术资讯 558 次浏览
效果如下:
<template> <div class="sign"> <div class="content"> <canvas id="canvas" :width="width" :height="height"/> </div> <div class="btn"> <button @click="clearCanvas()">清除</button> <button @click="save()">保存</button> </div> </div> </template> <script> export default { name: 'App', data () { return { }; }, props: { // 画布宽度 width: { type: Number, default: window.innerWidth }, // 画布高度 height: { type: Number, default: 500 }, // 笔触半径 radius: { type: Number, default: 10 }, // 笔触颜色 color: { type: String, default: '#000' }, // 画布填充背景 fillStyle: { type: String, default: '#ccc' } }, created () { }, mounted () { this.int(); }, methods: { // 绘制涂擦效果圆形 // @param { integer } 圆心的x坐标 // @param { integer } 圆心的y坐标 // @param { integer } 圆心半径 // @param { string } 填充的颜色 fillCircle (ctx, x, y, radius, fillColor) { ctx.fillStyle = fillColor || this.color; ctx.beginPath(); ctx.moveTo(x, y); ctx.arc(x, y, radius, 0, Math.PI * 2, false); // 标准画圆 ctx.fill(); }, // 保存图片 save (name = '签名图片') { let imgBase64 = this.canvas.toDataURL('image/png'); // 获取截图base64, 1表示质量(无损压缩) let a = document.createElement('a'); a.download = name + '.png'; // 必须要设置download属性才能够直接下载base64图片 a.href = imgBase64; a.click(); // 触发点击,起到下载效果 }, // 清除画布 clearCanvas () { let canvas = this.canvas; canvas.getContext('2d').fillStyle = this.fillStyle; canvas.getContext('2d').fillRect(0, 0, this.width, this.height); }, // 数据初始化 int () { this.canvas = document.querySelector('#canvas'); let ctx = this.canvas.getContext('2d'); let move = false; // 按下标识 ctx.fillStyle = this.fillStyle; ctx.fillRect(0, 0, this.width, this.height); // 事件兼容PC 移动端 let eventStart = 'ontouchstart' in document ? 'touchstart' : 'mousedown'; let eventMove = 'ontouchmove' in document ? 'touchmove' : 'mousemove'; let eventEnd = 'ontouchend' in document ? 'touchend' : 'mouseup'; this.canvas.addEventListener(eventStart, (e) => { console.log(e); let sx = e.touches ? e.touches[0].pageX : e.pageX; let sy = e.touches ? e.touches[0].pageY : e.pageY; move = true; this.fillCircle(ctx, sx, sy, this.radius); }, false); this.canvas.addEventListener(eventMove, (e) => { let sx = e.touches ? e.touches[0].pageX : e.pageX; let sy = e.touches ? e.touches[0].pageY : e.pageY; move && this.fillCircle(ctx, sx, sy, this.radius); }, false); this.canvas.addEventListener(eventEnd, (e) => { move = false; }, false); } } }; </script> <style lang="less" scoped> .sign{ .btn { padding:10px; button { height: 50px; width:100%; margin-bottom:10px; font-size: 20px; } } } </style>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文我们来来接怎样实现更容易扩展的JavaScript进度管理方式,下文有很详细的原理介绍及测试,对大家学习和工作都有一定的参考价值,感兴趣的朋友就继续往下看吧。
这篇文章主要介绍的是 vue定义私有过滤器和基本使用,下面文章围绕vue定义私有过滤器的相关资料展开内容,需要的朋友可以参考一下,希望对大家有所帮助
Vue 最独特的特性之一,是其非侵入性的响应式系统。下面这篇文章主要给大家介绍了关于vue响应式原理与双向数据的相关资料,需要的朋友可以参考下
这篇文章主要给大家分享用jQuery怎样做一个九宫格抽奖功能的示例,九宫格抽奖功能在很多营销活动页面都能看到,还是比较常用的一个抽奖功能, 这篇主要是使用jQuery来实现的,文中代码具有一定的借鉴价值,感兴趣的朋友可以参考,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧。
这篇文章主要为大家详细介绍了js动态实现表格添加和删除操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008