当前位置:网站首页>根据指定区域内容生成图片并进行分享总结
根据指定区域内容生成图片并进行分享总结
2022-08-01 06:52:00 【桐溪漂流】
需求及思路
- 需要把特定的页面区域生成图片
- 分享生成的图片
最终结论
- 可以根据
html2canvas
来生成特定的图片 - 普通浏览器中可以把生成的图片直接下载下来,最简单的就是通过
a
标签下载 - 微信中不允许直接下载,只能进行长按进行保存的操作
- 对保存的图片再进行转发分享
效果
这里只展示如何生成图片的,下载的就不做展示了
具体实现
本次尝试是在react项目中进行的,所以下面展示的代码直接是JSX的语法了
- 安装
html2canvas
依赖
// yarn add html2canvas
- 页面使用代码
import { Button, Image } from 'antd-mobile';
import styles from './index.less';
import { useCallback, useState } from 'react';
import ShareContainer from '@/components/ShareContainer';
// ShareContainer 是为了方便使用,创建的一个组件,大小依据外层容器的大小
// 生成的图片范围也为它的直接父级所覆盖的区域-本例中为蓝色线框的区域
export default function IndexPage() {
const [create, setCreate] = useState<any>();
const onClick = useCallback(() => {
create();
}, [create]);
return (
<div className={styles.layout}>
<ShareContainer setCreate={(create) => setCreate(create)}>
<div className={styles.content}>
<div className={styles.top}></div>
<div className={styles.btn}>
<Button color="primary" block onClick={onClick}>
click
</Button>
</div>
<div className={styles.bottom}>
<Image src='/123.png' />
</div>
</div>
</ShareContainer>
</div>
);
}
样式文件
.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
组件代码
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<any>();
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 (
<div className={styles.box + ' share-container'} ref={boxRef}>
<ImageViewer
image={url}
visible={visible}
onClose={() => {
setVisible(false);
}}
renderFooter={() => <div className={styles.tip}>长按保存图片</div>}
/>
{children}
</div>
);
}
// 如果需要在普通浏览器中进行下载。这里可以把 长按保存图片 换成<a download href={url}></a>标签
// 这样可以直接利用a标签进行下载生成的图片
样式文件
.box {
width: 100%;
height: 100%;
}
.tip {
width: 100%;
text-align: center;
font-size: 18px;
line-height: 60px;
color: #fff;
}
边栏推荐
- 【翻译】确保云原生通信的安全:从入口到服务网及更远的地方
- 阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
- Classwork (7) - #598. remainder operation (mod)
- Jupyter shortcuts
- 「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
- Offer brush questions - 1
- Vsce package after the Command failed: NPM list - production - parseable - the depth = 99999 - loglevel = error exception
- 【一句话攻略】彻底理解JS中的回调(Callback)函数
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
- Qt Widget 项目对qml的加载实例
猜你喜欢
mysql的行锁和间隙锁
我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer
插入排序—直接插入排序和希尔排序
Win任务栏图标异常解决
Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]
MVVM project development (commodity management system 1)
datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
Motion analysis and parameter optimization of crank-slider mechanism
牛客刷SQL---2
奇葩问题 npm install 报错 gyp ERR
随机推荐
Bean的生命周期
Solve the problem of page flicker caused by browser scroll bars
Detailed explanation of the crawler framework Scrapy
leetcode125 Verify palindrome string
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
基于百度OCR的网站验证码在线识别
支付宝如何生成及配置公钥证书
从零开始—仿牛客网讨论社区项目(一)
ORACLE 实现另外一个用户修改包(package)
NUMPY
曲柄滑块机构运动分析和参数优化
声音信号处理基频检测和时频分析
基于MATLAB的BP神经网络进行语音特征信号分类
我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer
【FiddlerScript】利用FiddlerScript抓包保利威下载
Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)
Vsce package after the Command failed: NPM list - production - parseable - the depth = 99999 - loglevel = error exception
LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
NIO编程
matlab 风速模型 小波滤波