当前位置: 首页 > news >正文

uniapp 使用canvas 画海报,有手粘贴即可用

在这里插入图片描述
html部分

<view  ="doposter">下载海报</view>
<canvas canvas-id="myCanvas" type='2d' style="width: 370px; height: 550px;opcity:0;position: fixed;z-index:-1;" id="myCanvas" />

js 部分

 drawBackground() {const canvasId   = 'myCanvas'const ctx        = uni.createCanvasContext(canvasId, this)const title      = this.titleconst goodsCover = this.selcetShareImgUrl // 分享商品图片 需要换成自己的产品图片const goodsTitle = this.goods.name // 商品名称const goodsPrice = '¥ '+this.goods.original_priceconst des1       = '① 长按识别二维码'const des2       = '② 查看商品详情'const qrcode     = this.goods.share.code //二维码地址 需要换成自己的二维码//  绘制背景图ctx.setFillStyle('#fff')ctx.fillRect(0, 0, 370, 550)// 字体颜色ctx.setFontSize(17)ctx.setFillStyle('#111')ctx.fillText(title, 50, 39.9)ctx.fillText(title, 49.9, 40)ctx.fillText(title, 50,40)ctx.fillText(title, 50, 40.1)ctx.fillText(title, 50.1, 40.1)// 商品名称 且拦截页面文字长度let titleGoods = this.goods.name.split('').length <= 20 ? this.goods.name : this.goods.name.substring(0,18)+' ...'ctx.setFontSize(16)ctx.setFillStyle('#111')ctx.fillText(titleGoods , 30 , 420)// 商品价格ctx.setFontSize(18)ctx.setFillStyle('#f36d00')ctx.fillText(goodsPrice, 29.9, 450)ctx.fillText(goodsPrice, 30,450)ctx.fillText(goodsPrice, 30, 450.1)ctx.fillText(goodsPrice, 30.1, 450)//  二维码描述ctx.setFontSize(13)ctx.setFillStyle('#999')ctx.fillText(des1, 30, 490)ctx.setFontSize(13)ctx.setFillStyle('#999')ctx.fillText(des2, 30, 520)uni.downloadFile({url:goodsCover,success: (res) => {// 商品图片ctx.drawImage(res.tempFilePath, 30, 70, 310, 310)//  二维码ctx.drawImage(qrcode, 240, 420, 110, 110)ctx.draw(false, () => {uni.canvasToTempFilePath({canvasId: canvasId,success: (res) => {console.log('临时图片路径:', res.tempFilePath);uni.saveImageToPhotosAlbum({filePath: res.tempFilePath,success: () => {uni.hideLoading()uni.showModal({title: '提示',content: ' 图片保存成功',showCancel: false,confirmText: '知道了',confirmColor: '#f36d00',success: res => {uni.navigateBack()}})}})},fail: (error) => {console.error('转化图片失败:', error);}},this)});}})},async doposter() {uni.showLoading({title: '正在生成海报'});await this.drawBackground();},
http://www.lryc.cn/news/291468.html

相关文章:

  • Vite+Vue3+TS 引入使用Cesium.js
  • Cocos creator 动作系统
  • 对Spring当中AOP的理解
  • 【Vue】2-8、Axios 网络请求
  • Vue中嵌入原生HTML页面
  • streampark+flink一键整库或多表同步mysql到doris实战
  • Vim实战:使用 Vim实现图像分类任务(二)
  • 学习MySQL ENUM数据类型
  • 88.合并两个有序数组
  • python查询xml类别
  • nginx配置及性能优化
  • 阿里云如何找回域名,进行添加或删除?
  • 机器学习 低代码 ML:PyCaret 的使用
  • 前端入门第二天
  • Django实现富文本编辑器Ckeditor5图片上传功能
  • 【C语言】epoll_wait / select
  • Java 数据抓取
  • 深度学习之处理多维特征的输入
  • 西瓜书读书笔记整理(十二) —— 第十二章 计算学习理论(下)
  • 初探分布式链路追踪
  • 闭包的理解?闭包使用场景
  • openssl3.2 - 帮助文档的整理
  • 中移(苏州)软件技术有限公司面试问题与解答(5)—— Linux进程调度参数调优是如何通过代码实际完成的1
  • 初识C语言·文件操作
  • 跨境卖家:如何利用自养号测评抢占市场先机?
  • 开发手札:Github Timeout 22
  • 学习鸿蒙基础(3)
  • 2024/1/27 备战蓝桥杯 1-2
  • 【PyQt】02-基本UI
  • 无需 Root 卸载手机预装软件,精简过的老年机又行了