依赖:html2canvas

function dataURLtoFile(dataurl, filename) {
 
        var arr = dataurl.split(','),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), 
            n = bstr.length, 
            u8arr = new Uint8Array(n);
            
        while(n--){
            u8arr[n] = bstr.charCodeAt(n);
        }
        
        return new File([u8arr], filename, {type:mime});
    }
    
    //Usage example:
    

function takeScreenshot() {
    html2canvas(document.body,{
	 allowTaint: true,
     foreignObjectRendering: true
	}).then(function(canvas) {
		//document.body.appendChild(canvas);
		var dataurl=canvas.toDataURL();
		var file = dataURLtoFile(dataurl,'cap.png');
		//var url = window.URL.createObjectURL(file);
		var a = document.createElement('a');
		var url = window.URL.createObjectURL(file);
		var filename = 'cap.png';
		a.href = url;
		a.download = filename;
		a.click();
		window.URL.revokeObjectURL(url);
		console.log(url);
		
	});
}

  

相关文章:

  • 2022-12-23
  • 2021-04-08
  • 2021-11-11
  • 2021-04-23
  • 2021-07-07
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2022-01-07
  • 2021-12-24
  • 2022-02-09
  • 2022-01-31
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
相关资源
相似解决方案