当前位置: 首页 > news >正文

uniapp 使用cavans 生成海报

uniapp 使用cavans 生成海报

npm install qs-canvas

1.创建 useCanvas.js

/*** Shopro + qs-canvas 绘制海报* @version 1.0.0* @author lidongtony* @param {Object} options - 海报参数* @param {Object} vm - 自定义组件实例*/
import QSCanvas from 'qs-canvas';
import { getPosterData } from './poster';export default async function useCanvas(options, vm) {const width = options.width;const qsc = new QSCanvas({canvasId: options.canvasId,width: options.width,height: options.height,setCanvasWH: (canvas) => {options.height = canvas.height;},},vm,);let drawer = getPosterData(options);// 绘制背景图const background = await qsc.drawImg({type: 'image',val: drawer.background,x: 0,y: 0,width,mode: 'widthFix',zIndex: 0,});await qsc.updateCanvasWH({width: background.width,height: background.bottom,});let list = drawer.list;for (let i = 0; i < list.length; i++) {let item = list[i];// 绘制文字if (item.type === 'text') {await qsc.drawText(item);}// 绘制图片if (item.type === 'image') {if (item.d) {qsc.setCircle({x: item.x,y: item.y,d: item.d,clip: true,});}if (item.r) {qsc.setRect({x: item.x,y: item.y,height: item.height,width: item.width,r: item.r,clip: true,});}await qsc.drawImg(item);qsc.restore();}// 绘制二维码if (item.type === 'qrcode') {await qsc.drawQrCode(item);}}await qsc.draw();// 延迟执行, 防止不稳定setTimeout(async () => {options.src = await qsc.toImage();}, 100);return options;
}

index.vue

<template><canvasclass="hideCanvas":canvas-id="poster.canvasId":id="poster.canvasId":style="{height: poster.height + 'px',width: poster.width + 'px',}"/></template><script setup>import useCanvas from './useCanvas';const props = defineProps({shareInfo: {type: Object,default() {},},});const Sys = uni.getSystemInfoSync();const poster = reactive({canvasId: 'canvasId',width: Sys.device.windowWidth * 0.9,height: 600,src: '',});async function getPoster() {poster.src = '';poster.shareInfo = props.shareInfo;// #ifdef APP-PLUSposter.canvasId = 'canvasId-' + new Date().getTime();// #endifconst canvas = await useCanvas(poster, vm);return canvas;}
</script>

poster/index.js

import user from './user';
export function getPosterData(options) {switch (options.shareInfo.poster.type) {case 'user':return user(options);}
}export function formatImageUrlProtocol(url) {// #ifdef H5// H5平台 https协议下需要转换if (window.location.protocol === 'https:' && url.indexOf('http:') === 0) {url = url.replace('http:', 'https:');}// #endif// #ifdef MP-WEIXIN// 小程序平台 需要强制转换为https协议if (url.indexOf('http:') === 0) {url = url.replace('http:', 'https:');}// #endifreturn url;
}

需要不同的海报 就创建不同 js 文件 存放你需要画的那些元素
poster/user.js

import { formatImageUrlProtocol } from './index';const user = (poster) => {const width = poster.width;//你需要拼接的参数const userInfo = {};return {background: formatImageUrlProtocol('httpxxxxxxxx'),list: [{name: 'nickname',type: 'text',val: userInfo.nickname,x: width / 2,y: width * 0.4,paintbrushProps: {textAlign: 'center',fillStyle: '#333',font: {fontSize: 14,fontFamily: 'sans-serif',},},},{name: 'avatar',type: 'image',val: formatImageUrlProtocol('httpxxxxxx'),x: width * 0.4,y: width * 0.16,width: width * 0.2,height: width * 0.2,d: width * 0.2,},// #ifndef MP-WEIXIN{name: 'qrcode',type: 'qrcode',val: poster.shareInfo.link,x: width * 0.35,y: width * 0.84,size: width * 0.3,},// #endif// #ifdef MP-WEIXIN{name: 'wxacode',type: 'image',val: 'httpxxxxxxxxxxx',x: width * 0.35,y: width * 0.84,width: width * 0.3,height: width * 0.3,},// #endif],};
};export default user;

在这里插入图片描述

相关文章:

  • 使用docker搭建squid和ss5
  • Docker Compose 一键快速部署 RocketMQ
  • SpringBoot3集成MyBatisPlus
  • 【鸿蒙学习笔记】Image迭代完备
  • 难道 Java 已经过时了?
  • Android 内存原理详解以及优化(二)
  • 边缘网关在实际应用中的价值与挑战-天拓四方
  • 66.前端接口调用返回400的错误
  • OpenBMB × Hugging Face × THUNLP,大模型课开班丨伙伴活动推荐
  • P3374 【模板】树状数组 1
  • 【C++】开源:量化金融计算库QuantLib配置与使用
  • 【简单讲解神经网络训练中batch的作用】
  • Python知识点背诵手册,超详细知识梳理
  • ENVI5.6使用笔记
  • 开启IT世界的第一步:高考新生的暑期学习指南
  • Druid 在有赞的实践
  • echarts的各种常用效果展示
  • Github访问慢解决办法
  • iOS小技巧之UIImagePickerController实现头像选择
  • Java的Interrupt与线程中断
  • JSDuck 与 AngularJS 融合技巧
  • Redis 懒删除(lazy free)简史
  • 百度地图API标注+时间轴组件
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 从零搭建Koa2 Server
  • 读懂package.json -- 依赖管理
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 计算机在识别图像时“看到”了什么?
  • 实战|智能家居行业移动应用性能分析
  • 应用生命周期终极 DevOps 工具包
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • ​​​​​​​​​​​​​​Γ函数
  • ​Java基础复习笔记 第16章:网络编程
  • ​Linux·i2c驱动架构​
  • # C++之functional库用法整理
  • $().each和$.each的区别
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (2.2w字)前端单元测试之Jest详解篇
  • (C语言)fread与fwrite详解
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (独孤九剑)--文件系统
  • (二)WCF的Binding模型
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (汇总)os模块以及shutil模块对文件的操作
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (数据结构)顺序表的定义
  • (转)mysql使用Navicat 导出和导入数据库
  • .describe() python_Python-Win32com-Excel
  • .DFS.
  • .Net Core和.Net Standard直观理解
  • .Net IE10 _doPostBack 未定义
  • .net MySql
  • .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  • .net连接oracle数据库