当前位置:网站首页>Generate pictures based on the content of the specified area and share them with a summary
Generate pictures based on the content of the specified area and share them with a summary
2022-08-01 07:03:00 【Tongxi Rafting】
Needs and ideas
- You need to generate a picture for a specific page area
- Share the generated image
Final conclusion
- You can generate specific images according to
html2canvas
- The generated image can be downloaded directly in a normal browser. The easiest way is to download it through the
a
tag - Direct download is not allowed in WeChat, only long press to save
- Forward and share the saved picture
Effects
Here only shows how to generate pictures, and downloads will not be shown
Concrete implementation
This attempt is made in the react project, so the code shown below is directly the syntax of JSX
- Install
html2canvas
dependencies
// yarn add html2canvas
- Page usage code
import { Button, Image } from 'antd-mobile';import styles from './index.less';import { useCallback, useState } from 'react';import ShareContainer from '@/components/ShareContainer';// ShareContainer is a component created for convenience, the size is based on the size of the outer container// The resulting image extent is also the area covered by its immediate parent - in this case the area of the blue wireframeexport default function IndexPage() {const [create, setCreate] = useState();const onClick = useCallback(() => {create();}, [create]);return ( setCreate(create)}> );}
style file
.layout {width: 90%;height: 90%;border: 1px solid blue;}.content {width: 100%;height: 100%;.top {width: 100%;height: 128px;background: url(/avatar.png);background-size: 64px 64px;}.btn {width: 100%;margin: 8px 0;}.bottom {width: 100%;}}
ShareContainer
Component code
import { ImageViewer } from 'antd-mobile';import styles from './index.less';import html2canvas from 'html2canvas';import { useCallback, useEffect, useRef, useState } from 'react';interface Props {children: any;setCreate?: (create: any) => void;}export default function IndexPage({ children, setCreate }: Props) {const [url, setUrl] = useState('');const [visible, setVisible] = useState(false);const boxRef = useRef();const onCreate = useCallback(() => {if (boxRef.current) {html2canvas(boxRef?.current, { scale: 1 }).then(function (canvas) {const aurl = canvas.toDataURL('image/png');canvas.getContext('2d')?.save();setUrl(aurl);setVisible(true);});}}, [url]);useEffect(() => {if (setCreate) {setCreate(() => onCreate);}}, [setCreate]);return ( {setVisible(false);}}renderFooter={() => Long press to save image}/>{children} );}// If you need to download in a normal browser.Here you can change the long press to save the image to the tag// This can directly use the a tag to download the generated image
style file
.box {width: 100%;height: 100%;}.tip {width: 100%;text-align: center;font-size: 18px;line-height: 60px;color: #fff;}
边栏推荐
- 我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的
- 信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
- Motion analysis and parameter optimization of crank-slider mechanism
- Vim三种模式
- GO错误处理方式
- "By sharing" northwestern university life service | | bytes a second interview on three sides by HR
- 如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
- 戴尔PowerEdge服务器R450 RAID配置步骤
- 图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
- datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
猜你喜欢
matlab simulink 粒子群优化模糊pid控制的电机泵
Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
测试工具(四)Jenkins环境搭建与使用
爆肝3万字,最硬核丨Mysql 知识体系、命令全集 【建议收藏 】
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
插入排序—直接插入排序和希尔排序
MATLAB program design and application of MATLAB 2.5
日志导致线程Block的这些坑,你不得不防
随机推荐
leetcode125 Verify palindrome string
Golang:go连接和使用mysql
rhcsa 第三次
【一句话攻略】彻底理解JS中的回调(Callback)函数
信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
Self-made a remote control software - VeryControl
Using FiddlerScript caught poly FiddlerScript 】 【 download
安装SQL Server详细教程
uva12326
Practical training Navicat Chinese and English mode switching
戴尔PowerEdge服务器R450 RAID配置步骤
NIO编程
Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis
Offer brush questions - 1
Solve the problem of page flicker caused by browser scroll bars
响应式织梦模板园林花卉类网站
Data organization -- singly linked list of the linear table
特别数的和
[Translation] Securing cloud-native communications: From ingress to service mesh and beyond
问下 mysql向pg同步多个表的话 有什么好的方案吗?