uniapp使用l-painter画板,海报

在这里插入图片描述

Painter画板地址 http://liangei.gitee.io/limeui-docs/components/painter/
Painter画板在uniapp插件市场的地址:https://ext.dcloud.net.cn/plugin?id=2389
1,页面使用

<l-painter isRenderImage :width="'686rpx'" :height="'930rpx'" custom-style="position: fixed; top:10px; left: 500%;" ref="painter" @success="poster($event)"/>

2,导入组件

import lPainter from "@/components/lime-painter/index.vue"
components:{
	'l-painter':lPainter
},

3,画板样式

onShareLoad(){
	let that=this;
	let base={
		width: '686rpx',
		height: '950rpx',
		views: [
			{
				css: {
					width: '686rpx',
					height: '500rpx',
					display: "inline-block",
				},
				views: [
					{
						type: 'image',
						src: '../../static/bg1.png',
						mode: 'scaleToFill',
						css: {
							radius: '32rpx 32rpx 0rpx 0rpx',
							left: '0rpx',
							top: '0rpx',
							width: '686rpx',
							height: '500rpx',
						}
					}
				],
				type: "view"
			}, 
			{
				css: {
					width: '686rpx',
					height: '190rpx',
					display: "inline-block",
					background: '#ffffff',
				},
				views: [
					{
						type: 'text',
						text:'¥',
						css: {
							color: '#FE4F02',
							left: '30rpx',
							top: '550rpx',
							fontSize: '26rpx',
							lineHeight: '45rpx'
						}
					},
					{
						type: 'text',
						text:'1688.00',
						css: {
							color: '#FE4F02',
							left: '60rpx',
							top: '550rpx',
							fontWerght: 'bold',
							fontSize: '52rpx',
							lineHeight: '45rpx'
						}
					},
					{
						type: 'text',
						text: 'uniapp使用l-painter画板,海报uniapp使用l-painter画板,海报',
						css: {
							maxLines: 1,
							width: '630rpx',
							color: '#1A1A1A',
							left: '32rpx',
							top: '620rpx',
							fontSize: '32rpx',
							fontWerght: 'bold',
							lineHeight: '45rpx'
						}
					}
				],
				type: "view"
			},{
				css: {
					width: '686rpx',
					height: '52rpx',
					display: "inline-block",
				},
				views:[
					{
						type: 'image',
						src: '../../static/line.png',
						mode: 'scaleToFill',
						css: {
							left: '0rpx',
							top: '690rpx',
							width: '686rpx',
							height: '52rpx',
						}
					}
				],
				type: "view"
			},{
				css: {
					width: '686rpx',
					height: '180rpx',
					display: "inline-block",
					background: '#ffffff',
					radius: '0rpx 0rpx 32rpx 32rpx',
				},
				views:[
					{
						type: 'text',
						text: '购买热线',
						css: {
							left: '40rpx',
							top: '770rpx',
							fontSize: '28rpx',
							color:'#999999'
						}
					},
					{
						type: 'image',
						src:  '../../static/phone.png',
						mode: 'widthFix',
						css: {
							left: '184rpx',
							top: '770rpx',
							width: '36rpx',
							height: '36rpx',
							
						}
					},
					{
						type: 'text',
						text:  '手机号码',
						css: {
							left: '240rpx',
							top: '770rpx',
							fontSize: '28rpx',
							fontWerght: 'bold',
						}
					},
					{
						type: 'text',
						text:  '150-1234-4567',
						css: {
							left: '360rpx',
							top: '770rpx',
							fontSize: '28rpx',
							fontWerght: 'bold',
						}
					},
					{
						type: 'image',
						src:  '../../static/wechat.png',
						mode: 'widthFix',
						css: {
							left: '184rpx',
							top: '840rpx',
							width: '36rpx',
							height: '36rpx',
							
						}
					},
					{
						type: 'text',
						text:  '微信号码',
						css: {
							left: '240rpx',
							top: '840rpx',
							fontSize: '28rpx',
							fontWerght: 'bold',
						}
					},
					{
						type: 'text',
						text:  'csc1234',
						css: {
							left: '360rpx',
							top: '840rpx',
							fontSize: '28rpx',
							fontWerght: 'bold',
						}
					},
				],
				type: "view"
			},
			{
				css: {
					top:'360rpx',
					left:'480rpx',
					width: '170rpx',
					height: '220rpx',
					display: "inline-block",
					background: '#ffffff',
					borderRadius:'16rpx',
					boxShadow: "0 20rpx 58rpx rgba(0,0,0,.15)"
				},
				views:[
					{
						type: 'text',
						text: '产品详情二维码',
						css: {
							width:'280rpx',
							top: '540rpx',
							left: '488rpx',
							fontSize: '22rpx',
							color:'#1A1A1A'
						}
					},
					{
						type: 'image',
						src:  '../../static/logo.png',
						mode: 'widthFix',
						css: {
							top: '370rpx',
							left: '484rpx',
							width: '160rpx',
							height: '160rpx',
							borderRadius:'50%'
						}
					}
				],
				type:"view"
			}
		]
	}
	const painter = that.$refs.painter;
	painter.render(base);
},

