前端代码生成博客封面图片
背景
最近在学习前端css样式,flex布局。可以实现各种图形。之前每次发博客都为找不到合适的图片而苦恼。刚好前端画好页面后,引入html2canvas的js库,可以直接下载图片,很好地满足了我的需求。现在分享一下学习成果,希望对你也有帮助。
案例一
1、效果
2、代码
html画文章封面图 #box { display: flex; justify-content: space-between; flex-wrap: wrap; width: 700px; height: 410px; border: 1px solid gray; row-gap: 10px; } .div1 { width: 0px; height: 0px; border-top: 100px solid transparent; border-left: 100px solid transparent; border-right: 100px solid transparent; border-bottom: 100px solid green; } .div2 { width: 0px; height: 0px; border: 100px solid; border-color: transparent transparent blue transparent; } .div3 { width: 200px; height: 200px; background: linear-gradient(45deg, deeppink, yellowgreen); } .div4 { width: 200px; height: 200px; background: linear-gradient(45deg, deeppink, yellowgreen 50%, yellowgreen 50%, deeppink 100%); } .div5 { width: 200px; height: 200px; border-radius: 50%; background-color: pink; } .div6 { width: 100px; height: 200px; border-radius: 100px 0px 0px 100px; background: red; } .div7 { margin-top: 50px; width: 100px; height: 50px; border-radius: 0px 0px 50px 50px; border-bottom: 10px solid #111111; box-sizing: border-box; border-top: none; } .btn{ margin-top: 10px; margin-left: 10px; }生成图片 function capture() { html2canvas(document.getElementById('box'), { useCORS: true, allowTaint: false, scale: 1 }).then(canvas => { var link = document.createElement('a'); link.href = canvas.toDataURL("image/png"); const now = new Date(); link.download = formatDateTime(now)+'.png'; link.click(); }); } function formatDateTime(date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // 注意:月份是从0开始的 const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); return `${year}${month}${day}${hours}${minutes}${seconds}`; }
案例二
1、效果
2、代码
html画文章封面图 #box { height: 410px; width: 700px; border: 1px solid gray; display: flex; flex-direction: column; background-color: #3B7ABF; font-weight: bold; } .div1 { height: 350px; line-height: 350px; font-size: 50px; color: white;; justify-content: center; text-align: center; } .div2 { height: 60px; line-height: 60px; font-size: 20px; color:#BF9C0E; justify-content: center; text-align: right; margin-right: 20px; } .btn{ margin-top: 10px; margin-left: 10px; }生成图片 function capture() { html2canvas(document.getElementById('box'), { useCORS: true, allowTaint: false, scale: 1 }).then(canvas => { var link = document.createElement('a'); link.href = canvas.toDataURL("image/png"); const now = new Date(); link.download = formatDateTime(now)+'.png'; link.click(); }); } function formatDateTime(date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // 注意:月份是从0开始的 const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); return `${year}${month}${day}${hours}${minutes}${seconds}`; }这是html画的封面图———创作于2025-05-05
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。