Home > Article > Web Front-end > html5 canvas WeChat poster sharing
This article mainly introduces the relevant information on the detailed explanation of html5 canvas WeChat poster sharing (personal climbing pit). The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
Randomly generate a picture
Get the avatar and title of the WeChat user (adjust the backend interface to obtain it)
Combine the user's avatar and title with a randomly generated picture to create a poster
Maybe the user on the previous page also filled in the wish text. Fill in the picture
to achieve the effect
Record the problems encountered in the process of implementing the function
#Canvas long press in WeChat browser is invalid and cannot be shared like img (then I will convert img)
After being converted to img, it can be displayed in the WeChat developer tools, but it will not work on a real device (I want to cry without tears). Du Niang said that the image may be cross-domain^-^
User avatar synthesis also requires rounded corners, I said no, I just looked at the canvas api document and I have no love for Du Niang
canvas If the text exceeds the specified width, it must be wrapped. I only know the various text alignment methods that do not exceed the specified width. ctx.textAlign = 'center';
canvas under high-definition screen The vague question (it’s super simple and I don’t know why Du Niang is so verbose) is not canvas.witdt=innerWidth*devicePixelRatio
HTML structure
<p class="imgBox" v-cloak> <img :src='imgSrc' v-if="imgSrc" /> </p>
CSS
<style> *{ margin:0; padding:0; } body, html { width: 100%; height: 100%; } .imgBox { width: 100%; height: 100%; } img { width: 100%; display: block; } </style>
script
##
// js主要结构 new Vue({ el:'imgBox', data:{ urlParam: {},//获取url中的传值对象 randomNum: 1,//随机数用于确定那个祈福页 userName: '',//用户称呢 imgSrc: '',//合成最终图片 userImg: '',//用户头像图片 userMessage: '',//用户留言 }, methods: { // 分享到盆友圈 wxShareFriends: function () {}, // 初始化请求头 wxHttp: function () { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); }, // 获取随机数[1,10] randomNumbers() { this.randomNum = Math.ceil(Math.random() * 10) }, // 获取微信用户头像和称呢和用户输入祝福语 getUserInfo() { var vm = this; $.post('API请求地址', function (data) { if (data.code == 1) { vm.userImg = data.data.headimg; vm.userName = data.data.nickname; if (vm.randomNum % 2 == 0) { vm.userMessage= '红尘相遇,年华已老。岁月花开多少不在,古往今来相遇是一件既微妙。而又神圣的事情,红尘的情网中' } else { vm.userMessage = '红尘相遇,年华已老' } } vm.$nextTick(function () { vm.drawCanvasBgImg(); }) }) }, // 获取页面dpr和宽度 getWindowInfo() { var windowInfo = {}; windowInfo.dpr = window.devicePixelRatio; if (window.innerWidth) { windowInfo.width = window.innerWidth; } else { windowInfo.width = document.body.clientWidth; } return windowInfo; }, // 画活动页分享背景大图 drawCanvasBgImg () {}, // 在背景图片的画布上截取一个圆然后填充入用户头像 drawCanvasUserImg(canvas, ctx, dpr) {}, // 填写用户称呢或者用户留言 canvasFillText (canvas, ctx, dpr, circleR) {}, // 合成base64位分享图 convertCanvasToImage (canvas) { this.imgSrc = canvas.toDataURL("image/jpeg");//png有毒在安卓机下识别二维码无法跳转 this.$Spin.hide(); } } })
Drawing method Steps
Draw activity page sharing background large image drawCanvasBgImg ()
//拿到数据后开始画背景大图 想法很简单就是把图片画到canvas中然后在画布上再画头像文字让后转成img drawCanvasBgImg () { var vm = this; var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); var clientWidth = this.getWindowInfo().width; //获取屏幕宽度用于canvas宽度自适应移动端屏幕 var dpr = this.getWindowInfo().dpr; ctx.globalCompositeOperation = "source-atop";//** 坑锯齿感觉没什么用不知道是不是用错地方了 ** canvas.width = dpr * clientWidth; //由于手机屏幕时retina屏,都会多倍渲染,用dpr来动态设置画布宽高,避免图片模糊 canvas.height = dpr * clientWidth * 609 / 375;//去掉微信头部的状态栏应该是603 没搞懂603还是不能让图片满屏直接多加到了609 var img = new Image(); img.crossOrigin = '';//死坑的图片跨域 (img.crossOrigin = "Anonymous"这种写法还是不能显示base64格式图片) img.src = "http://xxx" + this.randomNum + ".jpg"; img.onload = function () { ctx.drawImage(img, 0, 0, canvas.width, canvas.height); vm.drawCanvasUserImg(canvas, ctx, dpr); } },
User avatar drawCanvasUserImg (canvas, ctx, dpr)
// 在背景图片的画布上截取一个圆然后填充入用户头像 drawCanvasUserImg: function (canvas, ctx, dpr) { var vm = this; var circleR = 50 * dpr;//半径 var circleX = canvas.width / 2;//圆心X坐标 var circleY = 50 * dpr;//圆心Y坐标 var imgX = circleX - circleR;//图片X开始坐标 var imgY = circleY - circleR;//图片Y开始坐标 var imgWidth = 2 * circleR;//图片按圆形大小 var img = new Image(); img.crossOrigin = ''; img.src = this.userImg; img.onload = function () { ctx.save(); // 保存当前ctx的状态 ctx.arc(circleX, circleY, circleR, 0, 2 * Math.PI); //画出圆 ctx.clip(); //裁剪上面的圆形 ctx.drawImage(img, imgX, imgY, imgWidth, imgWidth); // 在刚刚裁剪的园上画图 ctx.restore(); // 还原状态 vm.canvasFillText(canvas, ctx, dpr, circleR); } },
Draw text in canvas
// 填写用户称呢或者用户留言 canvasFillText (canvas, ctx, dpr, circleR) { var fontSizeThis = dpr * 20 + 'px' + ' Arial'; var userNameY = 0;//用户名Y轴坐标 var userMessageX = dpr * 40;//用户留言X轴坐标 var userMessageY = 0;//用户留言Y轴坐标 var lastSubStrIndex = 0;//字符串下标 var lineWidth = 0;//一行宽度 var allTextWidth = 0;//所有字符宽度 ctx.font = fontSizeThis; // 需要用户名是写入用户名 if (this.userName) { userNameY = circleR * 2.5; ctx.fillStyle = "#0094ff"; ctx.textAlign = 'center'; ctx.fillText(this.userName, canvas.width / 2, userNameY); } if (this.userMessage) { userMessageY = userNameY + dpr * 35; ctx.fillStyle = "#000"; // 获取字符宽度 for (var i = 0; i < this.userMessage.length; i++) { allTextWidth += ctx.measureText(this.userMessage[i]).width; } // 字符串长度大于画布区域要换行 if (allTextWidth > canvas.width - 2* userMessageX) { for (var i = 0; i < this.userMessage.length; i++) { lineWidth += ctx.measureText(this.userMessage[i]).width; if (lineWidth > canvas.width - 2*userMessageX) { ctx.textAlign = 'left'; ctx.fillText(this.userMessage.substring(lastSubStrIndex, i), userMessageX, userMessageY); userMessageY += dpr * 25;//设置行高 lineWidth = 0; lastSubStrIndex = i; } if (i == this.userMessage.length - 1) { ctx.fillText(this.userMessage.substring(lastSubStrIndex, i + 1), userMessageX, userMessageY); } } } else { // 小于者居中显示 ctx.textAlign = 'center'; ctx.fillText(this.userMessage, canvas.width / 2, userMessageY); } } this.convertCanvasToImage(canvas); },Related recommendations:
jQuery and CSS3 implement parallax for APPLE TV posters Sharing of effect examples
Share a mobile project that uses canvas to synthesize poster images
Use PHP to send custom backgrounds, avatars, and text. QR code poster and WeChat message containing prompts
The above is the detailed content of html5 canvas WeChat poster sharing. For more information, please follow other related articles on the PHP Chinese website!