2016-05-17 39 views
1

我試圖將我的網頁轉換爲圖像。是否有可能使用jquery將我的網頁轉換爲圖像?

現在我有點困惑,因爲它是可能用我的樣式替換我的網頁的html。

var data = '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">' + 
'<foreignObject width="100%" height="100%">' + 
'<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:40px">' + 
'<em>I</em> like ' + 
'<span style="color:white; text-shadow:0 0 2px blue;">' + 
'<select><option>1</option><option>1</option><option>1</option><option>1</option></select>'+ 
'cheese</span>' + 
'</div>' + 
'</foreignObject>' + 
'</svg>'; 

如何分析我的網頁HTML創建這樣的SVG圖像..

+0

你的問題不明確。請重新說明。 –

+0

可能的重複[如何使用JavaScript的屏幕截圖的div?](http://stackoverflow.com/questions/6887183/how-to-take-screen-shot-of-a-div-with-javascript) – choz

回答

1

是。使用.innerHTML讓你的頁面文字的HTML,並把它裏面<foreignObject>像在本教程中指出:

var canvas = document.getElementById('canvas'); 
 
var ctx = canvas.getContext('2d'); 
 

 
var data = '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">' + 
 
    '<foreignObject width="100%" height="100%">' + 
 
    document.querySelector('div').parentNode.innerHTML + 
 
    '</foreignObject>' + 
 
    '</svg>'; 
 

 
var DOMURL = window.URL || window.webkitURL || window; 
 

 
var img = new Image(); 
 
var svg = new Blob([data], { 
 
    type: 'image/svg+xml;charset=utf-8' 
 
}); 
 
var url = DOMURL.createObjectURL(svg); 
 

 
img.onload = function() { 
 
    ctx.drawImage(img, 0, 0); 
 
    DOMURL.revokeObjectURL(url); 
 
} 
 

 
img.src = url;
<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:40px"> 
 
    <em>I</em> like 
 
    <span style="color:white; text-shadow:0 0 2px blue;"> 
 
      cheese</span> 
 
</div> 
 

 
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Drawing_DOM_objects_into_a_canvas --> 
 

 
<canvas id="canvas" style="border:2px solid black;" width="200" height="200"> 
 
</canvas>

相關問題