一个可以不用写复杂Canvas逻辑的画报解决方案,傻瓜式开发Canvas海报

一个可以不用写复杂Canvas逻辑的画报解决方案,傻瓜式开发Canvas海报,第1张

一个可以不用写复杂Canvas逻辑的画报解决方案,傻瓜式开发Canvas海报 介绍参数说明configimagesimages/configimages/config/roundtexts 原生微信小程序使用方法uniapp使用方法程序页面.vue 结尾

介绍

大家好! 这是一款可以通过JSON配置生成Canvas画报。
有原版本和uniapp版本
现阶段只能生成固定尺寸海报,如果有需求生成不固定商品图海报,等待2.0…

参数说明
参数类型是否必填说明
configArray配置项
config
参数类型是否必填说明
backgroundString海报背景色(默认白色)
canvasWidthString画布宽
canvasHeightString画布高
imagesArray图片或矩形配置项
textsArray文字配置项
images
参数类型是否必填说明
widthnumber矩形或者图片宽
heightnumber矩形或者图片高
urlString图片的Url(1、不填写就是矩形。2、可以填写网络路径可以是本地图。)
borderRadiusnumber圆角值
topnumber相对于画布Y轴值
leftnumber相对于画布X轴值
zIndexnumber层级
configArray矩形配置
images/config
参数类型是否必填说明
roundObjeck配置哪个方向的角是圆角,默认不传全是圆角
fillColorString矩形填充颜色
typeStringfill / stroke / both,填充/描边/填充和描边
strokeColorString描边色
images/config/round
参数类型是否必填说明
leftTopBoolean是否圆角
leftBottomBoolean是否圆角
rightTopBoolean是否圆角
rightBottomBoolean是否圆角
texts
参数类型是否必填说明
fontSizenumber字体大小
colornumber字体颜色(默认#000)
zIndexString层级
fontWeightnumber字体粗细
contentnumber文本内容
textAlignString文字位置 left / center / right
textBaselineStringtop、bottom、middle、normal 位置 (默认bottom)
topnumber相对于画布Y轴值
leftnumber相对于画布X轴值
maxLengthnumber文本最大长度(默认20)
linenumber可展示几行(默认1)
lineHeightnumber行高 (默认10)
原生微信小程序使用方法

A.wxml

// An highlighted block
 <Tool id="Tool" config="{{config}}" bindmyevent="onSuccessDraw"></Tool>
 <view style="display: flex;justify-content: space-between;width: 100%;margin-top: 30px;">
        <button bindtap="onDrawImage" style="width:30%;background: rgb(0, 122, 0);color: #fff;">
            生成图片
        </button>
        <button bindtap="onSaveImage" style="width:30%;background: rgb(0, 110, 161);color: #fff;">
            保存图片
        </button>
    </view>

A.js

// An highlighted block
Page({
    data: {
         config: {
            background: 'transparent',
            canvasWidth: 375,
            canvasHeight: 667,
            images: [
                {
                    width: 64,
                    height: 64,
                    url: '',
                    borderRadius: 32,
                    top: 35,
                    left: 20,
                    zIndex: 2,
                    config: {
                        round: {
                            leftTop: true, 
                            leftBottom: true, 
                            rightTop: true, 
                            rightBottom: true, 
                        },
                        fillColor: '#ff0000',
                        type: 'stroke',
                        strokeColor: '#000',
                    }
                }],
            texts: [
                {
                    fontSize: 16,
                    color: '#fff',
                    zIndex: 3,
                    fontWeight: 100,
                    content: '刘威威',
                    textAlign: 'left',
                    middle: '',
                    top: 60,
                    left: 92,
                    maxLength: 9,
                    line: 2
                }
            ],
        },
    // 绘图完成之后的回调函数
	onSuccessDraw(e) {
        this.setData({
            canvasImg: e.detail //生成海报-本地Url
        })
    },
    //点击或者默认执行方法 开始画图on
    onDrawImage() {
        let startPainting = this.selectComponent('#Tool')
       	startPainting.onStartPain()
    },
		
})
uniapp使用方法 程序页面.vue
<template>
	<view class="content">
		<CanvasToos ref="onToos" v-on:myevent="onSuccessDraw" :config="config"></CanvasToos>
		<image :src="canvasImg" style="width: 375px;height: 667px;"></image>
		<view style="display: flex;justify-content: space-between;width: 100%;margin-top: 30px;">
			<button @click="onDrawImage" style="width:30%;background: rgb(0, 122, 0);color: #fff;">
				生成图片
			</button>
			<button @click="onSaveImage" style="width:30%;background: rgb(0, 110, 161);color: #fff;">
				保存图片
			</button>
		</view>
	</view>
</template>

<script>
	//Canvas-Toos.vue 是组件的文件名
	import CanvasToos from '@/components/canvas-toos/Canvas-Toos.vue';
	export default {
		components: {
			CanvasToos
		},
		data() {
			return {
				canvasImg: '',
				config: {/**同上**/ },
			}
		},
		onLoad() {

		},
		methods: {
			onDrawImage() {
				this.$refs.onToos.onStartPain()
			},
			//绘图完成回调函数
			onSuccessDraw(e) {
				console.log('✅ 绘图完成', e)
				this.canvasImg = e
			},
		}
	}
</script>

结尾

基本使用方法就这些,如果有不懂的 || 插件获取。联系微信:NovLiu_08

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1297100.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存