Uniapp使用wxml-to-canvas进行动态页面转图片

news/2025/3/6 7:32:27/
参考文章链接:https://www.cnblogs.com/liangtao999/p/16719534.html
官方的链接:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/extended/component-plus/wxml-to-canvas.html

一、引入组件:

1、方法一:直接文件夹导入(我用的这种)

完整的包:https://github.com/ThaneYang/uniapp-wxml-to-canvas/tree/main/wxcomponents

① 方式一:

(1)在程序根目录下新建wxcomponents文件夹
(2)将上面的链接中 wxcomponents 的内容复制进来

② 方式二:

下载资源文件,将文件中的内容直接放在项目的根目录

资源文件链接:https://download.csdn.net/download/Y1914960928/90456850

2、方法二:npm 安装

npm install wxml-to-canvas

二、配置:

pages.json 文件:

"globalStyle": {"usingComponents": {"wxml-to-canvas": "/wxcomponents/wxml-to-canvas/index"}},

三、使用:

1、新建 domData.js 文件:

每个标签元素必须要设置宽高

const wxml = (detailData) => {let html = `<view class="canvasBox"><view class="viewBox"><text class="text1">页面ID:</text><text class="text2">${detailData.orderId}</text></view><image src="` + detailData.pic + `"  class="pic"/>`for(let i =0; i < detailData.textArr.length; i++) {html+=`<text class="content">` + detailData.textArr[i] + `</text>`}html+=`</view>`return html
}/*** @param {*} screenWidth 屏幕宽度* @param {*} canvasWidth  画布宽度* @param {*} canvasHeight  画布高度*/
const style = (screenWidth, canvasWidth, canvasHeight) => {return {"canvasBox": {width: canvasWidth,height: canvasHeight,position: 'relative',overflow: 'hidden',backgroundColor: '#ffffff',},"viewBox": {width: canvasWidth - 50,height: 40,flexDirection: 'row',textAlign: 'right',},"text1": {fontSize: 14,color: '#333',width: 80,height: 40,},"text2": {fontSize: 14,color: '#333',width: 80,height: 40,color: '#FF0000',},"content": {fontSize: 14,color: '#333',width: canvasWidth * 0.84,height: screenWidth * 0.15,marginLeft: 20,},"pic": {width: 56,height: 56,marginTop: 20,marginLeft: 50,marginBottom: 20,overflow: 'hidden',},}
}module.exports = {wxml,style
}

2、在 xx.vue 页面中写入:

