Using html2canvas to generate shared images, CDN images do not show the problem

qwer 2021-04-07 21:41:06
using html2canvas html canvas generate


Generate shared images , Contains the text and text returned by the back end , The picture is cdn resources . Start using html2canvas when , Using local images , No problem generating screenshots .

Code implementation

import html2canvas from 'html2canvas';
const shareContent = document.getElementById("capture"); // The content area to generate the screenshot 
(window.html2canvas || html2canvas)(shareContent, {
useCORS: true, // Allow cross-domain + Set up <img crossOrigin="Anonymous" />
scrollY: 0,
scrollX: 0,
height:shareContent.offsetHeight - 2,
}).then((canvas) => {
console.log(" Generate the whole picture ");
setUrl(canvas.toDataURL("image/png", 1))
 Copy code 


But instead cdn Resource time , stay ios Random on the device The screenshot is empty 、 The picture can't show the situation .


Because using local images , No such problem , So the front end is taking pictures cdn Resource time ,

  1. Convert resources asynchronously to base64
  2. take base64 Assign a value to img label
  3. Finally, execute the code to generate the screenshot

After the test, the problem is solved

Code implementation

  1. You need to load more than one cdn picture , So use Promise Processing multiple cdn Turn the picture into base64
// Pass in cdn Address 
function convertUrlToBase64(url) {
return new Promise((resolve) => {
let img = new Image();
img.crossOrigin = 'Anonymous';
img.src = url;
img.onload = function () {
let canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
let ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
let ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase();
let dataURL = canvas.toDataURL('image/' + ext);
let base64 = {
dataURL: dataURL,
type: 'image/' + ext,
ext: ext
 Copy code 
  1. Achieve asynchronous loading of multiple images , take base64 Assign a value to img label , Finally, execute to generate a screenshot
const bgImg = document.getElementsByClassName('photo')[0];
const avatarImg = document.getElementsByClassName('avatar')[0]
Promise.all([convertUrlToBase64(bgImg.src), convertUrlToBase64(avatarImg.src)])
.then(([base641,base642]) => {
bgImg.src = base641.dataURL;
avatarImg.src = base642.dataURL;
(window.html2canvas || html2canvas)(shareContent, {
useCORS: true,
scrollY: 0,
scrollX: 0,
height:shareContent.offsetHeight - 2,
}).then((canvas) => {
console.log(" Generate the whole picture ");
setUrl(canvas.toDataURL("image/png", 1))
 Copy code 


html2canvas Generate screenshots ,cdn The picture does not show that the problem has been solved perfectly .

Encountered in the process of using , When generating a screenshot , The whole text moves down , It's not settled yet , In the official view issue…


  1. Behind the miracle of the sixth championship is the football with AI blessing in the Bundesliga
  2. An easy to use Visual Studio code extension - live server, suitable for front-end gadget development
  3. 用 Python 抓取公号文章保存成 HTML
  4. User login of front end spa project based on Vue and Quasar (2)
  5. Summary of common selectors in CSS
  6. Using Python to grab articles with public number and save them as HTML
  7. To "restless" you
  8. 【免费开源】基于Vue和Quasar的crudapi前端SPA项目实战—环境搭建 (一)
  9. 【微信小程序】引入阿里巴巴图标库iconfont
  10. layui表格点击排序按钮后,表格绑定事件失效解决方法
  11. Unity解析和显示/播放GIF图片,支持http url,支持本地file://,支持暂停、继续播放
  12. 【vue】 export、export default、import的用法和区别
  13. [free and open source] crudapi front end spa project based on Vue and Quasar
  14. [wechat applet] introduces Alibaba icon library iconfont
  15. Layui table click Sort button, table binding event failure solution
  16. Element树形控件Tree踩坑:修改current-node-key无效
  17. Unity parses and displays / plays GIF images, supports HTTP URL, supports local file: / /, supports pause and resume playback
  18. Element树形控件Tree踩坑:修改current-node-key无效
  19. The usage and difference of export, export default and import
  20. Element tree control: invalid to modify current node key
  21. Element tree control: invalid to modify current node key
  22. linux下安装apache(httpd-2.4.3版本)各种坑
  23. How to install Apache (httpd-2.4.3) under Linux
  24. 程序员业余时间写的代码也算公司的?Nginx之父被捕引发争议
  25. Nacos serialize for class [] failed.
  26. Do programmers write code in their spare time? Controversy over the arrest of nginx's father
  27. Nacos serialize for class [ . common.http.HttpRestResult ] failed.
  28. Seamless management of API documents using eolink and gitlab
  29. vue 的基础应用(上)
  30. 28岁开始零基础学前端,这些血的教训你一定要避免
  31. Basic application of Vue
  32. Starting at the age of 28, you must avoid these bloody lessons
  33. Ubuntu 16.04 can not connect to the wireless solution and QQ installation
  34. Industry security experts talk about the rapid development of digital economy, how to guarantee the security of data elements?
  35. 利用Vue实现一个简单的购物车功能
  36. Behind the "tireless classroom" and teacher training, can byte education really "work wonders"?
  37. Using Vue to realize a simple shopping cart function
  38. 【css】伪类和伪类元素的区别
  39. 【css效果】实现简单的下拉菜单
  40. 【vue】父子组件传值
  41. The difference between pseudo class and pseudo class elements
  42. [CSS effect] simple drop-down menu
  43. [Vue] value transfer by parent-child component
  44. 【css】设置table表格边框样式
  45. 【css】修改input,textarea中的placeholder样式
  46. vue-router的两种模式(hash和history)及区别
  47. CSS3的滤镜filter属性
  48. [CSS] set table border style
  49. [CSS] modify the placeholder style in input and textarea
  50. Two modes of Vue router (hash and History) and their differences
  51. Filter property of CSS3
  52. 全局安装gulp 报错问题解决
  53. Solution of error report in global installation of gulp
  54. 18个好用的自定义react hook
  55. 你应该知道的常用服务器HTTP状态码?
  56. 18 user defined react hooks
  57. What HTTP status codes should you know about common servers?
  58. 手把手教你打造属于自己团队的前端小报系统
  59. Hand in hand to teach you to build your own front-end tabloid system
  60. In 2021, enterprise SEO actual operation, how to less update, batch ranking regional words?