html部分:
<div style="width:400px">
<img :src="image" class="img" alt=""/>
<canvas id="mycanvas" width='750' height='530' class="my-canvas"></canvas>
</div>
js部分:
export default {
data() {
return {
image: "",
nane:'张三',
zhengshu:'3333333'
};
},
mounted() {
this.drawPhoto();
},
methods: {
drawPhoto() {
let canvas = document.getElementById("mycanvas");
let context = canvas.getContext("2d");
let img = new Image();
img.setAttribute("crossOrigin", "anonymous");
img.src = require("xxx.jpg");
img.onload = () => {
canvas.setAttribute("width",img.width)
canvas.setAttribute("height",img.height)
context.drawImage(img,0,0,img.width,img.height);
context.font = "90px Arial";
context.fillText(this.nane, 420, 1150);
context.fillText(this.zhengshu, 2700, 1020);
this.image = canvas.toDataURL("image/jpg", 1.0);
};
},
}
}
css部分:
<style scoped>
.my-canvas{
position: fixed;
top: -99999999999px;
left: -99999999999px;
z-index: -99999999999;
opacity: 0;
}
.img{
width: 100%;
height: auto;
}
</style>