之前我曾寫過如何將canvas圖形轉換成圖片和下載canvas影像的方法,這些都是在為這個插件做技術準備,
技術路線很清晰,將網頁的某個區域的內容生成影像,保持到canvas里,然后將canvas內容轉換成圖片,保存到本地,最后上傳到微博,
我在網上搜尋到html2canvas這個能將指定網頁元素內容生成canvas影像的javascript工具,這個js工具的用法很簡單,你只需要將它的js檔案引入到頁面里,然后呼叫html2canvas()函式:
html2canvas(document.body, { onrendered: function(canvas) { /* canvas is the actual canvas element, to append it to the page call for example document.body.appendChild( canvas ); */ } });
這個html2canvas()函式有個引數,上面的例子里傳入的引數是document.body,這會截取整個頁面的影像,如果你想只截取一個區域,比如對某個p或某個table截圖,你就將這個p或某個table當做引數傳進去,
我最終并沒有選用html2canvas這個js工具,因為在我的實驗程序中發現它有幾個問題,
首先,跨域問題,我舉個例子說明這個問題,比如我的網頁網址是http://www.webhek.com/about/,而我在這個頁面上有個張圖片,這個圖片并不是來自www.webhek.com域,而是來自CDN圖片服務器www.webhek-cdn.com/images/about.jpg,那么,這張圖片就和這個網頁不是同域,那么html2canvas就無法對這種圖片進行截圖,如果你的網站的所有圖片都放在單獨的圖片服務器上,那么用html2canvas對整個網頁進行截圖是就會發現所有圖片的地方都是空白,
這個問題也有補救的方法,就是用代理:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>html2canvas php proxy</title>
<script src="https://www.cnblogs.com/coderhf/p/html2canvas.js"></script>
<script>
//<![CDATA[
(function() {
window.onload = function(){
html2canvas(document.body, {
"logging": true, //Enable log (use Web Console for get Errors and Warnings)
"proxy":"html2canvasproxy.php",
"onrendered": function(canvas) {
var img = new Image();
img.onload = function() {
img.onload = null;
document.body.appendChild(img);
};
img.onerror = function() {
img.onerror = null;
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");
}
});
};
})();
//]]>
</script>
</head>
<body>
<p>
<img alt="google maps static" src="http://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=12&size=800x600&maptype=roadmap&sensor=false">
</p>
</body>
</html>
這個方法只能用在你自己的服務器里,如果是對別人的網頁截圖,還是不行,
試驗的程序中還發現用html2canvas截屏出來的影像有時會出現文字重疊的現象,我估計是因為html2canvas在決議頁面內容、處理css時不是很完美的原因,
最后,我在火狐瀏覽器的官方網站上找到了drawWindow()這個方法,這個方法和上面提到html2canvas不同之處在于,它不分析頁面元素,它只針對區域,也就是說,它接受的引數是四個數字標志的區域,不論這個區域中什么地方,有沒有頁面內容,
void drawWindow( in nsIDOMWindow window, in float x, in float y, in float w, in float h, in DOMString bgColor, in unsigned long flags [optional] );
這個原生的JavaScript方法看起來非常的完美,正是我需要的,但這個方法不能使用在普通網頁中,因為火狐官方發現這個方法會引起有安全漏洞,在這個bug修復之前,只有具有“Chrome privileges”的代碼才能使用這個drawWindow()函式,
雖然有很大的限制,但周折一下還是可以用的,在我開發的火狐addon插件中,main.js就是具有“Chrome privileges”的代碼,我在網上發現了一段火狐插件SDK里自帶代碼樣例:
var window = require('window/utils').getMostRecentBrowserWindow(); var tab = require('tabs/utils').getActiveTab(window); var thumbnail = window.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); thumbnail.mozOpaque = true; window = tab.linkedBrowser.contentWindow; thumbnail.width = Math.ceil(window.screen.availWidth / 5.75); var aspectRatio = 0.5625; // 16:9 thumbnail.height = Math.round(thumbnail.width * aspectRatio); var ctx = thumbnail.getContext("2d"); var snippetWidth = window.innerWidth * .6; var scale = thumbnail.width / snippetWidth; ctx.scale(scale, scale); ctx.drawWindow(window, window.scrollX, window.scrollY, snippetWidth, snippetWidth * aspectRatio, "rgb(255,255,255)"); // thumbnail now represents a thumbnail of the tab
這段代碼寫的非常清楚,只需要依據它做稍微的修改就能適應自己的需求,
更多學習內容觀看我的知乎:打造全網web高級前端工程師資料庫(總目錄)看完學的更加快,知識更牢固,你值得擁有(持續更新)~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/62355.html
標籤:JavaScript
下一篇:vue 鍵盤操作事件
