vue中使用cavans对图片添加水印 和 使用html2canvas.js实现图片的截图功能
1,选择上传一张图片,使用cavans对其添加水印 我这里使用的是vant组件上传图片,你们也可以使用原生的input上传图片,然后拿到资源句柄; 效果图如下: 水印是在左下角 代码如下: 大家主要看afterRead(file) 方法即可,其中需要注意的是: 1,形参file是拿到的图片的base64格式和其他的一些信息; 2, 把信息给打 this.base64AddWaterMaker()这个方法进行操作; 3,canvas.getContext(‘2d’);创建了一个2d的画布; 4,img.onload()方法是一个异步加载的方法; 5,toDataURL()方法把图形转变成base64编码格式的png
<template>
<div>
<h1>cavans实现截图</h1>
<p>用户选择一张图片进行添加水印并截图</p>
<!-- <van-uploader :after-read="afterRead" />-->
<van-uploader :after-read="afterRead"> <van-button icon="plus" type="primary" size="small">上传文件</van-button></van-uploader>
<van-button type="primary" size="small" class="del" @click="delImg">删除图片</van-button>
<van-button type="primary" size="small" class="del" @click="screenShot">图片截图</van-button>
<van-button type="primary" size="small" class="del" @click="jumpTo">跳转静态路由</van-button>
<div class="preview" v-if="true">
<div class="perview-box">
<img :src="imgBase64" alt="" id="img" />
</div>
</div>
</div>
</template>
<script>
import html2canvas from '../../plugins/html2canvas.js';
export default {
components: {},
data() {
return {
imgBase64: '',
waterMakeConfig: {
font: 'microsoft yahei',
textArray: ['-新节', '2021/11/26 16:44'],
},
};
},
computed: {},
watch: {},
created() {},
mounted() {},
methods: {
afterRead(file) {
try {
this.base64AddWaterMaker(file.content, this.waterMakeConfig).then((res) => {
这里可以把添加水印后的文件传给后端,我这里只是举例并没有做
console.log("res",res);
});
} catch (error) {
console.log('error:', error);
}
},
base64AddWaterMaker(base64Img, waterMakeConfig) {
let _this = this;
return new Promise((resolve, reject) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
img.crossOrigin = 'Anonymous';
img.src = base64Img;
img.onload = function () {
console.log('img.width', img.width);
canvas.width = img.width;
canvas.height = img.height;
ctx.font = `50px Georgia`;
ctx.drawImage(img, 0, 0, img.width, img.height);
ctx.fillText(waterMakeConfig.textArray[0], img.width * 0.05, img.height * 0.8);
ctx.fillText(waterMakeConfig.textArray[1], img.width * 0.05, img.height * 0.85);
ctx.fillStyle = '#fff';
let resultBase64 = canvas.toDataURL('image/png');
if (!resultBase64) {
reject();
} else {
_this.imgBase64 = resultBase64;
resolve(resultBase64);
}
};
});
},
delImg() {
this.imgBase64 = null;
},
jumpTo() {
this.$router.push({ path: '/routers/123' });
},
screenShot() {
if(!this.imgBase64){
return alert("请先上传一张图片!")
}
const imgEl = document.getElementById('img');
html2canvas(imgEl, {
scale: window.devicePixelRat,
backgroundColor: null,
allowTaint: false,
useCORS: true,
taintTest: true,
}).then((canvas) => {
let url = canvas.toDataURL('image/png', 1.0);
var saveLink = document.createElement('a');
saveLink.href = url;
saveLink.download = 'downLoad.png';
saveLink.click();
});
},
},
};
</script>
<style lang="scss" src="./cavans.scss" scoped></style>
2,图片截图功能 2,1 图片截图功能需要引入一个插件,插件下载地址为: https://blog-static.cnblogs.com/files/lyt520/html2canvas.min.js 2,2 下载完成后放到一个工具文件夹中 然后引入:
2.3 然后在 methods里面 新增一个screenShot()方法即可完成截图功能 代码如下:
screenShot() {
if(!this.imgBase64){
return alert("请先上传一张图片!")
}
const imgEl = document.getElementById('img');
html2canvas(imgEl, {
scale: window.devicePixelRat,
backgroundColor: null,
allowTaint: false,
useCORS: true,
taintTest: true,
}).then((canvas) => {
let url = canvas.toDataURL('image/png', 1.0);
var saveLink = document.createElement('a');
saveLink.href = url;
saveLink.download = 'downLoad.png';
saveLink.click();
});
},
3,css代码如下:
.container{
width: 402px;
height: 300px;
background:#ccc;
border: 1px solid red;
margin: 0 auto;
}
// 预览的盒子
.perview-box{
width: 402px;
height: 400px;
border: 1px solid;
margin-top: 10px;
#img{
display: inline-block;
width: 400px;
}
}
.del{
margin-left: 20px;
}
|