HTML5 canvas 合成图片并保存
function draw() {
var canvas = document.getElementById("thecanvas");
var ctx = canvas.getContext("2d");
var day = $('.bc_date').val();
var myDate = new Date();
if(day!=''){
myDate = new Date(day);
}
var day = myDate.getFullYear() + "年" + (myDate.getMonth() + 1) + "月" + myDate.getDate() + "日";
var number = "J430404-201";
var photo = new Image();
photo.src = "../assets/image/back.jpg";
var name = $('.name').val();
photo.onload = function() {
ctx.drawImage(photo, 0, 0);
// ctx.save();
// ctx.rotate(-Math.PI/100);
ctx.fillStyle = "rgba( 27, 27, 27, 0.5)";
ctx.font = "23px simsun";
ctx.fillText(name, 213, 213);
ctx.fillText(day, 216, 307);
// ctx.font = "24px simsun";
// ctx.fillText(number,236,401);
fit(ctx);
saveImageInfo();
// ctx.restore();
};
}
function saveImageInfo() {
var mycanvas = document.getElementById("thecanvas");
var image = mycanvas.toDataURL("image/jpeg");
var img = $('.image img');
$(".loading").css("display", "none");
img.attr("src", image);
img.parent().css("display", "block");
setTimeout(function() {
window.location.href = image;
}, 5000);
}
$('.commit').on('click', function() {
draw();
});
$('.btn_close').on('click', function() {
$(".image").css("display", "none");
});
function fit(canvas) {
if (!browser.versions.android) {
canvas.font = "24px simsun";
canvas.fillText("J430404-2016", 236, 401);
} else {
canvas.fillText("J430404-201", 236, 401);
}
}
demo:
结婚证生成神器