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

embedded/2025/3/6 15:35:07/
参考文章链接: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/embedded/170509.html

相关文章

洛谷 P11830 省选联考2025 幸运数字 题解

题意 小 X 有 n n n 个正整数二元组 ( a i , b i ) ( 1 ≤ i ≤ n ) (a_i, b_i) (1 \leq i \leq n) (ai​,bi​)(1≤i≤n)。他将会维护初始为空的可重集 S S S&#xff0c;并对其进行 n n n 轮操作。第 i ( 1 ≤ i ≤ n ) i (1 \leq i \leq n) i(1≤i≤n) 轮操作中&#…

SPI硬件设计及通信原理解析

SPI(Serial Peripheral interface,串行外围设备接口),是一种高速的,全双工,同步通信总线。 SPI采用主从控制模式(Master--Slave)架构,一般有1个主设备、一个或多个从设备,使得主设备可以与多个从设备之间实现片间通信。 SPI在芯片管脚中只占用四根线节约了芯片的管脚…

记一次误禁用USB导致键盘鼠标失灵的修复过程

背景说明 在电脑上插入了一个USB hub&#xff0c;然后弹窗提示&#xff1a;“集线器端口上出现电涌”&#xff0c;点开让选择“重置”或者“关闭”&#xff0c;不小心点了关闭&#xff0c;结果这个usb口就被关了&#xff0c;再插任何东西都没反应&#xff0c;找了很多办法都恢…

前端基础之消息订阅与发布

需要下载npm I pubsub-js 在Student.vue中发送数据 <template> <div class"demo"> <h2 class"title">学生姓名:{{name}}</h2> <h2>学生性别:{{sex}}</h2> <button click"sendStudentName">将学生名给…

Android中的Loader机制

Android中的Loader机制是在Android 3.0版本之后引入的一种异步加载数据的方法。以下是对Android中Loader机制的详细解释&#xff1a; 一、Loader机制的基本概念 Loader是Android提供的一种异步加载数据的机制&#xff0c;它可以在后台线程中执行耗时操作&#xff0c;并在加载…

Java面试知识点总结

目录 基础篇 一、JAVA: 二、JDK ?JRE ?JVM的区别&#xff1a; 三、java的基本数据类型 四、switch默认接受的几种数据类型 五、修饰符的作用 六、Static修饰符 七、final,finally,finalize 三者区别 八、StringBuffer StringBuilder String 区别 九、String的常用方…

前端埋点项目从设计到实现详解

了解前端埋点 前端埋点详解 “前端埋点项目从设计到实现详解”的问题&#xff0c;并且要参考提供的20条证据。首先&#xff0c;我得先理解前端埋点的基本概念和它在项目中的作用。根据证据&#xff0c;前端埋点主要是通过在代码中嵌入脚本来收集用户行为数据&#xff0c;比如点…

颠覆NLP的魔法:深度解读Transformer架构及其核心组件

目录 颠覆NLP的魔法&#xff1a;深度解读Transformer架构及其核心组件 一、Transformer 架构概述 二、核心组件解析 1. Self-Attention&#xff08;自注意力机制&#xff09; 2. 位置编码&#xff08;Positional Encoding&#xff09; 3. 多头注意力&#xff08;Multi-Hea…