VUE使用canvas實(shí)現(xiàn)簽名組件
本文實(shí)例為大家分享了VUE使用canvas實(shí)現(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: {
? ? // 畫(huà)布寬度
? ? width: {
? ? ? type: Number,
? ? ? default: window.innerWidth
? ? },
? ? // 畫(huà)布高度
? ? height: {
? ? ? type: Number,
? ? ? default: 500
? ? },
? ? // 筆觸半徑
? ? radius: {
? ? ? type: Number,
? ? ? default: 10
? ? },
? ? // 筆觸顏色
? ? color: {
? ? ? type: String,
? ? ? default: '#000'
? ? },
? ? // 畫(huà)布填充背景
? ? 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)畫(huà)圓
? ? ? ctx.fill();
? ? },
? ? // 保存圖片
? ? save (name = '簽名圖片') {
? ? ? let imgBase64 = this.canvas.toDataURL('image/png'); // 獲取截圖base64, 1表示質(zhì)量(無(wú)損壓縮)
? ? ? let a = document.createElement('a');
? ? ? a.download = name + '.png'; // 必須要設(shè)置download屬性才能夠直接下載base64圖片
? ? ? a.href = imgBase64;
? ? ? a.click(); // 觸發(fā)點(diǎn)擊,起到下載效果
? ? },
? ? // 清除畫(huà)布
? ? 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)識(shí)
? ? ? ctx.fillStyle = this.fillStyle;
? ? ? ctx.fillRect(0, 0, this.width, this.height);
? ? ? // 事件兼容PC 移動(dòng)端
? ? ? 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>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
axios上傳文件錯(cuò)誤:Current?request?is?not?a?multipart?request
最近工作中使用vue上傳文件的時(shí)候遇到了個(gè)問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于axios上傳文件錯(cuò)誤:Current?request?is?not?a?multipart?request解決的相關(guān)資料,需要的朋友可以參考下2023-01-01
如何使用el-table實(shí)現(xiàn)純前端導(dǎo)出(適用于el-table任意表格)
我們?nèi)粘W鲰?xiàng)目,特別是后臺(tái)管理系統(tǒng),常常需要導(dǎo)出excel文件,這篇文章主要給大家介紹了關(guān)于如何使用el-table實(shí)現(xiàn)純前端導(dǎo)出的相關(guān)資料,本文適用于el-table任意表格,需要的朋友可以參考下2024-03-03
簡(jiǎn)單聊一聊vue中data的代理和監(jiān)聽(tīng)
這篇文章主要給大家介紹了關(guān)于vue中data的代理和監(jiān)聽(tīng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-09-09
Vue腳手架學(xué)習(xí)之項(xiàng)目創(chuàng)建方式
這篇文章主要給大家介紹了關(guān)于Vue腳手架學(xué)習(xí)之項(xiàng)目創(chuàng)建方式的相關(guān)資料,文中通過(guò)介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
vue使用showdown并實(shí)現(xiàn)代碼區(qū)域高亮的示例代碼
這篇文章主要介紹了vue使用showdown并實(shí)現(xiàn)代碼區(qū)域高亮的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
vue 項(xiàng)目接口管理的實(shí)現(xiàn)
在vue開(kāi)發(fā)中,會(huì)涉及到很多接口的處理,當(dāng)項(xiàng)目足夠大時(shí),就需要定義規(guī)范統(tǒng)一的接口,本文就來(lái)介紹一下vue 項(xiàng)目接口管理,具有一定的參考價(jià)值,感興趣的小伙伴可以一起來(lái)了解一下2019-01-01
Vue實(shí)現(xiàn)當(dāng)前頁(yè)面刷新的4種方法舉例
我們?cè)陂_(kāi)發(fā)vue的頁(yè)面的時(shí)候,有時(shí)候會(huì)遇到需要刷新當(dāng)前頁(yè)面功能,但是vue框架自帶的router是不支持刷新當(dāng)前頁(yè)面功能,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)當(dāng)前頁(yè)面刷新的4種方法,需要的朋友可以參考下2023-04-04

