2015-01-15 97 views
0

在我的應用程序中,我將兩張圖像合併爲html.now中的單個圖像,並將該圖像保存到我的磁盤中。但代碼工作正常,只有2次,之後,它沒有保存image.i試圖找到錯誤,但沒有得到任何東西。請幫我把它整理出來。如何將畫布圖像保存在磁盤中?

<p>Image to use:</p> 

<img id="scream" src="img_the_scream.jpg" alt="The Scream" 
width="220" height="277"> 
<img id="scream2" src="img_the_scream.jpg" alt="The Scream" 
width="220" height="277"> 


<p>Canvas to fill:</p> 

<canvas id="canvas" width="800" height="300" 
style="border:1px solid #d3d3d3;"> 
Your browser does not support the HTML5 canvas tag.</canvas> 

<p><button onclick="myCanvas()">Try it</button></p> 

<script> 
function myCanvas() { 
    var c = document.getElementById("canvas"); 
    var ctx = c.getContext("2d"); 
    var img = document.getElementById("scream"); 
    ctx.drawImage(img,10,10); 

var img = document.getElementById("scream2"); 
    ctx.drawImage(img,210,10); 
} 
</script> 
     <h1>Save Image</h1> 


     <button type="button" onclick="saveImage()">save image</button> 
     <script type="text/javascript"> 
      function saveImage() { 
       var ua = window.navigator.userAgent; 

       if (ua.indexOf("Chrome") > 0) { 
        // save image without file type 
        var canvas = document.getElementById("canvas"); 
        document.location.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); 

        // save image as png 
        var link = document.createElement('a'); 
        link.download = "test1.png"; 
        link.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");; 
        link.click(); 
       } 
       else { 
        alert("Please use Chrome"); 
       } 
      } 
     </script> 
+0

[使用HTML5/Javascript生成並保存文件]的可能重複(http://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file) – Pinal

+0

@ Pinal,工作正常,如果我downaload正常圖像..但如果我這樣做後結合它不適合我 –

回答

1

您可以右鍵單擊畫布並將圖像另存爲。

+0

Upvote。是的,如果您的目標客戶端都使用最近的Chrome瀏覽器,則確實可以右鍵單擊 - 保存畫布(對於最新版本的Firefox也是如此)。鼓勵瀏覽器製造商增加此功能。 – markE