VUE使用canvas實現(xiàn)簽名組件
更新時間:2022年07月13日 11:01:06 作者:#麻辣小龍蝦#
這篇文章主要為大家詳細(xì)介紹了VUE使用canvas實現(xiàn)簽名組件,兼容PC移動端,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了VUE使用canvas實現(xiàn)簽名組件的具體代碼,供大家參考,具體內(nèi)容如下
效果如下:
<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坐標(biāo) ? ? // @param { integer } 圓心的y坐標(biāo) ? ? // @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); // 標(biāo)準(zhǔn)畫圓 ? ? ? ctx.fill(); ? ? }, ? ? // 保存圖片 ? ? save (name = '簽名圖片') { ? ? ? let imgBase64 = this.canvas.toDataURL('image/png'); // 獲取截圖base64, 1表示質(zhì)量(無損壓縮) ? ? ? let a = document.createElement('a'); ? ? ? a.download = name + '.png'; // 必須要設(shè)置download屬性才能夠直接下載base64圖片 ? ? ? a.href = imgBase64; ? ? ? a.click(); // 觸發(fā)點擊,起到下載效果 ? ? }, ? ? // 清除畫布 ? ? clearCanvas () { ? ? ? let canvas = this.canvas; ? ? ? canvas.getContext('2d').fillStyle = this.fillStyle; ? ? ? canvas.getContext('2d').fillRect(0, 0, this.width, this.height); ? ? }, ? ? // 數(shù)據(jù)初始化 ? ? int () { ? ? ? this.canvas = document.querySelector('#canvas'); ? ? ? let ctx = this.canvas.getContext('2d'); ? ? ? let move = false; // 按下標(biāo)識 ? ? ? 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Vue+elementUI實現(xiàn)動態(tài)表單的校驗功能(根據(jù)條件動態(tài)切換校驗格式)
這篇文章主要介紹了Vue+elementUI的動態(tài)表單的校驗(根據(jù)條件動態(tài)切換校驗格式),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04