首页 > 其他分享 >uniapp 之绘制海报 并适应机型

uniapp 之绘制海报 并适应机型

时间:2023-11-14 15:34:20浏览次数:23  
标签:uniapp 海报 const 机型 borderRadius res pxRatio canvasBox

  之前绘制的海报出现的问题:

    ① 海报有一角圆角没体现出来

    ② 海报内容随机型,变动到其他位置(并不是想要的地方)

  针对于这个问题进行修改

  注意:海报设置的宽度与你canvas给的标签的宽高是保持一致,下面图片为完成的海报图

 

 

 

 

  一、设置海报的初始化宽高,这个需要在小程序的onReady生命周期去执行

    setCanvasSize() {
				const resScreen = uni.getSystemInfoSync();
				const screenWidth = resScreen.screenWidth; // 屏幕宽度
				const screenHeight = resScreen.screenHeight; // 屏幕高度

				// 375*752
				let pxWidthRatio = screenWidth / 375;
				let pxHeightRatio = screenHeight / 752

				this.boxWidth = this.boxWidth * pxWidthRatio
				this.boxHeight = this.boxHeight * pxHeightRatio

				this.canvasContext = uni.createCanvasContext('firstCanvas', this, this.boxWidth, this.boxHeight);
			},

 

  二、绘制海报

       // 绘制图片
			captureViewAsImage() {
         // 获取设备的宽高 const screenWidth = uni.getSystemInfoSync().screenWidth; const screenHeight = uni.getSystemInfoAsync().screenHeight; // 获取canvas上下文对象 const pxRatio = screenWidth / 750; const width = this.boxWidth * pxRatio; // canvas宽度 const height = this.boxHeight * pxRatio; // canvas高度 const canvasBox = this.canvasContext const borderRadius = 20; // 圆角半径 // 绘画圆角矩形 this.clipRectangle(canvasBox, width, height, borderRadius * pxRatio) // 绘制当前图片 uni.getImageInfo({ src: 分享的图片地址, success: res => { const imgWidth = res.width; // 原始图片宽度 const imgHeight = res.height; // 原始图片高度 const targetWidth = 340; // 目标宽度 // 根据目标高度计算等比例的宽度 const targetHeight = Math.floor((imgHeight / imgWidth) * targetWidth); canvasBox.drawImage(res.path, 0, 0, targetWidth, targetHeight); // 绘制底部白色区域 const whiteHeight = 190; // 底部白色区域的高度 canvasBox.fillStyle = '#fff'; // 设置填充颜色为白色 canvasBox.fillRect(0, height - whiteHeight * pxRatio, width, whiteHeight * pxRatio); // 绘制Logo图片 canvasBox.drawImage('logo图片', 14, (this.boxHeight - 364) * pxRatio, 63, 63); // 绘制二维码 canvasBox.drawImage('二维码图片', (width + 130) * pxRatio, ( this.boxHeight - 166) * pxRatio, 63, 63); // 绘制文字 canvasBox.setFontSize(17); canvasBox.setFillStyle('#333'); canvasBox.font = '600 17px PingFangSC, PingFangSC-Regular' canvasBox.fillText('您自己小程序名称', 14, (this.boxHeight - 106) * pxRatio); canvasBox.setFontSize(12); canvasBox.font = '500 12px PingFangSC, PingFangSC-Regular' canvasBox.fillText('辅助描述文字', 14, (this.boxHeight - 56) * pxRatio); // 调用draw方法进行绘图 canvasBox.draw(true, () => { // 将绘制结果保存为图片 uni.canvasToTempFilePath({ canvasId: 'firstCanvas', success: res => { this.isLoading = false console.log('生成图片成功', res.tempFilePath);
                      // 微信自带的分享功能 wx.showShareImageMenu({ path: res.tempFilePath, success: res => { console.log("success:", res); this.isShowShare = false }, // 取消 fail: err => { console.log("fail:", err); this.isShowShare = false } }) }, fail: err => { console.log('生成图片失败', err); } }, this) }); } }) },

  

   三、海报图进行剪切

      clipRectangle(canvasBox, width, height, borderRadius) {
				// 绘制圆角矩形路径
				canvasBox.beginPath();

				// 左上角
				canvasBox.arc(borderRadius, borderRadius, borderRadius, Math.PI, Math.PI * 1.5);
				
				// 右上角
				canvasBox.arc(width - borderRadius, borderRadius, borderRadius, Math.PI * 1.5, Math.PI * 2);
				
				// 右下角
				canvasBox.arc(width - borderRadius, height - borderRadius, borderRadius, 0, Math.PI * 0.5);
				
				// 左下角
				canvasBox.arc(borderRadius, height - borderRadius, borderRadius, Math.PI * 0.5, Math.PI);

				// 闭合路径
				canvasBox.closePath();

				// 设置线条连接处为圆角
				canvasBox.lineJoin = 'round';

				// 画布裁切
				canvasBox.clip();

				canvasBox.fillStyle = '#fff';
				canvasBox.fill();
			},

  

  注:该文档为个人理解所写,有误可建议修改(也可留下您宝贵的建议哦~)

 

标签:uniapp,海报,const,机型,borderRadius,res,pxRatio,canvasBox
From: https://www.cnblogs.com/persistIn/p/17831738.html

相关文章

  • uniapp(安卓)之文件上传
    uniapp(安卓)之文件上传uniapp提供的uni.chooseFile只支持H5和微信小程序,所以想上传除图片/视频外的非媒体文件,需要使用原生的方式开发。 uploadtxdr(){//使用plus选择文件 letthat=this; letfilePath='' letmain=plus.android.runtimeMainAct......
  • PG游戏库大师的选择?分析师透露明年iPad全新6款机型,史上最大屏iPad Air领衔
    苹果今年罕见未发布任何新平板,引起PGSOFT科技电子外界对2024年iPad系列的高度关注。最新消息透露,明年上半年至下半年将陆续推出四大新系列,包括入门iPad、iPadmini、高阶iPadAir和旗舰iPadPro,共6款机型。除了芯片性能升级外,高阶iPadPro将进行屏幕面板升级,或带来一波价格涨幅。分......
  • uniapp 404页面
    需求:uniapp在写H5时,如果在地址栏乱输入,会跳转到404页面。思路:uniapp有个应用生命周期onPageNotFound,让跳转不存在的页面时会执行这个回调函数。所以需求就很好解决了!具体操作:第一步:写一个404页面,并在pages.json中配置其路由。第二步:在App.vue的onPageNotFound......
  • app直播源码,uniapp随机数生成签名
    app直播源码,uniapp随机数生成签名1、首先,需要引入node.js内置模块crypto,它提供了一些加密相关的函数和方法。可以在项目的common文件夹下新建一个js文件,例如utils.js,并在其中定义一个生成随机串的方法,例如: //引入node.js内置模块cryptoconstcrypto=require('cr......
  • android短视频开发,uniapp页面滚动条到指定位置
    android短视频开发,uniapp页面滚动条到指定位置#html指定位置<viewclass="gap_body_position"></view> #js执行this.$nextTick(()=>{  //一定要用nextTickuni.pageScrollTo({duration:300,selector:'.gap_body_position'});})​以上就是android短视频开发,uniapp页......
  • uniapp小程序页面跳回携带参数
    B返回A1.B跳回事件letpages=getCurrentPages();//当前页页⾯实例            letnowPage=pages[pages.length-1];//当前页⾯实例            letprevPage=pages[pages.length-2];//上一页面实例            //......
  • uniApp:使用vue3+Vite4+pinia+sass技术栈构建(03)-封装对象类
    1.在src文件夹创建models文件夹import{user}from"@/service/api"//用户信息返回的数据类型interfaceuserInfoType{username:string,phone:string}//返回类型interfaceResultType<T>{errno:number,errmsg:string,datas:T}classuser......
  • uniapp之文件保存
    uniapp之文件保存文件保存分几种情况:1.网络文件保存:使用uni.downloadFile创建临时文件地址,然后使用uni.saveFile保存uni.downloadFile({//下载 url:path, success:(res)=>{ if(res.statusCode==200){ uni.saveFile({ tempFileP......
  • uniapp做移动端手写签名生成图片
    1、电子签名电子签名目前已经有很多现成的插件,所以我这里直接选择了一个,有需要的可以看下https://ext.dcloud.net.cn/plugin?id=4354代码如下:HTML:<viewclass="content"> <viewstyle="width:750rpx;height:400px;"> <l-signaturedisableScrollbackgroundColor="......
  • uniapp实用功能代码(小程序支付,图片保存,返回刷新,分享到朋友圈)
    1.uniapp小程序支付:uni.request({url:"http://xxxxxx/payOrder",//后端接口返回调起支付需要的参数data:{userId:1,//此接口需要的参数一般有多个此仅为示例},method:"POST",success:(res)=>{console.log(res.data,"这......