4,下载海报,这里区分了h5下载和小程序下载

poster(event) {
	console.info(event)
	let path = event;
	let that=this;
	// #ifdef H5
	var eleLink = document.createElement('a');
	eleLink.download = "filename";
	eleLink.style.display = 'none';
	eleLink.href = path;
	document.body.appendChild(eleLink);
	eleLink.click();
	// 然后移除
	document.body.removeChild(eleLink);
	// #endif
	// #ifndef H5
	uni.saveImageToPhotosAlbum({
		filePath: path,
		success(res) {
			that.painterShow = false;
			that.show=false;
			uni.showToast({
				title: '已保存到相册',
				icon: 'success',
				duration: 2000
			})
		}
	})
	// #endif
},
### 关于 `l-painter-view` 组件的技术信息 #### 功能概述 `l-painter-view` 是用于在 uni-app 中创建可绘制视图的一个组件,允许开发者通过 JSON 配置来定义绘图内容。此方法简化了复杂图形界面的设计流程,并提供了灵活的数据驱动渲染机制[^3]。 #### 基本属性介绍 - **ref**: 设置当前实例的 `$refs` 名称以便后续操作该 DOM 节点。 - **isCanvasToTempFilePath**: 是否开启 canvas 导出图片功能,默认关闭。 - **pixel-ratio**: 定义导出图像时使用的像素比例,默认值为 1;提高数值可以得到更清晰的画面质量。 - **custom-style**: 自定义样式字符串,可用于调整位置和其他 CSS 属性。 #### 使用示例 下面是一个简单的例子展示如何利用 `<l-painter>` 和其子元素 `<l-painter-view>` 来构建一个带有背景颜色和文字描述的小程序卡片: ```html <template> <view class="container"> <!-- 显示生成后的图片 --> <image v-if="path" :src="path" mode="widthFix"></image> <!-- painter 组件 --> <l-painter ref="painter" isCanvasToTempFilePath @success="drawDone" pixel-ratio="2" custom-style="position:fixed;left:-9999px;"> <!-- view 子组件配置 --> <l-painter-view type="rect" backgroundColor="#ffffff"/> <!-- 添加更多 l-painter-view 可以组合成复杂的布局结构 --> <!-- 文字层 --> <l-painter-view type="text" text="Hello Painter!" fontSize="48" color="#000000" top="50%" left="50%" textAlign="center" /> </l-painter> <!-- 触发按钮 --> <button @click="createImage">生成图片</button> </view> </template> <script> export default { data() { return { path: '' }; }, methods: { createImage(){ this.$refs.painter.canvasToTempFilePath().then(res => { console.log('成功', res); this.path = res.tempFilePath; }).catch(err => { console.error('失败', err); }); }, drawDone(e){ console.log('完成回调:', e.detail); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值