2017-08-21 198 views
0

我正在使用html2canvas库来截取html视图的截图。html2canvas边框图像问题

但背景图像加载失败。我收到错误消息Error loading background:

这是我的JSFiddle

window.takeScreenShot = function() { 
 
    html2canvas(document.getElementById("target"), { 
 
     onrendered: function (canvas) { 
 
      document.body.appendChild(canvas); 
 
     }, 
 
     useCORS:true, 
 
     logging:true, 
 
     allowTaint:true 
 
    }); 
 
}
#target{ 
 
    width:300px; 
 
    height:160px; 
 
    background:lightblue; 
 
    color:#fff; 
 
    padding:10px; 
 
    background-image: url(https://static.pexels.com/photos/20974/pexels-photo.jpg); 
 
    background-repeat:no-repeat; 
 
    background-position:center center; 
 
    -o-background-size: 100%; 
 
    -moz-background-size: 100%; 
 
    -webkit-background-size: 100%; 
 
    background-size: 100%; 
 
    height: 100%; 
 
} 
 

 
#borderimg1 { 
 
    border: 10px solid transparent; 
 
    padding: 15px; 
 
    -webkit-border-image: url(https://www.w3schools.com/cssref/border.png) 30 round; /* Safari 3.1-5 */ 
 
    -o-border-image: url(https://www.w3schools.com/cssref/border.png) 30 round; /* Opera 11-12.1 */ 
 
    border-image: url(https://www.w3schools.com/cssref/border.png) 30 round; 
 
} 
 

 
#borderimg2 { 
 
    border: 10px solid transparent; 
 
    padding: 15px; 
 
    -webkit-border-image: url(https://www.w3schools.com/cssref/border.png) 30 stretch; /* Safari 3.1-5 */ 
 
    -o-border-image: url(https://www.w3schools.com/cssref/border.png) 30 stretch; /* Opera 11-12.1 */ 
 
    border-image: url(https://www.w3schools.com/cssref/border.png) 30 stretch; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> 
 
    <button onclick="takeScreenShot()">to image</button> 
 
<div id="target"> 
 
    
 
<p>The border-image property specifies an image to be used as the border around an element:</p> 
 
<p id="borderimg1">Here, the middle sections of the image are repeated to create the border.</p> 
 
<p id="borderimg2">Here, the middle sections of the image are stretched to create the border.</p> 
 

 
<p>Here is the original image:</p><img src="https://www.w3schools.com/cssref/border.png"> 
 
<p><strong>Note:</strong> Internet Explorer 10, and earlier versions, do not support the border-image property.</p> 
 

 
</div>

image for error message

回答

0

对于互联网安全的原因,试图从在画布上不同的域使用的图像会导致问题。有两个html2canvas选项,useCORS和proxy,旨在解决这个问题。

您必须在您的项目中创建代理并将其用于html2canvas代理选项。

点击here查看以不同编程语言创建的示例代理。在html2canvasc#例子)代理的

使用

html2canvas(document.body, { 
    "logging": true, //Enable log (use Web Console for get Errors and Warings) 
    "proxy":"html2canvasproxy.ashx", 
    "onrendered": function(canvas) { 
     var img = new Image(); 
     img.onload = function() { 
      document.body.appendChild(img); 
     }; 
     img.error = function() { 
      if(window.console.log) { 
       window.console.log("Not loaded image from canvas.toDataURL"); 
      } else { 
       alert("Not loaded image from canvas.toDataURL"); 
      } 
     }; 
     img.src = canvas.toDataURL("image/png"); 
    } 
}); 
+0

感谢您的重播,但。我认为问题不在代理服务器上。问题与** border-Image **。边框图像选项只适用于我的原因。只显示黑色边框而不是图像。除了背景图像加载良好。请参阅代码片段或[Jsfiddle](http://jsfiddle.net/sodofkcs/1042/)。点击** toImage **按钮查看结果。我希望你能理解并帮助我解决这个问题。谢谢。 –

+0

查看此更新[JSFIDDLE](http://jsfiddle.net/sodofkcs/1047/)。我的问题仅限于** border-image **。 '-webkit-border-image:url(“http://..*.png”)10 fill stretch; border-image-source:url(“http://...*.png”); ' –