angular - 如何将div转换为图像?
问题描述
我有一个 div,我需要制作这个 div 的图像并发送到服务器。有没有办法使用Angular 7来做到这一点?我试图搜索库但没有结果。所有解决方法都使用本机 JS。
解决方案
要将 HTML 内容保存到图像中,您需要使用HTML2CANVAS
库
创建一些参考孩子
@ViewChild('screen') screen: ElementRef;
@ViewChild('canvas') canvas: ElementRef;
@ViewChild('downloadLink') downloadLink: ElementRef;
当然,您需要添加 HTML 引用,例如
<table #screen id="table" class="table table-striped">...
<div id="download">
<img #canvas>
<a #downloadLink></a>
</div>
然后创建一个函数来制作图像
downloadImage(){
html2canvas(this.screen.nativeElement).then(canvas => {
this.canvas.nativeElement.src = canvas.toDataURL();
this.downloadLink.nativeElement.href = canvas.toDataURL('image/png');
this.downloadLink.nativeElement.download = 'marble-diagram.png';
this.downloadLink.nativeElement.click();
});
}
推荐阅读
- java - 使用 Spring Boot 构建基本 Web 应用程序的不同方法?
- bash - bash - 根据 file2 中的 ID 在 file1 中查找名称
- assertj - Assertj + 使用元组时,对于一个字段如何检查它是否匹配多个值
- android-studio - Flutter 医生报告看到了两个 Android Studio 安装
- powershell - 在 powershell 中获取当前用户的 microsoft 帐户名称
- java - 布尔值不能在函数式编程java中取消引用
- unity3d - Unity 3d VR - 在控制器按钮上隐藏和显示模型点击
- r - 如何使用 dplyr 评估连续值及其下方的值?
- javascript - 在 Angular 4 中使用发布请求时出现 400 错误
- python-3.x - 为什么我应该使用 For 循环来读取 StratifiedShuffleSplit 输出?