百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

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

yuyutoo 2024-10-11 21:37 9 浏览 0 评论

组件介绍

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

资源

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

需要注意的问题

  • 包含图片最好的地址最好要支持跨域
  • 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": {}
}

相关推荐

深度解读Spring框架的核心原理

深度解读Spring框架的核心原理在Java开发的世界里,提到Spring框架,就像提起一位久经沙场的老将,它几乎成了企业级应用开发的代名词。那么,这个被无数开发者膜拜的框架究竟有何独特之处?今天,我...

「Spring认证」Spring 框架概述

Spring是最流行的企业Java应用程序开发框架。全球数以百万计的开发人员使用SpringFramework来创建高性能、易于测试和可重用的代码。Spring框架是一个开源的Java...

学习Spring框架 这一篇就够了

1.spring概述1.1Spring是什么(理解)...

Spring框架双核解析:IOC与AOP的本质与实战

#Spring核心#IOC容器#AOP编程#Java框架设计...

Spring Boot与传统Spring框架的对比:探索Java开发的新境界

SpringBoot与传统Spring框架的对比:探索Java开发的新境界在Java生态系统中,Spring框架无疑是一个里程碑式的存在。从最初的简单依赖注入容器,到如今覆盖企业级开发方方面面的庞大...

Spring MVC框架源码深度剖析:从入门到精通

SpringMVC框架源码深度剖析:从入门到精通SpringMVC框架简介SpringMVC作为Spring框架的一部分,为构建Web应用程序提供了强大且灵活的支持。它遵循MVC(Model-V...

Spring框架入门

一.spring是什么?Spring是分层...

程序员必知必会技能之Spring框架基础——面向切面编程!

面向切面编程AOP(AspectOrientedProgramming)与OOP(ObjectOrientedProgramming,面向对象编程)相辅相成。AOP提供了与OOP不同的抽象软件结...

Spring Security安全框架深度解读:为你的应用穿上“钢铁铠甲”

SpringSecurity安全框架深度解读:为你的应用穿上“钢铁铠甲”在现代网络世界里,保护我们的应用程序免受各种威胁攻击至关重要。而在这个过程中,SpringSecurity框架无疑是我们最可...

Spring框架的设计哲学与实现:打造轻量级的企业级Java应用

Spring框架的设计哲学与实现:打造轻量级的企业级Java应用Spring框架自2003年诞生以来,已成为企业级Java应用开发的代名词。它不仅仅是一个框架,更是一种设计理念和哲学的体现。本文将带你...

Spring框架深度解析:从核心原理到底层实现的全方位避坑指南

一、Spring框架核心概念解析1.控制反转(IoC)与依赖注入(DI)Spring的核心思想是通过IoC容器管理对象的生命周期和依赖关系。传统开发中,对象通过new主动创建依赖对象,导致高耦合;而S...

Java框架 —— Spring简介

简介一般来说,Spring指的是SpringFramework,它提供了很多功能,例如:控制反转(IOC)、依赖注入...

Spring 框架概述,模块划分

Spring框架以控制反转(InversionofControl,IoC)和面向切面编程(Aspect-OrientedProgramming,AOP)为核心,旨在简化企业级应用开发,使开发者...

spring框架怎么实现依赖注入?

依赖注入的作用就是在使用Spring框架创建对象时,动态的将其所依赖的对象注入到Bean组件中,其实现方式通常有两种,一种是属性setter方法注入,另一种是构造方法注入。具体介绍如下:●属性set...

Spring框架详解

  Spring是一种开放源码框架,旨在解决企业应用程序开发的复杂性。一个主要优点就是它的分层体系结构,层次结构让你可以选择要用的组件,同时也为J2EE应用程序开发提供了集成框架。  Spring特征...

取消回复欢迎 发表评论: