当前位置:网站首页>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;}边栏推荐
猜你喜欢

我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的

图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载

Dell PowerEdge Server R450 RAID Configuration Steps

测试工具(四)Jenkins环境搭建与使用

阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?

Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)

Golang:go静态文件处理

Using FiddlerScript caught poly FiddlerScript 】 【 download

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

太厉害了,终于有人能把文件上传漏洞讲的明明白白了
随机推荐
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
MySQL row locks and gap locks
第6章——数据库的安全性
爬虫基本原理介绍、实现以及问题解决
Self-made a remote control software - VeryControl
Vim三种模式
Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis
表的创建、修改与删除
Golang:go开启web服务
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
从零开始—仿牛客网讨论社区项目(一)
头歌MySQL数据库实训答案 有目录
深度比较两个对象是否相同
第5章——以程序方式处理MySQL数据表的数据
仿牛客网讨论社区项目—项目总结及项目常见面试题
响应式织梦模板园林景观类网站
图像基本操作的其他内容
从购买服务器到网站搭建成功保姆级教程~超详细
Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]
Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)