在点击图片后旋转图片后,尝试为我的图片建立下载链接,然后单击下载以获取更改后的新图片, 谢谢 ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////////////////////////
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Flipping photo in a canvas tag</title>
<style>
#canvas {cursor: pointer;}
canvas {
height: 50vh;
}
</style>
</head>
<body>
<label>Image File:</label><br/>
<input type="file" id="imageLoader" name="imageLoader"/>
<h1>Example of using <code><canvas></code></h1>
<p>This example shows how a photo is loaded in a <code><canvas></code> tag
and then flipped.</p>
<p>Click on the photo to flip (provided, of course, that your browser supports <code><canvas></code>)</p>
<canvas id="canvas" style="border:1px red solid;"></canvas>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
var img = new Image();
function handleImage(e){
var reader = new FileReader();
reader.onload = function(event){
img.onload = function(){
can.width = img.width;
can.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
ctx.save();
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
can.onclick = function() {
console.log('here');
ctx.translate(img.width-1, img.height-1);
ctx.rotate(Math.PI);
ctx.drawImage(img, 0, 0, img.width, img.height);
};
</script>
<p><a href="http://www.phpied.com/photo-canvas-tag-flip/">Blog post is here</a></p>
</body>
</html>
答案 0 :(得分:0)
如果将画布转换为数据URL,则可以将其作为href属性添加到具有download属性的链接元素。
HTML:
<canvas id="canvas"></canvas>
<a download="new-image.png" id="download">Download</a>
Javascript:
var canvas = document.getElementById('canvas');
var button = document.getElementById('download');
button.setAttribute( 'href', canvas.toDataURL('image/png', 1) )
答案 1 :(得分:0)
您可以尝试一下,只是要记住,并非所有浏览器都支持canvas.toBlob()。
从代码中传入画布变量args[0]
。
can