uniapp 生成商品分享通用海报源码详解

2024-06-21 19:18

本文主要是介绍uniapp 生成商品分享通用海报源码详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

uniapp 生成商品分享通用海报源码,使用canvas生成海报,圆片加圆角,标题换行显示两排 多余点点,兼容H5端和小程序端。

组件显示代码<template><view><uni-popup ref="popup" type="center" class="poster-alert"><view class="poster"><image :src="poster" @longpress="saveImg"></image></view><view class="hm row-center"><view class="item" @tap="shareWx"><button open-type="share" class="sharebtn"><image :src="util.ossimg('button-wechat.png')"></image></button><text>微信</text></view>					<!-- #ifdef MP-WEIXIN --><view class="item" @tap="saveImg"><view class="sharebtn"><image :src="util.ossimg('button-save.png')"></image></view><text>保存图片</text></view><!-- #endif --></view>			</uni-popup></view>
</template><script>export default{name:'GoodsPoster', //商品分享海报	data(){return{poster:'',}},methods:{show(){this.$refs.popup.open();},close(){this.$refs.popup.close();},//分享微信好友shareWx(){},//保存图片async saveImg(){// #ifdef MP-WEIXINlet that = this;let imginfo = await uni.getImageInfo({src: that.poster })if(imginfo[1]){uni.saveImageToPhotosAlbum({filePath:imginfo[1].path,success() { that.tips.toast('保存成功') },fail(rej){  that.tips.toast('保存失败'); }})}// #endif				 },}		}
</script><style lang="scss" scoped>.poster-alert{ width:520rpx;.poster{ width:520rpx; height:832rpx; overflow: hidden; background:#fff; border-radius:16rpx;}.hm{ margin-top:60rpx; .item{ text-align: center; margin:0 60rpx;.sharebtn{ background:none; border:0; padding:0; width:100rpx; height:100rpx; margin:0 auto; margin-bottom:12rpx;&:after{display:none;}image{ width:100rpx; height:100rpx; display:block; }					}text{ font-size:24rpx; color:#fff;}}}}	
</style>

 

海报生成代码<template><view>		<!--分享海报-->	<goods-poster ref="poster"></goods-poster><canvas canvas-id="mycanvas" class="mycanvas"></canvas>	</view>
</template><script>import GoodsPoster from '@/components/Goods/GoodsPoster.vue'export default{components:{  GoodsPoster },data(){return{}},async onLoad() {this.create();},		methods:{		//生成圆形填充图片//参数:ctx:canvas, img:图片, x:距离X, y:距离Y, r:半径circleImg(ctx, img, x, y, r) {ctx.save();var d =2 * r;var cx = x + r;var cy = y + r;ctx.arc(cx, cy, r, 0, 2 * Math.PI);ctx.clip();ctx.drawImage(img, x, y, d, d);ctx.restore();},//圆角矩形填充图片// 参数:ctx: canvas, img:图片地址, x:距离X,y:距离Y, width:宽, height:高,radius:圆角大小drawRoundRectPath(ctx, img, x, y, width, height, radius) {ctx.save();ctx.translate(x, y);ctx.beginPath(0);ctx.arc(width - radius, height - radius, radius, 0, Math.PI / 2);ctx.lineTo(radius, height);ctx.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);ctx.lineTo(0, radius);ctx.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);ctx.lineTo(width - radius, 0);ctx.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);ctx.lineTo(width, height - radius);ctx.closePath();ctx.clip();	ctx.drawImage(img,0,0,uni.upx2px(460),uni.upx2px(460));ctx.restore();},// 文字自动换行// 参数:ctx:canvas, content:文字, drawX:距离X, drawY:距离Y, lineHeight: 行高, lineMaxWidth最大宽度, lineNum: 行数textPrewrap(ctx, content, drawX, drawY, lineHeight, lineMaxWidth, lineNum) {var drawTxt = ''; // 当前绘制的内容var drawLine = 1; // 第几行开始绘制var drawIndex = 0; // 当前绘制内容的索引// 判断内容是否可以一行绘制完毕if(ctx.measureText(content).width <= lineMaxWidth){ctx.fillText(content, drawX, drawY);} else {for (var i = 0; i < content.length; i++) {drawTxt += content[i];if (ctx.measureText(drawTxt).width >= lineMaxWidth) {if (drawLine >= lineNum) {ctx.fillText(content.substring(drawIndex, i) + '..', drawX, drawY);break;} else {ctx.fillText(content.substring(drawIndex, i + 1), drawX, drawY);drawIndex = i + 1;drawLine += 1;drawY += lineHeight;drawTxt = '';}} else {// 内容绘制完毕,但是剩下的内容宽度不到lineMaxWidthif (i === content.length - 1) {ctx.fillText(content.substring(drawIndex), drawX, drawY);}}}}},		 //获取图片信息//微信:要下载远程图片到本地//H5远程图片时要将图片生成base64位,这个可让后台帮生成base64位,前端生成始终有跨域问题头疼, 另一种是将图片放在static目录async getimgInfo(path){// #ifdef MP-WEIXINconst info = await uni.downloadFile({url:path})				return info;// #endif					// #ifdef H5const info = [{},{tempFilePath:'/static/img/p.png'}]	return info;// #endif									},//生成海报async create(){uni.showLoading({title:'海报生成中...'})let ctx  = uni.createCanvasContext('mycanvas',this);ctx.fillStyle = "#fff"ctx.fillRect(0,0,uni.upx2px(520),uni.upx2px(845))//头像let head = "https://avatar.csdnimg.cn/8/C/D/0_u012015434.jpg";let headurl = await this.getimgInfo(head);headurl = headurl[1].tempFilePath;this.circleImg(ctx,headurl,uni.upx2px(30),uni.upx2px(30),uni.upx2px(36))//姓名ctx.setFontSize(uni.upx2px(28))ctx.fillStyle = '#000';ctx.fillText("神夜大侠",uni.upx2px(115),uni.upx2px(55))//说明ctx.setFontSize(uni.upx2px(20))ctx.fillStyle = '#000';ctx.fillText("这件商品很棒,推荐给你看看",uni.upx2px(115),uni.upx2px(85))//logolet logo = "https://avatar.csdnimg.cn/8/C/D/0_u012015434.jpg";let logourl = await this.getimgInfo(logo);logourl = logourl[1].tempFilePath;ctx.drawImage(logourl,uni.upx2px(410),uni.upx2px(35),uni.upx2px(80),uni.upx2px(40))//商品图let img = "https://avatar.csdnimg.cn/8/C/D/0_u012015434.jpg";let imgurl = await this.getimgInfo(img);imgurl = imgurl[1].tempFilePath;this.drawRoundRectPath(ctx,imgurl,uni.upx2px(30),uni.upx2px(120),uni.upx2px(460),uni.upx2px(460),uni.upx2px(16));	//商品标题ctx.setFontSize(uni.upx2px(24))ctx.fillStyle = '#333';this.textPrewrap(ctx,"这是商品标题名称商品标题名称",uni.upx2px(30),uni.upx2px(670),uni.upx2px(36),uni.upx2px(250),2);//价格ctx.setFontSize(uni.upx2px(22))ctx.fillStyle = this.config.webColor;ctx.fillText("¥",uni.upx2px(30),uni.upx2px(760))ctx.setFontSize(uni.upx2px(36))ctx.fillStyle = this.config.webColor;ctx.fillText("58.00",uni.upx2px(50),uni.upx2px(760))//二维码let ewm = 'https://avatar.csdnimg.cn/8/C/D/0_u012015434.jpg';let ewmurl = await this.getimgInfo(ewm);ewmurl = ewmurl[1].tempFilePath;ctx.drawImage(ewmurl,uni.upx2px(339),uni.upx2px(620),uni.upx2px(150),uni.upx2px(150))//扫码购买ctx.setFontSize(uni.upx2px(20))ctx.fillStyle = "#333";ctx.fillText("-扫码购买商品-",uni.upx2px(347),uni.upx2px(800))										ctx.draw();setTimeout(()=>{uni.canvasToTempFilePath({canvasId:'mycanvas',success:res=>{this.$refs.poster.poster = res.tempFilePath;this.$refs.poster.show();uni.hideLoading();},fail:err=>{this.tips.toast('海报生成失败')uni.hideLoading();}})},500)			 },	 }}
</script><style lang="scss" scoped>	.mycanvas{ width:520rpx; height:845rpx; background:#fff; position: fixed; bottom:1000px;  border-radius:16rpx;}
</style>

这篇关于uniapp 生成商品分享通用海报源码详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1082095

相关文章

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr