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

uniapp纯前端绘制商品分享图

效果如图
在这里插入图片描述

// useMpCustomShareImage.ts

interface MpCustomShareImageData {canvasId: stringprice: stringlinePrice: stringgoodsSpecFirmName: stringimage: string
}const CANVAS_WIDTH = 500
const CANVAS_HEIGHT = 400
const BG_IMAGE = 'https://public-scjuchuang.oss-cn-chengdu.aliyuncs.com/goods_share.png'
const DEFAULT_IMAGE ='https://public-scjuchuang.oss-cn-chengdu.aliyuncs.com/mini_program/mp_default_sc.png'const loadImageInfo = (src: string): Promise<UniApp.GetImageInfoSuccessData> => {return new Promise((resolve, reject) => {uni.getImageInfo({src,success: resolve,fail: reject,})})
}// 绘制价格
const loadPrice = (ctx: any, text: string, x: number, y: number) => {ctx.font = `bold ${text.length >= 10 ? '32px' : '36px'} sans-serif`ctx.setFillStyle('#e2231a')ctx.setTextAlign('center')ctx.setTextBaseline('middle')ctx.fillText(text, x, y)
}// 绘制划线价
const loadLinePrice = (ctx: any, text: string, x: number, y: number) => {ctx.font = '20px sans-serif'ctx.setFillStyle('#999999')ctx.fillText(text, x, y)const metrics = ctx.measureText(text)ctx.strokeStyle = '#999999'ctx.lineWidth = 2const textWidth = metrics.widthctx.beginPath()ctx.moveTo(x - textWidth / 2, y)ctx.lineTo(x + textWidth / 2, y)ctx.stroke()
}// 绘制规格厂家
const loadGoodsSpecFirmName = (ctx: any, text: string, y: number) => {const maxWidth = 480ctx.font = '20px sans-serif'ctx.setFillStyle('#333')// 短文字const textWidth = ctx.measureText(text).widthif (textWidth <= maxWidth) {return ctx.fillText(text, CANVAS_WIDTH / 2, y)}// 长文字,省略显示const displayText = text.substring(0, 21) + '...'ctx.fillText(displayText, CANVAS_WIDTH / 2, y)
}const useMpCustomShareImage = () => {const set = async (data: MpCustomShareImageData): Promise<string> => {const { canvasId, price, linePrice, goodsSpecFirmName, image = DEFAULT_IMAGE } = dataconst imageWidth = 250const imageHeight = 250try {const ctx = uni.createCanvasContext(canvasId, this)const [bgImageInfo, imageInfo] = await Promise.all([loadImageInfo(BG_IMAGE),loadImageInfo(image),])// 绘制背景图ctx.drawImage(bgImageInfo.path, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT)// 绘制商品图ctx.drawImage(imageInfo.path, 20, 80, imageWidth, imageHeight)// 绘制价格loadPrice(ctx, price, CANVAS_WIDTH - imageWidth / 2, 150)// 绘制划线价loadLinePrice(ctx, linePrice, CANVAS_WIDTH - imageWidth / 2, 200)// 绘制规格厂家loadGoodsSpecFirmName(ctx, goodsSpecFirmName, 360)// 绘制await new Promise<void>((resolve) => {ctx.draw(false, resolve)})// 生成分享图const { tempFilePath } = await new Promise<UniApp.CanvasToTempFilePathRes>((resolve, reject) => {uni.canvasToTempFilePath({canvasId,success: resolve,fail: reject,})},)return tempFilePath} catch (error) {console.error('生成分享图失败:', error)return image}}return { set }
}export default useMpCustomShareImage

// 调用

import useMpCustomShareImage from './useMpCustomShareImage'const mpCustomShareImage = useMpCustomShareImage()mpCustomShareImage.set({canvasId: 'shareCanvas',price: `¥4.60/盒`,linePrice: `¥4.73/盒`,goodsSpecFirmName: `0.25g*50粒 重庆科瑞制药(集团)有限公司`,image:'https://public-scjuchuang.oss-cn-chengdu.aliyuncs.com/mini_program/mp_default_sc.png',}).then((img) => {// 分享图console.log(img)})// -left-[1000px] -top-[1000px] 要放在页面上,且不显示
<canvascanvasId="shareCanvas"class="w-[500px] h-[400px] absolute -left-[1000px] -top-[1000px]"
/>
http://www.dtcms.com/a/332839.html

相关文章:

  • 18- 网络编程
  • 【学习笔记】Java并发编程的艺术——第10章 Executor框架
  • 从PDF到洞察力:基于飞算JavaAI的智能文档分析引擎实战
  • canoe面板中的进度条的使用
  • 分享一个基于Hadoop的二手房销售签约数据分析与可视化系统,基于Python可视化的二手房销售数据分析平台
  • AI工作流入门指南:从概念到实践
  • Redis 缓存和 Redis 分布式锁
  • SpringCloud -- Nacos详细介绍
  • Vue3从入门到精通: 4.5 数据持久化与同步策略深度解析
  • 电工的基础知识以及仪器的使用
  • linux下用c++11写一个UDP回显程序
  • 什么是敏感内容识别技术?
  • 平替 Claude Code,API接入 GPT-5,Codex CLI 国内直接使用教程
  • linux-数据链路层
  • ChatGPT-5(GPT-5)全面解析:一场从通用智能迈向专属智能的进化风暴,正在重塑人类与机器的协作边界
  • 当 FastGPT 遇见 Doris:无需手写 SQL,丝滑实现自助 ChatBI
  • wordpress忘记密码怎么办
  • 开源数据发现平台:Amundsen Frontend Service 应用程序配置
  • 专题三_二分_在排序数组中查找元素的第一个和最后一个位置
  • Jeecg后端经验汇总
  • Java: Spring前端传递列表和数组限制大小256问题
  • Vue 3 的运行机制
  • 前端开发神器之 VS Code AI 辅助插件 Amazon Q
  • expand.exe命令为什么能显示CD.iso的版本信息?
  • vue3 wangeditor5 编辑器,使用方法
  • 非极大值抑制(NMS)详解:目标检测中的“去重神器”
  • 基于 Vue2+Quill 的富文本编辑器全方案:功能实现与样式优化
  • 列式存储与行式存储:核心区别、优缺点及代表数据库
  • Flink Stream API 源码走读 - map 和 flatMap
  • ETH持续上涨推动DEX热潮,交易活跃度飙升的XBIT表现强势出圈