2014-09-29 87 views
0

我試圖將畫布導出爲png圖像。我用下面的代碼來做到這一點。將畫布導出爲圖像在IE中不起作用

function onExport() { 
     var canvas = $("#container_canvas")[0]; //id of the canvas 
     var dt = canvas.toDataURL(); 
     this.href = dt; 
    } 

這適用於除IE以外的所有其他瀏覽器。沒有行動發生。我使用IE 10版本。 幫我解決這個問題。謝謝。

+0

這是什麼$( 「#container_canvas」)[0] ??你已經使用了一個jQuery選擇器,它會返回你的ID然後[0]爲什麼..試圖調試,並得到什麼「$(」#container_canvas「)[0]」返回? – 2014-09-29 06:32:36

回答

0

下面是一個更完整的從畫布創建html img元素的示例。

它在IE10中正常工作。

如果用戶需要,他們可以右鍵單擊「保存」作爲新的img。

實施例的代碼和一個演示:

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

 

 
ctx.fillStyle='skyblue'; 
 
ctx.fillRect(50,50,150,55); 
 
ctx.strokeRect(50,50,150,55); 
 
ctx.fillStyle='white'; 
 
ctx.font='14px verdana'; 
 
ctx.fillText('Click the canvas',55,65); 
 
ctx.fillText('to save it as',55,80); 
 
ctx.fillText('a new image',55,95); 
 

 

 
canvas.addEventListener("click",function(){ 
 
    var img=new Image(); 
 
    img.onload=function(){ 
 
    document.body.appendChild(img); 
 
    } 
 
    img.src=canvas.toDataURL(); 
 
});
body{ background-color: ivory; } 
 
canvas{border:1px solid red;} 
 
img{border:1px solid blue;}
<h4>Click the canvas. The new blue element is an img</h4> 
 
<canvas id="canvas" width=300 height=300></canvas>