html2canvas生成名片、工作证
自己没事玩的,填入信息生成工作证,有需要的可以自己研究一下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>生成名片</title>
<link rel="stylesheet" href="https://www.jq22.com/demo/html2canvas202308281129/css/index.css">
</head>
<body>
<div class="box">
<div id="container">
<div class="container-border">
<img id="img" style="width: 200px;height: 200px;" src="https://www.jq22.com/demo/html2canvas202308281129/img/t.png" alt="">
<div class="detail font-color">
<div><span class="x-nickname">昵称</span></div>
<div><span class="x-name">真实姓名</span></div>
<div>职位:<span class="x-title">XXXX</span></div>
</div>
<div class="introduce font-color">
</div>
</div>
</div>
<div class="rightConent">
<div style="position: relative">
<input class="file-input" type="file" name="myfile">
<div class="file-font">上传头像</div>
</div>
<div class="inputBox">
<input class="nickname" type="text" value="" placeholder="请输入花名">
<input class="name" type="text" value="" placeholder="请输入姓名">
<input class="title" type="text" value="" placeholder="请输入职位">
<input class="picname" type="text" value="" placeholder="图片名">
</div>
<div id="saveBtn">保存图片</div>
</div>
</div>
</body>
</html>
<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/html2canvas/1.4.1/html2canvas.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// canvas生成图片
$("#saveBtn").on("click", function () {
let getPixelRatio = function (context) { // 获取设备的PixelRatio
let backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 0.5;
return (window.devicePixelRatio || 0.5) / backingStore;
};
let nickname = $('.nickname').val()
let name = $('.name').val()
let title = $('.title').val()
if (nickname) {
$('.x-nickname').text(nickname)
}
if (name) {
$('.x-name').text(name)
}
if (title) {
$('.x-title').text(title)
}
let generateName = $('.picname').val() ? $('.picname').val() + ".jpg" : '默认' + ".jpg"//图片名
let ImageContainer = document.getElementById("container");
let width = ImageContainer.offsetWidth;
let height = ImageContainer.offsetHeight;
let canvas = document.createElement("canvas");
let context = canvas.getContext('2d');
let scale = getPixelRatio(context);
canvas.width = width * scale;
canvas.height = height * scale;
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
context.scale(scale, scale);
let opts = {
scale: scale,
canvas: canvas,
width: width,
height: height,
dpi: window.devicePixelRatio
};
html2canvas(ImageContainer, opts).then(function (canvas) {
context.imageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
let dataUrl = canvas.toDataURL('image/jpeg', 1.0);
dataURIToBlob(generateName, dataUrl, callback);
});
});
})
let dataURIToBlob = function (generateName, dataURI, callback) {
let binStr = atob(dataURI.split(',')),
len = binStr.length,
arr = new Uint8Array(len);
for (let i = 0; i < len; i++) {
arr = binStr.charCodeAt(i);
}
callback(generateName, new Blob());
}
let callback = function (generateName, blob) {
let triggerDownload = $("<a>").attr("href", URL.createObjectURL(blob)).attr("download", generateName).appendTo("body").on("click", function () {
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(blob, generateName);
}
});
triggerDownload.click();
triggerDownload.remove();
};
function changeImgFile(input) {
let file = input.files;
if (window.FileReader) {
let fr = new FileReader();
fr.readAsDataURL(file);
fr.onloadend = function () {
document.getElementById("img").src = fr.result;
}
}
}
</script>
页:
[1]