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

前端用canvas绘图并支持下载

1.根据数据绘制饼图

/** 绘制环形图 */
const drawPieCharts = () => {const {canWithdrawalPriceFront,noWithdrawalPriceFront,haveWithdrawalPriceFront,} = this.state;const myCanvas = this.cavasRef.current;// @ts-ignoreconst ctx = myCanvas.getContext('2d');if (ctx) {//数据const data = [canWithdrawalPriceFront,noWithdrawalPriceFront,haveWithdrawalPriceFront,];/* 需要把数据转出弧度 先计算总数*/let total = 0;data.forEach((item) => {// @ts-ignoretotal += item;});if (total === 0) {return;}//计算每个数据所占的弧度const angleList: any = [];data.forEach((item) => {if (item !== 0) {// @ts-ignoreconst angle = Math.PI * 2 * (item / total);angleList.push(angle);} else {angleList.push(0);}});/* 颜色 */const colorArr = ['#5FE6E6', '#FFB356', '#69BAF8'];/** 圆心坐标 */const centerWidth = ctx.canvas.width / 2;const centerHeight = ctx.canvas.height / 2;/** 起始位置 */let startAngle = 0;// 根据弧度绘制扇形// @ts-ignoreangleList.forEach((item, index) => {// 上一次绘制的结束弧度等于当前次的起始弧度const endAngle = startAngle + item;ctx.beginPath();ctx.moveTo(centerWidth, centerHeight);ctx.arc(centerWidth, centerHeight, 120, startAngle, endAngle);ctx.fillStyle = colorArr[index];ctx.fill();// 记录当前的结束位置作为下一次的起始位置startAngle = endAngle;});}
}

2.绘制两张图片并下载


const downloadImg = (item: ICanvasProp) => {const {posterItemWidth,posterItemHeight,posterBgWidth,posterBgHeight,codeX,codeY,posterCodeBgWidth,posterCodeBgHeight,} = item;// 用canvas绘图,然后再下载const imgName = moment().format('YYYY-MM-DD HH:mm:SS');const canvas = document.createElement('canvas');const ctx = canvas.getContext('2d');canvas.width = posterItemWidth;canvas.height = posterItemHeight;if (ctx) {ctx.rect(0, 0, posterItemWidth, posterItemHeight);const bgImg = new Image();// 解决缓存导致跨域问题bgImg.src = item.bgImgUrl + `?tamp=${new Date().getTime()}`;bgImg.setAttribute('crossOrigin', 'anonymous');bgImg.onload = () => {ctx.drawImage(bgImg, 0, 0, posterBgWidth, posterBgHeight);const codeImg = new Image();// 解决缓存导致跨域问题codeImg.src = item.codeImgUrl + `?tamp=${new Date().getTime()}`;codeImg.setAttribute('crossOrigin', 'anonymous');codeImg.onload = () => {ctx.drawImage(codeImg, codeX, codeY, posterCodeBgWidth, posterCodeBgHeight);const link = document.createElement('a');link.setAttribute('download', `图片-${imgName}.png`);link.setAttribute('href', canvas.toDataURL('image/png'));link.style.display = 'none';document.body.appendChild(link);link.click();document.body.removeChild(link);};};}
}

http://www.lryc.cn/news/475070.html

相关文章:

  • 【Mac】Homebrew
  • Python笔记之线程库threading
  • go 包管理
  • Js内建对象
  • AXI接口的实现逻辑和底层原理,在FPGA中如何实现AXI接口,一篇文章足以搞明白!!!
  • 《GBDT 算法的原理推导》 11-12计算损失函数的负梯度 公式解析
  • mysql设计
  • Android 斗鱼面经
  • 【机器学习】26. 聚类评估方法
  • linux 最多能创建多少个 TCP 连接?
  • 我为何要用wordpress搭建一个自己的独立博客
  • Linux系统每日定时备份mysql数据
  • 书生大模型第一关Linux基础知识
  • 机器学习之fetch_olivetti_faces人脸识别--基于Python实现
  • 【系统设计】深入理解HTTP缓存机制:从Read-Through缓存到HTTP缓存的交互流程
  • FLINK单机版安装部署入门-1
  • 深度学习-学习率调整策略
  • 【学员提问bug】小程序在onUnload里面调接口,用来记录退出的时间, 但是接口调用还没成功, 页面就关闭了。如何让接口在onUnload关闭前调用成功?
  • 【刷题13】链表专题
  • Python Turtle模块详解与使用教程
  • 【PTA】4-2 树的同构【数据结构】
  • Node.js——fs模块-同步与异步
  • Java基于微信小程序的私家车位共享系统(附源码,文档)
  • vscode 创建 vue 项目时,配置文件为什么收缩到一起展示了?
  • PySpark任务提交
  • 【果蔬购物商城管理与推荐系统】Python+Django网页界面+协同过滤推荐算法+管理系统网站
  • 【大模型】海外生成式AI赛道的关键玩家:OpenAI、Anthropic之外还有谁?
  • kubevirt cloud-init配置
  • Oracle 大表添加索引的最佳方式
  • 速度了解云原生后端!!!