首页 > 其他分享 >画板生成海报图

画板生成海报图

时间:2023-06-13 14:22:30浏览次数:33  
标签:info 海报 画板 setFillStyle ctx fillText 生成 let details

画板生成海报图

<canvas id="myCanvas" canvas-id="myCanvas" style="width: 320px; height: 520px" @longpress="saveTheQrCode(goods_poster)"></canvas>

网络生成的图片需要使用 promose进行一下异步处理这样可以方式因为异步问题造成海报无法生成

		getImageInfo(image) {
			return new Promise((req, rej) => {
				uni.getImageInfo({
					src: image,
					success: function (res) {
						req(res)
					}
				})
			})
		},

 绘制海报的对应方法解释 async 生成海报做成异步的(注意一点画板从上到下一层一层的最下面的代码在最上层)

		// 生成海报
		async clickGenerate() {
			uni.showLoading({
				title: '正在绘制'
			})     
			let goods_img = 'https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500'        
			//对网络图片进行处理
			let backImg = await this.getImageInfo(goods_img)       
			const ctx = uni.createCanvasContext('myCanvas')   
            //设置填充色,不设置默认就是 black
			ctx.setFillStyle('#00be88')
			//设置色块颜色   参数1 为X轴偏移距离 参数2 为Y轴偏移距离 参数3 为色块宽度 参数4 为色块高度
			ctx.fillRect(0, 0, 320, 540)
			//设置图片在画布上的位置 参数1 为图片地址  参数2 为X轴偏转距离 参数3 为图片宽度 参数4 为图片高度
			ctx.drawImage(backImg.path, 10, 10, 300, 300)
			ctx.setFillStyle('#fff')
			ctx.fillRect(10, 310, 300, 180)
			//设置字体大小
			ctx.setFontSize(16)
			ctx.setFillStyle('#333')
			//因为画板不支持直接换行   借鉴大佬的
			let _strlineW = 0;
			let _strLastIndex = 0; //每次开始截取的字符串的索引
			let _strHeight = 300 + 30; //绘制字体距离canvas顶部的初始高度
			let _num = 1;
			for (let i = 0; i < this.details_info.name.length; i++) {
				//计算每个字符的宽度
				_strlineW += ctx.measureText(this.details_info.name[i]).width;
				//判断当前宽度是否需要换行
				if (_strlineW > 300 - 20) {
				   //判断是否五第二行
					if (_num == 2 && 2) {
						//文字换行数量大于二进行省略号处理
						ctx.fillText(this.details_info.name.substring(_strLastIndex, i - 5) + '...', 20, _strHeight);
						_strlineW = 0;
						_strLastIndex = i;
						_num++;
						break;
					} else {
						ctx.fillText(this.details_info.name.substring(_strLastIndex, i), 20, _strHeight);
						_strlineW = 0;
						_strHeight += 20;
						_strLastIndex = i;
						_num++;
					}
				} else if (i == this.details_info.name.length - 1) {
					ctx.fillText(this.details_info.name.substring(_strLastIndex, i + 1), 20, _strHeight);
					_strlineW = 0;
				}
			}
			ctx.setFontSize(20)
			ctx.setFillStyle('red')
			ctx.fillText('¥' + this.details_info.sale_price, 20, 376);
			ctx.setFontSize(16)
			ctx.setFillStyle('#ccc')
			let priceW = ctx.measureText('¥' + this.details_info.sale_price).width;
			ctx.fillText(this.details_info.sale_price, priceW + 50, 376);
			ctx.setFillStyle('#00be88')
			ctx.fillText('----------------------------------------------', 10, 390, 300);
			ctx.drawImage(this.code_img, 40, 410, 60, 60)
			ctx.setFontSize(14)
			ctx.setFillStyle('#333')
			ctx.fillText('保存海报图', 160, 422);
			ctx.setFontSize(12)
			ctx.fillText('好货要和朋友一起分享', 160, 442);
			ctx.fillText(this.$store.getters.userInfo.username, 160, 462);
			ctx.setFillStyle('#fff')
			ctx.setFontSize(16)
			ctx.fillText('康复管家', 128, 510);
			ctx.draw()
			setTimeout(() => {
				uni.canvasToTempFilePath({
					canvasId: 'myCanvas',
					quality: 1,
					complete: function (res) {
						console.log(res);
						thar.goods_poster = res.tempFilePath
						console.log(res.tempFilePath);
						uni.hideLoading()
						uni.showToast({
							title: '绘制完成', //标题
							duration: 2000 //显示时间
						})

					}
				})
			}, 1500)
		},

  

