標籤:瀏覽器 sub 進階 UI name 組織 min 伺服器 技術
先分析下這個技術可實現的方式,以及優缺點吧!
前端實現
缺點是:相容性查,需要進階瀏覽器支援,因為需要支援 canvas 繪圖,還有就是會操作 html5 canvas api。(如果不會使用canvas的話,要麼去學,要麼這條方案當我沒說)
優點是:使用者體驗性很贊,很流暢。
大體實現方法:使用canvas。最終可將繪製的映像產生成圖片。使用者可以另存新檔儲存,也可以將產生的二進位圖片,上傳伺服器,產生串連。
後端實現
缺點是:體驗性會差很多,因為需要和伺服器互動,體驗流暢度會差一點。 優點:用戶端相容性好,基本支援所有瀏覽器。
大體方法:前端負責組織資料。後端負責根據資料產生圖片。後端解決的話就比較簡單了,有了資料買就是把位置對好,產生圖片,就行了。 nodejs, python, java, php 都用很多繪圖庫。
具體實現流程這裡就不廢話了。點到為止。下面便是具體的代碼實現
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><script type="text/javascript" src="http://html2canvas.hertzen.com/build/html2canvas.js"></script><script type="text/javascript" src="http://www.boolaw.com/tpl/default/js/jquery-1.8.3.min.js"></script><title>html2canvas網頁</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!-- html2canvas()中,第一個參數是要的Dom對象,第二個參數時渲染完成後回調的canvas對象。 --><script type="text/javascript">$(function(){print();});function print(){html2canvas( $("#canv") ,{onrendered: function(canvas){$(‘#down_button‘).attr( ‘href‘ , canvas.toDataURL() ) ;$(‘#down_button‘).attr( ‘download‘ , ‘myjobdeer.png‘ ) ;var html_canvas = canvas.toDataURL();$.post(‘‘, {order_id:1,type_id:2,html_canvas:html_canvas}, function(json){}, ‘json‘);}});}</script><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><div id="canv">此網頁示範了html2canvas後將後的網頁追加到了原網頁之上<br><br>這裡可以看作是邊界線<hr/></div><a type="button" id="down_button">下載</a><?phpif (isset($_POST[‘html_canvas‘])) {$order_id = $_POST[‘order_id‘];$type_id = $_POST[‘type_id‘];$html_canvas = $_POST[‘html_canvas‘];$image = base64_decode(substr($html_canvas, 22));header(‘Content-Type: image/png‘);$filename = $order_id . ‘-‘ . $type_id . ".png";$fp = fopen($filename, ‘w‘);fwrite($fp, $image);fclose($fp);}?></body></html>
利用html2canvas將當前網頁儲存為圖片.