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

server/2025/3/9 20:53:59/
参考文章链接: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/server/173757.html

相关文章

文献阅读 | Nature Methods | SCENIC: 单细胞调控网络推断和聚类

文献介绍 文献题目&#xff1a; SCENIC: 单细胞调控网络推断和聚类 研究团队&#xff1a; Stein Aerts&#xff08;比利时鲁汶大学&#xff09; 发表时间&#xff1a; 2017-10-09 发表期刊&#xff1a; Nature Methods 影响因子&#xff1a; 25.0&#xff08;2017年&#xff09…

【通义万相】蓝耘智算 | 开源视频生成新纪元:通义万相2.1模型部署与测评

【作者主页】Francek Chen 【专栏介绍】 ⌈ ⌈ ⌈人工智能与大模型应用 ⌋ ⌋ ⌋ 人工智能&#xff08;AI&#xff09;通过算法模拟人类智能&#xff0c;利用机器学习、深度学习等技术驱动医疗、金融等领域的智能化。大模型是千亿参数的深度神经网络&#xff08;如ChatGPT&…

从李佳琦团队看新型用工:灵活就业如何重构组织架构?

2022年“双11”期间&#xff0c;李佳琦直播间累计销售额突破115亿元&#xff08;来源&#xff1a;新腕数据《2022双11直播电商战报》&#xff09;&#xff0c;其背后团队规模约400人&#xff0c;但全职员工仅占35%&#xff0c;其余65%为外包选品团队、兼职客服、第三方MCN机构人…

XML布局文件与常用View组件

XML布局文件与常用View组件 一、基础知识 1.1 XML布局简介 Android应用的用户界面是由View和ViewGroup对象的层次结构组成的。每个ViewGroup都是一个可以包含View对象的容器。XML布局文件提供了一种类似HTML的方式来描述这种视图层次结构。 1.2 常用布局属性 <!-- 常用…

AI学习有感

和前辈聊天&#xff0c;谈到了现在的ai技术&#xff0c;这里对那天的谈话进行总结&#xff1a; AI是无状态的 我们在使用ai时有时候会有一个错觉&#xff0c;认为和ai聊天久了&#xff0c;ai就会像人与人之间交流一样&#xff0c;会保留一种对聊天对象的认知状态&#xff0c;这…

GB28181视频监控流媒体平台LiveGBS如何自定义收流端口区间以便减少收流端口数或解决端口冲突问题

LiveGBS GB28181流媒体服务在接收视频的时候默认是使用30000-30249&#xff0c; webrtc流播放端口区间默认是UDP的30250-30500区间。有些网络环境不方便开放这么大的端口区间&#xff0c;下面介绍下如何修改配置这个区间。 从页面上修改这个区间&#xff0c;端口区间尽量设置大…

EXCEL自动化13 | 批量重命名工作簿中的工作表

目录 一. 重命名工作表1. 修改单个文件的工作表2. 修改单个文件的多个工作表3. 替换文件中的所有工作表名二. 批量重命名多个文件的工作表如下图所示,文件夹下有6个excel文件(工作簿)。打开任意一个工作簿,可看到其中有工作表,如 Sheet 1 。 要将6个工作簿中的工作表 “S…

Java多线程与高并发专题——关于CopyOnWrite 容器特点

引入 在 CopyOnWriteArrayList 出现之前&#xff0c;我们已经有了 ArrayList 和 LinkedList 作为 List 的数组和链表的实现&#xff0c;而且也有了线程安全的 Vector 和Collections.synchronizedList() 可以使用。 首先我们来看看Vector是如何实现线程安全的 &#xff0c;还是…