未输入内容之前:
生成后的图片:
用下列图片到https://c.runoob.com/front-end/59 转换成base64格式后放入imgSrc的""中
完整代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>arpara产品发布会邀请函</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.10/lib/index.css" />
<style>
#app {
position: relative;
}
body {
margin: 0;
}
[v-cloak] {
display: none;
}
.bg {
width: 100vw;
position: relative;
overflow: hidden;
}
#scream {
width: 100%;
height: 100%;
}
.closeImg {
position: absolute;
top: 52px;
right: 8px;
width: 34px;
height: 34px;
z-index: 99999;
}
.pop {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: rgba(0, 0, 0, 0.6);
z-index: 98;
}
.saveImg {
display: block;
margin-top: 64px;
}
.popImg {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 9999;
text-align: center;
font-size: 40px;
font-family: PingFang SC;
font-weight: 500;
color: #fff;
text-shadow: 0px 3px 6px rgba(0, 0, 0, 0.3);
letter-spacing: 4px;
}
.inputMediaName{
width: 900px;
border: none;
color: #4D4D4D;
background-image: linear-gradient(55deg, #FE51A1 0%, #C98EBE 26.8798828125%, #B1ACD7 49.31640625%, #99A6E3 73.6572265625%, #6060D4 100%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-family: FZLanTingHei-B-GBK;
font-size: 60px;
font-weight: 400;
font-family: PingFang SC;
position: absolute;
top: 52%;
left: 50%;
transform: translateX(-50%);
z-index: 5;
text-align: center;
outline: none;
}
.inputMediaName textarea{
width: 900px;
}
.inputName {
max-width: 360px;
border: none;
background: transparent;
color: #2A1F1C;
font-size: 44px;
font-weight: bold;
font-family: FZLanTingHei-B-GBK;
position: absolute;
top: 62%;
left: 50%;
transform: translateX(-50%);
z-index: 5;
text-align: center;
outline: none;
}
.inputMediaName::-webkit-input-placeholder {
/* WebKit browsers */
font-size: 60px;
font-weight: 400;
color: #4D4D4D;
background: linear-gradient(55deg, #FE51A1 0%, #C98EBE 26.8798828125%, #B1ACD7 49.31640625%, #99A6E3 73.6572265625%, #6060D4 100%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.inputMediaName:-moz-placeholder {
/* Mozilla Firefox 4 to 18 */
font-size: 60px;
font-weight: 400;
color: #4D4D4D;
background: linear-gradient(55deg, #FE51A1 0%, #C98EBE 26.8798828125%, #B1ACD7 49.31640625%, #99A6E3 73.6572265625%, #6060D4 100%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.inputMediaName::-moz-placeholder {
/* Mozilla Firefox 19+ */
font-size: 60px;
font-weight: 400;
color: #4D4D4D;
background: linear-gradient(55deg, #FE51A1 0%, #C98EBE 26.8798828125%, #B1ACD7 49.31640625%, #99A6E3 73.6572265625%, #6060D4 100%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.inputMediaName::-ms-input-placeholder {
/* Internet Explorer 10+ */
font-size: 60px;
font-weight: 400;
color: #4D4D4D;
background: linear-gradient(55deg, #FE51A1 0%, #C98EBE 26.8798828125%, #B1ACD7 49.31640625%, #99A6E3 73.6572265625%, #6060D4 100%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.inputName::-webkit-input-placeholder {
color: #2A1F1C;
}
.inputName::-moz-placeholder {
color: #2A1F1C;
}
.inputName::-moz-placeholder {
color: #2A1F1C;
}
.inputName::-ms-input-placeholder {
color: #2A1F1C;
}
#myCanvas {
position: absolute;
left: -9999px;
top: -9999px;
}
.createImg {
width: 90%;
left: 5%;
position: fixed;
bottom: 43px;
font-size: 28px;
font-family: PingFang SC;
font-weight: 500;
color: #fff;
height: 88px;
line-height: 88px;
background: #0064FF;
opacity: 1;
border-radius: 8px;
z-index: 9;
text-align: center;
}
</style>
</head>
<body>
<div id="app" v-cloak>
<img id="scream" :src="imgSrc" alt="The Scream" @load="afterLoad">
<textarea autofocus type="" v-model="inputItem.mediaName" class="inputMediaName" placeholder="点击输入邀请媒体名称">
</textarea>
<input autofocus type="text" v-model="inputItem.name" class="inputName" placeholder="点击输入姓名">
<div v-on:click='canvas' class="createImg">生成邀请函</div>
<canvas id="myCanvas" :width="imgWidth" :height="imgHeight" ref="canvas">
您的浏览器不支持 HTML5 canvas 标签。</canvas>
<div class="pop" v-if='showImg' v-on:click="showImg = false">
</div>
<div class="popImg" v-if='showImg'>
长按保存至相册
<img :src="closeImg" class="closeImg" v-on:click="showImg = false" alt="">
<img class="saveImg" :src="saveImg" alt="">
</div>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vant@2.10/lib/vant.min.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js" type="text/javascript"></script>
<script>
new Vue({
el: '#app',
data: {
context: {},//canvas
imgHeight: '1334',//图片高度
imgWidth: '750',//图片高度
inputItem:{
mediaName:"",//媒体名称
name: '',//姓名
},
saveImg: '',//保存图片
showImg: false,//显示图片
closeImg: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADEAAAAxCAMAAABEQrEuAAAAzFBMVEUAAAD///86kuEIAAAARHRSTlMAAQIDBAUGBxcYGhshIiRLTVBUVVdZW2lwcnN0dXeEhYiLjI2VlpeYmZqcnZ6foKGjpKanqKmrrK2ur7CztbbBxMXIzKPOVfAAAAHqSURBVEjHnZZ5W8IwDMZZr6l4X3hfoBPBDSZzoODBvv93soMm6452e9xfaZvf2jTp27Za//g2Orf7TgM/snfT2UyN3Z8kSQK3FhDP0u97S1pnSfr1RQ3Aeyu/K2l2kyaIeFy73Uv7ZG0mgQ0RL8rrVDboUDUG3LykJ/gtSZtuHxDTLNxTHkO1QQKQcTXCXwHAcRHApKwCYBDDWEuBGABSjoUD4OdyxgGJiggfAVBYshirgZDm+qmv+t9KMWIsoR4L8y2bwgCZZQibAFCZKx6p4ZhAtYaq592QKQ4OMc0DkbGAKLgsUoR8AMDMBcdmymnutJypsj+5rahZDAgBYMZrjiYgSwBo3eGki0T/vli9ANC5BixYE21xMmRBmskRgRiWvBmA26pl3z7DVIsjbhAHZrpUliYgLOQjrMkHw8rikMrQXiSTbFsx+5EFwSO6yjSWZWCMhfv5cHGJJoFF1cDiw/NSLX0oZNp5wINcpckoZLlAeWDUZNTWwhFFhSnGgupdUg2UvvyVJDxziKjJfbfiBqoUMtTkoVu4FKu0NS+wnhoXPYN6ZwhcSd5qFuLVARoySJED1RjZHgEuXK/HsnGn0RZEraQr7fPCpWha2Bq5luZOw3eJm27y73Zqti+6R43ePocPl+3/PK7+AKHzueY3wvW7AAAAAElFTkSuQmCC',
imgSrc: '全屏背景base64图片路径地址',
toast: null
},
created() {
// window.scrollTo(0, document.documentElement.clientHeight);
this.toast = vant.Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true,
message: '加载中...',
});
},
mounted() {
},
methods: {
canvas() {
if (!this.inputItem.mediaName) {
alert('请输入邀请媒体名称')
return
}
if (!this.inputItem.name) {
alert('请输入姓名')
return
}
let imgs = new Image();
imgs.src = this.imgSrc;
let imgWidth = 0, imgHeight = 0, that = this;
//获取图片宽高
imgs.onload = function () {
imgWidth = imgs.width
imgHeight = imgs.height
const canvas = that.$refs.canvas
that.context = canvas.getContext("2d");
that.context.drawImage(imgs, 0, 0, that.imgWidth, that.imgHeight);
that.context.save();
that.context.font = "bold 60px FZRUIZH_JW--GB1-0";
that.context.textAlign = "center";
var gradient= that.context.createLinearGradient(0,0,canvas.width,0);
gradient.addColorStop("0","#FE51A1");
gradient.addColorStop("0.25","#C98EBE");
gradient.addColorStop("0.5","#B1ACD7");
gradient.addColorStop("0.75","#99A6E3");
gradient.addColorStop("1","#6060D4");
that.context.fillStyle=gradient;
that.context.lineWidth=1;
var str=that.inputItem.mediaName;
var lineWidth = 0;
var canvasWidth = canvas.width-50;
var initHeight=740;//绘制字体距离canvas顶部初始的高度
var lastSubStrIndex= 0; //每次开始截取的字符串的索引
for(let i=0;i<str.length;i++){
lineWidth+=that.context.measureText(str[i]).width;
if(lineWidth>canvasWidth){
that.context.fillText(str.substring(lastSubStrIndex,i),376,initHeight);//绘制截取部分
initHeight+=70;//40为字体的高度
lineWidth=100;
lastSubStrIndex=i;
}
if(i==str.length-1){//绘制剩余部分
that.context.fillText(str.substring(lastSubStrIndex,i+1),376,initHeight);
}
}
// that.context.fillText(that.inputItem.mediaName, that.imgWidth / 2, 800, 360);
that.context.font = "bold 44px FZRUIZH_JW--GB1-0";
that.context.textAlign = "center";
that.context.fillStyle = "#2A1F1C"
that.context.fillText(that.inputItem.name, that.imgWidth / 2, 900, 360);
that.context.save();
document.body.scrollTop = document.documentElement.scrollTop = 0
var base64Img = canvas.toDataURL('image/jpeg');
that.showImg = true;
that.saveImg = base64Img;
};
that.getData();//统计数量
},
afterLoad() {
vant.Toast.clear();
},
getData() {
$.post(`https://api-dev.tuoluohuodong.com/api/common/invitation/letter/template/num`, {}, function (res) {
console.log(res, '成功')
}, 'json')
}
},
})
</script>
</body>
</html>