小程序海报生成神器之一lime-painter配合uniapp简单使用示例

组件介绍

lime-painter 是一个运行在uniapp上优雅的海报生成插件,支持JSON方式和template方式生成海报

资源

完整demo: https://gitee.com/hackchen/demo-collection/tree/master/front-end/uniapp/lime-painter-demo

需要注意的问题
  1. 包含图片最好的地址最好要支持跨域

  2. nvue 必须为HBX 3.4.11及以上

使用步骤
  1. 安装lime-painter

从完整demo中的 uni_modules/lime-painter 目录复制到你的uniapp项目的 uni_modules 目录下

  1. 新建页面 pages/index/demo.vue,页面内容如下
<script setup>
import {ref} from 'vue'
import {onLoad} from '@dcloudio/uni-app'

// 海报元素的引用,用于后续操作DOM
const posterRef = ref(null);

// 控制海报是否显示
const posterIsShow = ref(false);

// 存储最终生成的海报图片URL
const pictureImage = ref('');

// 海报的JSON配置,包含CSS样式和视图层次结构
const posterJson = ref({
  css: {
    width: '750rpx',
    paddingBottom: '40rpx',
    background: 'linear-gradient(,#000 0%, #ff5000 100%)'
  },
  views: [ // 这里配置了多个视图元素,包括图片、文本和容器,每个都有自己的CSS样式
    {
      src: 'https://m.360buyimg.com/babel/jfs/t1/196317/32/13733/288158/60f4ea39E6fb378ed/d69205b1a8ed3c97.jpg',
      type: 'image',
      css: {
        background: '#fff',
        objectFit: 'cover',
        marginLeft: '40rpx',
        marginTop: '40rpx',
        width: '84rpx',
        border: '2rpx solid #fff',
        boxSizing: 'border-box',
        height: '84rpx',
        borderRadius: '50%'
      }
    },
    {
      type: 'view',
      css: {
        marginTop: '40rpx',
        paddingLeft: '20rpx',
        display: 'inline-block'
      },
      views: [
        {
          text: '隔壁老王2',
          type: 'text',
          css: {
            display: 'block',
            paddingBottom: '10rpx',
            color: '#fff',
            fontSize: '32rpx',
            fontWeight: 'bold'
          }
        },
        {
          text: '为您挑选了一个好物',
          type: 'text',
          css: {
            color: 'rgba(255,255,255,.7)',
            fontSize: '24rpx'
          }
        }
      ]
    },
    {
      css: {
        marginLeft: '40rpx',
        marginTop: '30rpx',
        padding: '32rpx',
        boxSizing: 'border-box',
        background: '#fff',
        borderRadius: '16rpx',
        width: '670rpx',
        boxShadow: '0 20rpx 58rpx rgba(0,0,0,.15)'
      },
      views: [
        {
          src: 'https://m.360buyimg.com/babel/jfs/t1/196317/32/13733/288158/60f4ea39E6fb378ed/d69205b1a8ed3c97.jpg',
          type: 'image',
          css: {
            objectFit: 'cover',
            objectPosition: '50% 50%',
            width: '606rpx',
            height: '606rpx'
          }
        },
        {
          css: {
            marginTop: '32rpx',
            color: '#FF0000',
            fontWeight: 'bold',
            fontSize: '28rpx',
            lineHeight: '1em'
          },
          views: [
            {
              text: '¥',
              type: 'text',
              css: {
                verticalAlign: 'bottom'
              }
            },
            {
              text: '39',
              type: 'text',
              css: {
                verticalAlign: 'bottom',
                fontSize: '58rpx'
              }
            },
            {
              text: '.39',
              type: 'text',
              css: {
                verticalAlign: 'bottom'
              }
            },
            {
              text: '¥59.99',
              type: 'text',
              css: {
                verticalAlign: 'bottom',
                paddingLeft: '10rpx',
                fontWeight: 'normal',
                textDecoration: 'line-through',
                color: '#999999'
              }
            }
          ],

          type: 'view'
        },
        {
          css: {
            marginTop: '30rpx',
            fontSize: '26rpx',
            color: '#8c5400'
          },
          views: [
            {
              text: '满100减11',
              type: 'text',
              css: {
                color: '#ff6200',
                border: '1rpx solid #ff6200',
                padding: '10rpx 16rpx 4rpx 16rpx',
                fontSize: '24rpx'
              }
            }
          ],

          type: 'view'
        },
        {
          css: {
            marginTop: '10rpx'
          },
          views: [
            {
              text: '360儿童电话手表9X 智能语音问答定位支付手表 4G全网通20米游泳级防水视频通话拍照手表男女孩星空蓝',
              type: 'text',
              css: {
                paddingRight: '32rpx',
                marginTop: '16rpx',
                boxSizing: 'border-box',
                lineClamp: 2,
                color: '#333333',
                lineHeight: '48rpx',
                fontSize: '30rpx',
                width: '478rpx'
              }
            },
            {
              text: 'limeui.qcoon.cn',
              type: 'qrcode',
              css: {
                width: '128rpx',
                height: '128rpx'
              }
            }
          ],
          type: 'view'
        }
      ],
      type: 'view'
    }
  ]
});

