2017-08-05 233 views
2

我試圖讓圖像的截圖,以紫紅色線(所以下面的標題爲「今天的衣服」的權利(截圖)。html2canvas截圖一直出現如下圖所示的空白

enter image description here 我使用html2canvas要做到這一點,但每次我點擊「快照」按鈕,我不斷收到一個空白屏幕截圖

這是我的HTML:

<div id="sidebar"><br/><center><h1 class = "outfitheading">Today's Outfit</h1></center> 
    <div id = "outfit"> 
    <center><div class = "clothediv" id = "hatdiv" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"> 

    </div></center> 
    <center><div class = "clothediv" id= "semitopdiv" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"> 

     </div></center> 


    <center><div class = "clothediv" id = "topdiv" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"> 

    </div></center> 


    <center><div class = "clothediv" id = "legweardiv" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"> 

    </div></center> 


    <center><div class = "clothediv" id = "shoediv" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"> 
    </div></center> 


    </div> 

    <button onclick="takeScreenShot()">Snapshot</button> 
    <center><div id= "likebutton" style = "display:none; visibility: hidden"> 
    <a href = "#" ><i id = "outlinehearticon" class="fa fa-heart-o fa-2x" aria-hidden="true" onclick = {fillHeart()}></i></a> 
    <a href = "#" ><i id = "filledhearticon" class="fa fa-heart fa-2x" aria-hidden="true" style = "display:none; visibility: visible" onclick = {unfillHeart()}></i></a> 

    <script> 
     $(document).ready(function(){ 
      $('[data-toggle="popover"]').popover(); 
     }); 
    </script> 

    </div></center> 
     <br/> 
     <br/> 
<div class = "buttons"><center> 
     <button class = "btn btn-lg btn-secondary generatebutton" style = "display: inline-block" type = "button" id= "generateoutfitbutton" onclick = {generateOutfit()}>Generate Outfit</button> 
     <!--<p><center>or</center></p>--> 
     &nbsp 
     &nbsp 

     <button class = "btn btn-lg btn-secondary collagebutton" style = "display: inline-block" type = "button" id= "collagebutton" onclick = {clearCanvas()}>Make your Own</button></center> 
</div> 

</div> 

這裏是我的javascript:

var takeScreenShot = function() { 
    html2canvas(document.getElementById("outfit"), { 
     onrendered: function (canvas) { 
      var tempcanvas=document.createElement('canvas'); 
      tempcanvas.width=465.5; 
      tempcanvas.height=524; 
      var context=tempcanvas.getContext('2d'); 
      context.drawImage(canvas,465.5,40,465.5,524,0,0,465.5,524); 
      var link=document.createElement("a"); 
      link.href=tempcanvas.toDataURL('image/jpg'); //function blocks CORS 
      link.download = 'screenshot.jpg'; 
      link.click(); 
     } 
    }); 
} 

這裏是我的代碼縮短向下的jsfiddle: https://jsfiddle.net/e51yepcz/

有誰知道爲什麼快照不工作或者我該如何解決?謝謝。

+0

465.5 ..您是否嘗試過使用整數代替?我不認爲你會有半像素的運氣。 – 2pha

+0

您是否嘗試過輸出返回到渲染回調而不是臨時畫布的畫布?只是爲了確保它是預期的。 – 2pha

+0

我試圖用465s替換所有的465.5s,但我仍然得到空白的截圖。 –

回答

0

我編輯你的小提琴,得到了以下錯誤:

Uncaught ReferenceError: takeScreenShot is not defined

所以我改變了一些東西,似乎工作。

我添加了一個ID,以您的按鈕:

<center><button id="snap">Snapshot</button></center> 

和JavaScript事件(jQuery的):

$('#snap').click(function() { 
html2canvas(document.getElementById("outfit"), { 
onrendered: function(canvas) { 
    var tempcanvas = document.createElement('canvas'); 
    tempcanvas.width=465; 
    tempcanvas.height=524; 
    var context=tempcanvas.getContext('2d'); 
    context.drawImage(canvas,465,40,465,524,0,0,465,524); 
    var link=document.createElement("a"); 
    link.href=canvas.toDataURL('image/jpg'); 
    link.download = 'screenshot.jpg'; 
    link.click(); 
    } 
    }); 
}); 

這裏是JsFiddle

+0

這個作品,謝謝!但如果我想通過單擊不同的按鈕將圖像附加到div上,然後拍攝div「裝備」的快照,則「裝備」內的div即使有圖像也會變空。 –

+0

是同一臺服務器中的映像? https://github.com/niklasvh/html2canvas/issues/722 – Kurohige

+0

他們不是,這就是爲什麼我傳遞選項allowTaint並在jquery中將其設置爲true,但是然後快照甚至不出現,但我也沒有得到任何錯誤。這裏有一個更新的「allowTaint」jsfiddle - 也許我的語法錯了? https://jsfiddle.net/e51yepcz/3/ –

相關問題