当前位置:网站首页>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
atag - 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
html2canvasdependencies
// 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%;}}ShareContainerComponent 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;}边栏推荐
- Practical training Navicat Chinese and English mode switching
- 2022.7.26 Mock Competition
- 爬虫框架 Scrapy 详解
- [Translation] Securing cloud-native communications: From ingress to service mesh and beyond
- 【南瓜书ML】(task4)神经网络中的数学推导(更新ing)
- Go 支持 OOP: 用 struct 代替 class
- Dbeaver connect the MySQL database and error Connection refusedconnect processing
- 从零开始—仿牛客网讨论社区项目(一)
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
- 信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
猜你喜欢

Srping中bean的生命周期

Using FiddlerScript caught poly FiddlerScript 】 【 download

【FiddlerScript】利用FiddlerScript抓包保利威下载

Data organization -- singly linked list of the linear table

【视觉SLAM十四讲】第一章理论详解

响应式织梦模板园林花卉类网站

The BP neural network based on MATLAB voice characteristic signal classification

MVVM项目开发(商品管理系统一)

LeetCode 0149. 直线上最多的点数

"By sharing" northwestern university life service | | bytes a second interview on three sides by HR
随机推荐
return; represents meaning
CSP-S2019兴奋不已
爬虫基本原理介绍、实现以及问题解决
旋度(7)连接失败localhost8080;连接拒绝了
升级为重量级锁,锁重入会导致锁释放?
Dell PowerEdge Server R450 RAID Configuration Steps
响应式织梦模板园林花卉类网站
uva10825
响应式织梦模板园林景观类网站
Bean的生命周期
仿牛客网讨论社区项目—项目总结及项目常见面试题
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer
Qt Widget project loading example of qml
图像基本操作的其他内容
sum of special numbers
Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis
插入排序—直接插入排序和希尔排序
05-SDRAM: Arbitration
Practical training Navicat Chinese and English mode switching