<template><view class="container"><view class="btn" @click="showCanvas">查看</view><view v-if="canvasVisiable"><scroll-view scroll-y="true" class="share-page-box" id="box" :style="{width: canvasWidth + 'px', height: canvasHeight + 'px' ,margin: '30rpx auto',}"><wxml-to-canvas class="widget" :width="canvasWidth" :height="canvasHeight"></wxml-to-canvas></scroll-view></view></view>
</template><script>
const { wxml, style } = require('./domData.js')
export default {data() {return {canvasVisiable: false,canvasWidth: 320, // 默认canvas宽高canvasHeight: 480,screenWidth: null, // 设备宽度screenHeight: null, // 设备宽度detailData: {},widget: null,};},methods: {showCanvas() {this.canvasVisiable = truethis.detailData = {orderId: 'dsjkjjkjkn',pic: 'https://bkimg.cdn.bcebos.com/pic/a8014c086e061d950a7bebb265ac1dd162d9f3d35cba',textArr: ['文字1','文字2','文字3']}// 获取设备信息wx.getSystemInfo({success: (res) => {console.log("设备信息",res);this.screenWidth = res.screenWidththis.screenHeight = res.screenHeight // 高度建议计算得出或写死。如使用res.screenHeight,文字过长时无法生成// this.canvasWidth = this.screenWidth// this.canvasHeight = this.screenHeightthis.canvasWidth = this.screenWidth - 40this.canvasHeight = 1000this.show = true// 数字容器宽度 动态设置 setTimeout(() => {wx.showLoading({title: '图片生成中...'})this.widget = this.selectComponent('.widget')this.renderToCanvas()}, 1000)}});},renderToCanvas() {const _wxml = wxml(this.detailData) // 拿到html代码const _style = style(this.screenWidth, this.canvasWidth, this.canvasHeight)const p1 = this.widget.renderToCanvas({wxml: _wxml,style: _style})p1.then((res) => {console.log('图片生成成功');wx.hideLoading()}).catch((err) => {console.log('图片生成失败')})},}
};
</script><style>
.btn {width: 380rpx;height: 72rpx;background: #FFFFFF;margin: 20rpx auto;border-radius: 36rpx;border: 2rpx solid #3898FD;text-align: center;font-weight: 400;font-size: 28rpx;line-height: 72rpx;color: #3898FD;
}
</style>

四、最终效果:

在这里插入图片描述


http://www.ppmy.cn/news/1577010.html

相关文章

蓝桥杯4T平台(按键调整数量)

知识点&#xff1a;按键&#xff08;控制变量改变&#xff09;LCD 定义变量 uint8_t key_val,key_up,key_down,key_old; uint16_t X2,Y2,T; char buf[21]; 函数声明 uint8_t key_scan(void); void key_proc(void); void lcd_Proc(void); 按键 //按键扫描 uint8_t key_sca…

R语言绘图:韦恩图

韦恩分析 韦恩分析&#xff08;Venn Analysis&#xff09;常用于可视化不同数据集之间的交集和并集。维恩图&#xff08;Venn diagram&#xff09;&#xff0c;也叫文氏图、温氏图、韦恩图、范氏图&#xff0c;用于显示元素集合重叠区域的关系型图表&#xff0c;通过图形与图形…

【多模态目标检测】M2FNet:基于可见光与热红外图像的多模态融合目标检测网络

M2FNet&#xff1a;Multi-modal fusion network for object detection from visible and thermal infrared images M2FNet&#xff1a;基于可见光与热红外图像的多模态融合目标检测网络 0.论文摘要 融合可见光&#xff08;VIS&#xff09;和热红外&#xff08;TIR&#xff09;…

云原生边缘智能:构建分布式IoT设备的自主决策引擎

引言&#xff1a;突破云计算边界 Tesla自动驾驶系统通过边缘节点每秒处理2300帧图像&#xff0c;决策延迟<10ms。西门子工业大脑部署1000边缘集群&#xff0c;实现工厂故障预测准确率达99.3%。IDC预测2025年75%企业数据将在边缘产生&#xff0c;Gartner指出轻量化Kubernete…

构建自己的AI客服【根据用户输入生成EL表达式】

要实现一个基于对话形式的AI客服系统&#xff0c;该系统能够提示用户输入必要的信息&#xff0c;并根据用户的输入生成相应的EL&#xff08;Expression Language&#xff09;表达式编排规则&#xff0c;您可以按照以下步骤进行设计和开发。本文将涵盖系统架构设计、关键技术选型…

代码随想录第五十天| 图论理论基础

图论理论基础 这篇我们将正式开始学习图论&#xff01; 在代码随想录中&#xff0c;图论相关的算法题目将统一使用ACM模式。为什么要使用ACM模式呢&#xff1f; 图的基本概念 在二维坐标中&#xff0c;两点可以连成线&#xff0c;多个点连成的线就构成了图。 当然&#xff0…

人工智能神经网络基本原理

MP 神经元数学模型 MP 模型是神经网络领域的早期模型&#xff0c;它模仿了神经元的基本结构和工作原理。 人工神经元是一个多输入、单输出的信息处理单元&#xff0c;是对生物神经元的建模。建模方式可以有很多种&#xff0c;不同的建模方式就意味着不同的人工神经元结构。 比…

【第13节】C++设计模式(行为模式)-Template(模板)模式

一、问题的提出 Template 模式&#xff1a;算法步骤框架与细节实现的分离 假设我们正在开发一个文档处理系统&#xff0c;需要支持多种文档格式的导出&#xff08;如 PDF、Word、HTML 等&#xff09;。每种文档格式的导出过程大致相同&#xff0c;都包含以下步骤&#xff1a; …