当前位置:网站首页>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;}边栏推荐
- 插入排序—直接插入排序和希尔排序
- 2022.7.27 Selected lectures on good topics
- The BP neural network based on MATLAB voice characteristic signal classification
- datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
- Detailed explanation of the crawler framework Scrapy
- 我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的
- matlab 风速模型 小波滤波
- 仿牛客网讨论社区项目—项目总结及项目常见面试题
- return;代表含义
- Motion analysis and parameter optimization of crank-slider mechanism
猜你喜欢

如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法

从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)

Dell PowerEdge Server R450 RAID Configuration Steps

rhcsa 第四天

Srping bean in the life cycle

Introduction to the basic principles, implementation and problem solving of crawler

爬虫基本原理介绍、实现以及问题解决

Golang:go开启web服务

curl (7) Failed connect to localhost8080; Connection refused

插入排序—直接插入排序和希尔排序
随机推荐
曲柄滑块机构运动分析和参数优化
监听父元素宽高,自适应插件大小
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
2022杭电多校第二场1011 DOS Card(线段树)
Create, modify and delete tables
Golang:go模版引擎的使用
小白的0基础教程SQL: 安装MYSQL 03
datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
响应式织梦模板园林景观类网站
Offer brush questions - 1
关于App不同方式更新的测试点归纳
响应式织梦模板园林花卉类网站
curl (7) Failed connect to localhost8080; Connection refused
Datagrip error "The specified database userpassword combination is rejected..."Solutions
Win任务栏图标异常解决
uva10825
Json对象和Json字符串的区别
插入排序—直接插入排序和希尔排序
LeetCode 0149. 直线上最多的点数
【南瓜书ML】(task4)神经网络中的数学推导(更新ing)