在我的应用程序中,我将两个图像合并为html.now画布上的单个图像我想将该画布图像保存到我的磁盘中。但是代码只运行了2次,之后它没有保存图像。我试图找到错误但什么都没有。请帮我解决。
<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 :(得分:0)
您可以右键单击画布并将图像另存为。