标签:info,海报,画板,setFillStyle,ctx,fillText,生成,let,details
From: https://www.cnblogs.com/yugueilou/p/17477383.html

相关文章

  • 如何使用Stable Diffusion生成艺术二维码?
    硬件准备物理内存:至少16G(8G直接安装阶段就卡死)N卡:此处我使用GTX16606G(2019年双12购买)操作系统windows11软件准备网络要通畅git:https://git-scm.com/download/winPython:https://www.python.org/ftp/python/3.10.6/python-3.10.6-amd64.exeCUDA驱动:https://develo......
  • JAVA非递归生成无限级菜单树的较简代码实现。(非泛用型工具包,仅总结逻辑)
    这是一个根据列表生成一个树状结构的较简单实现。搜了搜看起来好像没多少人总结过这种实现。写上来整理一下自己的思路,请大家用用看看,应该用起来问题不大?反正我没遇到BUG。实现的时间复杂度为O(N),空间复杂度应该还是O(N)吧。不过GPT说O(1)可能是因为java的对象实现hash链表是引用而不是......
  • mybaits-plugs 连接orcale自动生成
    mybaits-plugs连接orcale自动生成calorca  20200516shmybatis-plugs是对mybatis框架进一步封装,今天尝试使用mybatis-plugs的逆向工程连接orcale/mysql数据库生成实体类以及dao1、遇到的问题有连接数据的时候抱错,拒绝连接一定要仔细检查orcalsid服务id是否正......
  • 前端-CSS生成器
    按钮https://uiverse.io/buttons有好看的button、卡片、输入框等https://markodenic.com/tools/buttons-generator/玻璃态https://hype4.academy/tools/glassmorphism-generator调色板https://mycolor.space/滚动条https://scrollbar.jason-liang.com/......
  • cmake设置gcc将shared library 改为生成 executeable
    前文ubuntu下,同一份代码,使用gcc和clang编译结果不相同。gcc编译结果,生成的程序为type为sharedlibary,而clang编译生成的程序的type为executeable.解决方案为cmakelists.txt脚本增加如下脚本set(CMAKE_CXX_FLAGS"-no-pie")set(CMAKE_C_FLAGS"-......
  • mybatisplus自动生成代码
    官网:https://baomidou.com/pages/24112f/1.引入依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> </dependency> <dependency> ......
  • ai室内设计软件生成效果
    使用平台:触站AI使用模型:触站AI写实模型效果图类型:客厅使用AI室内设计软件可以让你免费得到一张完美的效果图。只需要输入你的想法和需求,选择模型风格(比如写实风、简约风等),触站AI就能以最短时间内生成符合你需求的室内设计效果图。这将大大缩短设计周期,让你在更短的时间内获得完美的......
  • AI自动生成室内效果图
    使用平台:触站AI使用模型:触站AI写实模型效果图类型:客厅❓描述词&......
  • 基于Let's Encrypt生成免费证书-支持多域名泛域名证书
    目录客户端certbotacme.sh安装acme.sh1.自动安装2.手动安装3.测试收否安装成功使用acme.sh生成证书1.HTTP方式2.DNS方式1.生成证书记录2.在域名解析中手动添加TXT记录3.重新生成证书4.使用DNSAPI的模式进行证书申请(支持自动更新)阿里云DNSAPIDnsPodAPI查看/删除证......
  • 关于VS2022使用EF生成实体模型报错的问题:运行转换:System.NullReferenceException:对象
    起因:之前版本vs2022生成EF模型一直没有问题,在更新了最新的vs2022之后,版本号17.6+,出现此问题:运行转换:System.NullReferenceException:对象引用未设置为对象的示例。在Microsoft.VisualStudio.TextTemplatingD21DB4521EFD493FAE41A9CE9DA80C875F3084552987498BD518713BDE91D14A......