Tôi muốn tải xuống phần tử canvas Html5 dưới dạng hình ảnh có phần mở rộng tệp có Javascript.Tải xuống phần tử canvas Html5 dưới dạng hình ảnh có đuôi tệp với Javascript
Thư viện CanvasToImage dường như không thể đạt được điều này.
Đây là mã của tôi cho đến thời điểm này bạn có thể xem tại số JsFiddle này.
<div id="canvas_container">
</div>
<p>
<a href='#' id="create_image">create</a>
<a href="#" id="download_image">download</a>
</p>
$("#create_image").click(function() {
var cnvs = createSmileyOnCanvas();
$('#canvas_container').append(cnvs);
});
$("#download_image").click(function() {
var img = $('#smiley_canvas').toDataURL("image/png");
var uriContent = "data:application/octet-stream," + encodeURIComponent(img);
window.open(uriContent, 'download smiley image');
});
function createSmileyOnCanvas() {
var canvas = document.createElement('canvas');
canvas.id = 'smiley_canvas';
var ctx = canvas.getContext('2d');
// Draw shapes
ctx.beginPath();
ctx.arc(75,75,50,0,Math.PI*2,true); // Outer circle
ctx.moveTo(110,75);
ctx.arc(75,75,35,0,Math.PI,false); // Mouth
ctx.moveTo(65,65);
ctx.arc(60,65,5,0,Math.PI*2,true); // Left eye
ctx.moveTo(95,65);
ctx.arc(90,65,5,0,Math.PI*2,true); // Right eye
ctx.stroke();
return canvas;
}
Tôi đã nghĩ của việc tạo ra các chức năng thông qua ajax nhưng tôi đã hy vọng cho một giải pháp phía khách hàng. –
Có vẻ như chỉ Chrome hỗ trợ thuộc tính tải xuống - mặc dù khá thú vị. http://html5-demos.appspot.com/static/a.download.html –