2014-09-22 46 views
0

小提琴1(使用html2canvas):http://liveweave.com/HM9hSY
小提琴2(使用html2canvasCodemirror):http://liveweave.com/rS8yxI
小提琴3(使用html2canvasCodemirror):http://liveweave.com/TF3Ono
小提琴4(使用html2canvasCodemirror):http://liveweave.com/hqPoQgCodemirror&html2canvas:保存的iFrame爲圖像

今天我想嘗試導出一個iframe作爲圖像。我對此進行了一些研究,並遇到一個名爲html2canvas的插件,它可以將html保存爲圖像。 (文檔可以查看here)。

Capture HTML Canvas as gif/jpg/png/pdf?
Using HTML5/Canvas/JavaScript to take screenshots

first fiddle只是一個實驗,以便保存HTML爲圖像。 (這個小提琴沒有使用Codemirror
second fiddle保存圖像,但不抓取從源給出的iframe預覽。 (這個小提琴沒有使用Codemirror)
third fiddle不保存圖像。 (這小提琴沒有使用Codemirror

我的問題是與2nd3rd小提琴其中[html2canvas] [17]不保存iframe的預覽。

我一直在玩整個昨天和今天,我一直沒有成功。

今天我決定嘗試使用Codemirror的代碼編輯器來渲染我想要的東西。

基本上我把身體的截圖和嵌入圖像。

(順便說一句這是Fiddle 4我指的是)

我再後,使用window.open我重置代碼編輯器返回到它那裏新窗口中打開圖像。

是否可以將iframe的預覽/源保存爲圖像? 我爲了達到這個效果做了什麼錯事?

任何幫助,非常感謝。

$(document).ready(function() { 
 
    $("#saveimg").click(function() { 
 
    html2canvas($("#preview"), { 
 
     onrendered: function(canvas) { 
 
     var myImage = canvas.toDataURL("image/png"); 
 
     $("#imgprev").html("<img src='"+ myImage +"' />"); 
 
     } 
 
    }); 
 
    }); 
 
});
<script src="https://raw.github.com/niklasvh/html2canvas/gh-pages/build/html2canvas.js"></script> 
 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
 
<link type='text/css' rel='stylesheet' href='http://necolas.github.io/normalize.css/3.0.1/normalize.css' /> 
 

 
<div align="center"> 
 
    <p> 
 
    <button id="saveimg"> 
 
     Save as Image 
 
    </button> 
 
    </p> 
 
</div> 
 
<iframe id="preview" src="http://duckduckgo.com/"></iframe> 
 
<div id="imgprev"></div> 
 
<canvas id="mycanvas"></canvas>

回答

0

小提琴:http://liveweave.com/LHfsld
小提琴:http://jsbin.com/xutivucanaye/1/edit

這不是最好的解決辦法,有點怪誕,但我得到它的工作。好樣的。

它不帶有嵌入式圖像,iframe中運行,它會永遠全面的CSS支持(你可以閱讀爲什麼從html2canvas's FAQ

這裏是我的整個片段:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Snap a Picture of Preview</title> 
    <meta charset='utf-8'> 
    <meta name='viewport' content='initial-scale=1.0'> 
    <meta http-equiv='X-UA-Compatible' content='IE=9' /> 
    <link type='text/css' rel='stylesheet' href='http://necolas.github.io/normalize.css/3.0.1/normalize.css'/> 
    <script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script> 
    <script src='https://raw.github.com/niklasvh/html2canvas/gh-pages/build/html2canvas.js'></script> 
    <script src='http://codemirror.net/lib/codemirror.js'></script> 
    <script src='http://codemirror.net/mode/xml/xml.js'></script> 
    <script src='http://codemirror.net/mode/javascript/javascript.js'></script> 
    <script src='http://codemirror.net/mode/css/css.js'></script> 
    <script src='http://codemirror.net/mode/htmlmixed/htmlmixed.js'></script> 
    <link rel='stylesheet' href='http://codemirror.net/lib/codemirror.css'> 
    <link rel='stylesheet' href='http://codemirror.net/doc/docs.css'> 
    <script src='http://codemirror.net/mode/htmlmixed/htmlmixed.js'></script> 
    <script src='http://codemirror.net/addon/edit/closetag.js'></script> 
    <script src='http://codemirror.net/addon/edit/matchbrackets.js'></script> 
    <script src='http://codemirror.net/addon/selection/active-line.js'></script> 
    <style type='text/css'> 
     .CodeMirror { 
     float: left; 
     width: 50%; 
     border: 1px solid black; 
     } 

     iframe { 
     width: 49%; 
     float: left; 
     height: 300px; 
     border: 1px solid black; 
     border-left: 0; 
     } 

     #beforeiframesnap { 
     position: absolute; 
     top: -500000px; 
     left: -500000px; 
     width: 0; 
     height: 0; 
     opacity: 0; 
     overflow: hidden; 
     } 
    </style> 
    </head> 
    <body> 
    <div align='center'> 
     <button class='snapFrame'>Snap</button> 
    </div> 
    <textarea id='code' name='code'><!DOCTYPE html> 
<html> 
    <head> 
    <meta charset=utf-8> 
    <title>HTML5 canvas demo</title> 
    <style>p {font-family: monospace;}</style> 
    </head> 
    <body> 
    <p>Canvas pane goes here:</p> 
    <canvas id=pane width=300 height=200></canvas> 

    <script> 
     var canvas = document.getElementById('pane'); 
     var context = canvas.getContext('2d'); 

     context.fillStyle = 'rgb(250,0,0)'; 
     context.fillRect(10, 10, 55, 50); 

     context.fillStyle = 'rgba(0, 0, 250, 0.5)'; 
     context.fillRect(30, 30, 55, 50); 
    </script> 
    </body> 
</html></textarea> 
    <textarea id='beforeiframesnap'></textarea> 
    <iframe id='preview'></iframe> 
    <div id='imgprev'></div> 

    <script type='text/javascript'> 
     $('#beforeiframesnap').val(""); 
     // Append JS library to HTML <head> 
     function appendJSLib(txt) { 
     var textArea = editor.getValue(); 
     var searchText = textArea.search('<head>'); 
     if(searchText>0) { 
      txt = '<head>'+'\n'+txt; 
      var updatedTextArea = textArea.replace('<head>',txt); 
      editor.setValue(updatedTextArea); 
     } 
     else { 
      reset(); 
      alert('WARNING! The &lt;head&gt tag seems to be missing in your HTML. Although your code may still work, we highly recommened that you have a valid HTML syntax. Please refer to the new document of correct formatted'); 
      txt = txt+textArea; 
      htmlEditor.setLine(0, txt); 
      return false; 
     } 
     } 
     // Append script to HTML <body> 
     function appendScript(txt) { 
     var textArea = editor.getValue(); 
     var searchText = textArea.search('<body>'); 
     if(searchText>0) { 
      txt = '<body>'+'\n'+txt; 
      var updatedTextArea = textArea.replace('<body>',txt); 
      editor.setValue(updatedTextArea); 
     } 
     else { 
      reset(); 
      alert('WARNING! The <body> tag seems to be missing in your HTML. Although your code may still work, we highly recommened that you have a valid HTML syntax. Please refer to the new document of correct formatted'); 
      txt = txt+textArea; 
      htmlEditor.setLine(0, txt); 
      return false; 
     } 
     } 

     $('.snapFrame').on('click', function() { 
     $('#beforeiframesnap').val("").val(editor.getValue()); 
     txt = '<'+'script type=\'text/javascript\' src=\'http://code.jquery.com/jquery-latest.min.js\'>'+'</'+'script'+'>'; 
     appendJSLib(txt); 
     txt = '<'+'script type=\'text/javascript\' src=\'https://raw.github.com/niklasvh/html2canvas/gh-pages/build/html2canvas.js\'>'+'</'+'script'+'>'; 
     appendJSLib(txt); 
     txt = '<'+'script type=\'text/javascript\'>\n$(document).ready(function() {\n html2canvas($(\'body\'), {\n onrendered: function(canvas) {\n  var myImage = canvas.toDataURL(\'image/png\');\n  $(\'body\').html(\'<img src=\'+ myImage +\' />\');  window.open(\'javascript:document.write("<img src=\'+ myImage +\' />")\', \'Opened Page\', \'width=660, height=440\');\n }\n });\n});\n</'+'script'+'>'; 
     appendScript(txt); 

     setTimeout(function() { 
      editor.setValue($('#beforeiframesnap').val()); 
     }, 1200); 
     }); 

     var delay; 

     // Initialize CodeMirror editor 
     var editor = CodeMirror.fromTextArea(document.getElementById('code'), { 
     mode: 'text/html', 
     tabMode: 'indent', 
     styleActiveLine: true, 
     lineNumbers: true, 
     lineWrapping: true, 
     autoCloseTags: true 
     }); 

     // Live preview 
     editor.on('change', function() { 
     clearTimeout(delay); 
     delay = setTimeout(updatePreview, 300); 
     }); 

     function updatePreview() { 
     var previewFrame = document.getElementById('preview'); 
     var preview = previewFrame.contentDocument || previewFrame.contentWindow.document; 
     preview.open(); 
     preview.write(editor.getValue()); 
     preview.close(); 
     } 
     setTimeout(updatePreview, 300); 
    </script> 
    </body> 
</html> 
1

我懷疑你正在運行到跨域限制。如果您能夠在iframe中打開http://mybank.com並獲取結果頁面的屏幕截圖,那麼可能會泄露僅用於用戶(將使用其會話的用戶)的私人信息。因此,瀏覽器不會讓你這樣做。