/**
 * 绘制海报成功的回调函数
 * @param {string} e 生成的海报图片数据URL
 * @summary 绘制海报成功后,将海报显示出来,并隐藏加载提示。
 */
const painterSsuccess = (e) => {
  console.log('painterSsuccess');
  posterIsShow.value = true;
  pictureImage.value = e;
  uni.hideLoading()
};
const renderPoster = () => {
  posterRef.value.render(posterJson.value);
}
onLoad(() => {
	uni.showLoading({
		title: '正在生成海报',
		icon: 'loading'
	})
  setTimeout(() => {
    // 需要延迟,不然会报错
    renderPoster();
  },1000)
})
</script>

<template>
<!-- 显示图像的元素 -->
  <image :src="pictureImage" v-if="pictureImage" mode="widthFix" style="width: 700rpx"></image>
  <l-painter
      ref="posterRef"
      @success="painterSsuccess"
      isCanvasToTempFilePath
      performance
      path-type="url"
      custom-style="position: fixed; left: 200%"
  >
  </l-painter>
</template>

<style scoped>

</style>
  1. 修改pages.json,内容如下
    {
	"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages

		{
			"path": "pages/index/demo",
			"style": {
				"navigationBarTitleText": "海报demo"
			}
		},
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "uni-app"
			}
		}
	],
	"globalStyle": {
		"navigationBarTextStyle": "black",
		"navigationBarTitleText": "uni-app",
		"navigationBarBackgroundColor": "#F8F8F8",
		"backgroundColor": "#F8F8F8"
	},
	"uniIdRouter": {}
}

插件市场地址:https://ext.dcloud.net.cn/plugin?id=2389

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp是一个基于Vue.js的跨平台应用开发框架,它支持使用Web技术构建原生应用。Lime-Echarts是一个轻量级的ECharts封装,用于在UniApp中集成高性能的数据可视化图表。按需引入Lime-Echarts可以减少应用的初始加载体积,提高用户体验。 按需引入Lime-Echarts的步骤如下: 1. **安装依赖**:首先确保你在项目中已经安装了Lime-Echarts和相关的依赖,可以使用npm或yarn进行安装: ``` npm install @vant/lime-charts --save ``` 2. **引入组件**:在需要使用Echarts的地方,只导入你需要的图表类型: ```javascript import { Line } from '@vant/lime-charts'; ``` 或者使用ES6模块语法: ```javascript import Line from '@vant/lime-charts/dist/components/line.vue'; ``` 3. **按需使用**:在组件的`<template>`中,只加载你实际需要显示的图表: ```html <van-lazy-render :once="true" :load="loadChart"> <template> <line ref="chart" :options="chartOptions"></line> </template> </van-lazy-render> ``` 在`methods`里定义`loadChart`方法,动态初始化并设置图表配置: ```javascript methods: { async loadChart() { if (this.shouldLoadChart) { // 初始化图表配置,例如 this.chartOptions = { // ... Echarts配置项 }; this.$refs.chart.init(this.chartOptions); } }, shouldLoadChart: false, // 初始设置为false,只有在需要时才切换为true } ``` 4. **动态加载**:在首次渲染时,`shouldLoadChart`保持为`false`,当需要显示图表时,将其设置为`true`,触发`loadChart`方法加载图表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值