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)文章
vue實(shí)現(xiàn)動(dòng)態(tài)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)動(dòng)態(tài)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09快速將Vue項(xiàng)目升級(jí)到webpack3的方法步驟
這篇文章主要給大家介紹了關(guān)于如何快速將Vue項(xiàng)目升級(jí)到webpack3的方法步驟文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09淺談mvvm-simple雙向綁定簡(jiǎn)單實(shí)現(xiàn)
本篇文章主要介紹了淺談mvvm-simple雙向綁定簡(jiǎn)單實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04基于Vue+elementUI實(shí)現(xiàn)動(dòng)態(tài)表單的校驗(yàn)功能(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式)
這篇文章主要介紹了Vue+elementUI的動(dòng)態(tài)表單的校驗(yàn)(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04vue 動(dòng)態(tài)創(chuàng)建組件的兩種方法
這篇文章主要介紹了vue 動(dòng)態(tài)創(chuàng)建組件的兩種方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12