[前端] VUE使用canvas实现签名组件

1968 0
王子 2022-10-21 16:03:49 | 显示全部楼层 |阅读模式
本文实例为大家分享了VUE使用canvas实现签名组件的具体代码,供大家参考,具体内容如下
效果如下:

  1. <template>
  2. <div class="sign">
  3.   <div class="content">
  4.    <canvas id="canvas" :width="width" :height="height"/>
  5.   </div>
  6.   <div class="btn">
  7.    <button @click="clearCanvas()">清除</button>
  8.    <button @click="save()">保存</button>
  9.   </div>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'App',
  15. data () {
  16.   return {
  17.   };
  18. },
  19. props: {
  20.   // 画布宽度
  21.   width: {
  22.    type: Number,
  23.    default: window.innerWidth
  24.   },
  25.   // 画布高度
  26.   height: {
  27.    type: Number,
  28.    default: 500
  29.   },
  30.   // 笔触半径
  31.   radius: {
  32.    type: Number,
  33.    default: 10
  34.   },
  35.   // 笔触颜色
  36.   color: {
  37.    type: String,
  38.    default: '#000'
  39.   },
  40.   // 画布填充背景
  41.   fillStyle: {
  42.    type: String,
  43.    default: '#ccc'
  44.   }
  45. },
  46. created () {
  47. },
  48. mounted (){
  49.   this.int();
  50. },
  51. methods: {
  52.   // 绘制涂擦效果圆形
  53.   // @param { integer } 圆心的x坐标
  54.   // @param { integer } 圆心的y坐标
  55.   // @param { integer } 圆心半径
  56.   // @param { string } 填充的颜色
  57.   fillCircle (ctx, x, y, radius, fillColor) {
  58.    ctx.fillStyle = fillColor || this.color;
  59.    ctx.beginPath();
  60.    ctx.moveTo(x, y);
  61.    ctx.arc(x, y, radius, 0, Math.PI * 2, false); // 标准画圆
  62.    ctx.fill();
  63.   },
  64.   // 保存图片
  65.   save (name = '签名图片') {
  66.    let imgBase64 = this.canvas.toDataURL('image/png'); // 获取截图base64, 1表示质量(无损压缩)
  67.    let a = document.createElement('a');
  68.    a.download = name + '.png'; // 必须要设置download属性才能够直接下载base64图片
  69.    a.href = imgBase64;
  70.    a.click(); // 触发点击,起到下载效果
  71.   },
  72.   // 清除画布
  73.   clearCanvas () {
  74.    let canvas = this.canvas;
  75.    canvas.getContext('2d').fillStyle = this.fillStyle;
  76.    canvas.getContext('2d').fillRect(0, 0, this.width, this.height);
  77.   },
  78.   // 数据初始化
  79.   int () {
  80.    this.canvas = document.querySelector('#canvas');
  81.    let ctx = this.canvas.getContext('2d');
  82.    let move = false; // 按下标识
  83.    ctx.fillStyle = this.fillStyle;
  84.    ctx.fillRect(0, 0, this.width, this.height);
  85.    // 事件兼容PC 移动端
  86.    let eventStart = 'ontouchstart' in document ? 'touchstart' : 'mousedown';
  87.    let eventMove = 'ontouchmove' in document ? 'touchmove' : 'mousemove';
  88.    let eventEnd = 'ontouchend' in document ? 'touchend' : 'mouseup';
  89.    this.canvas.addEventListener(eventStart, (e) => {
  90.     console.log(e);
  91.     let sx = e.touches ? e.touches[0].pageX : e.pageX;
  92.     let sy = e.touches ? e.touches[0].pageY : e.pageY;
  93.     move = true;
  94.     this.fillCircle(ctx, sx, sy, this.radius);
  95.    }, false);
  96.    this.canvas.addEventListener(eventMove, (e) => {
  97.     let sx = e.touches ? e.touches[0].pageX : e.pageX;
  98.     let sy = e.touches ? e.touches[0].pageY : e.pageY;
  99.     move && this.fillCircle(ctx, sx, sy, this.radius);
  100.    }, false);
  101.    this.canvas.addEventListener(eventEnd, (e) => {
  102.     move = false;
  103.    }, false);
  104.   }
  105. }
  106. };
  107. </script>
  108. <style lang="less" scoped>
  109. .sign{
  110. .btn {
  111.   padding:10px;
  112.   button {
  113.    height: 50px;
  114.    width:100%;
  115.    margin-bottom:10px;
  116.    font-size: 20px;
  117.   }
  118. }
  119. }
  120. </style>
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行