当前位置:网站首页>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;}
边栏推荐
- Does flinkcdc have any solution for mysql's date field type conversion?
- Motion analysis and parameter optimization of crank-slider mechanism
- Practical training Navicat Chinese and English mode switching
- datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
- CSP-S2019兴奋不已
- 【南瓜书ML】(task4)神经网络中的数学推导(更新ing)
- 【音视频】srs直播平台搭建
- first unique character in characters
- More than 2022 cattle guest school game 4 yue
- uva10825
猜你喜欢
Introduction to the basic principles, implementation and problem solving of crawler
Offer刷题——1
dbeaver连接MySQL数据库及错误Connection refusedconnect处理
Data organization -- singly linked list of the linear table
Self-made a remote control software - VeryControl
WebSocket implements chat function
数据湖:数据同步工具NiFi
MVVM project development (commodity management system 1)
信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
rhcsa 第三次
随机推荐
sum of special numbers
torch
仿牛客网讨论社区项目—项目总结及项目常见面试题
目标检测概述-上篇
Golang:go模版引擎的使用
深度比较两个对象是否相同
小程序通过云函数操作数据库【使用get取数据库】
旋度(7)连接失败localhost8080;连接拒绝了
第5章——以程序方式处理MySQL数据表的数据
对于升级go1.18的goland问题
LeetCode240+312+394
CSP-S2019兴奋不已
ORACLE modify another user package (package)
Flip letters using string container
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
点餐系统数据库设计--SQL Server
mysql中添加字段的相关问题
响应式织梦模板园林花卉类网站
支付宝如何生成及配置公钥证书
More than 2022 cattle guest school game 4 yue