当前位置: 首页 > ai >正文

自制喜悦字贴

一、想法

据说,把“喜悦”两个字挂在家里显眼的地方,时常看到,就能心情愉悦。刚好最近在学习前端flex布局,用代码实现,导出图片,打印出来,帖在家里,非常nice。现在分享给大家。

二、效果

导出竖版的图片:
在这里插入图片描述
导出横版的图片:
在这里插入图片描述

三、代码

竖版的代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>html画文章封面图</title><script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script><style lang="scss" scoped>@font-face {font-family: 'ResourceHanRoundedCN-Bold';src: url(./fonts/简-圆体/资源圆体/ResourceHanRoundedCN-Bold.ttf);font-weight: normal;font-style: normal;}#box {width: 420px;height: 594px;display: flex;align-items: center;flex-direction: column;font-weight: bold;font-family: ResourceHanRoundedCN-Bold;color: gold;font-size: 180px;background-color: red;}.xx{height: 297px;line-height: normal;display: flex;align-items: flex-end;justify-content: center;}.dd{height: 297px;line-height: normal;display: flex;align-items: flex-start;justify-content: center;margin-top: -50px;}.btn{margin-top: 10px;margin-left: 10px;}</style>
</head>
<body>
<div id="box"><div class="xx"></div><div class="dd"></div>
</div>
<button class="btn" onclick="capture()">生成图片</button>
<script>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'); 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}`;}
</script>
</body>
</html>

横版的代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>html画文章封面图</title><script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script><style lang="scss" scoped>@font-face {font-family: 'ResourceHanRoundedCN-Bold';src: url(./fonts/简-圆体/资源圆体/ResourceHanRoundedCN-Bold.ttf);font-weight: normal;font-style: normal;}#box {height: 420px;width: 594px;line-height: 400px;border: 1px solid gray;display: flex;align-items: center;flex-direction: column;font-weight: bold;font-family: ResourceHanRoundedCN-Bold;color: gold;font-size: 200px;background-color: red;}.btn{margin-top: 10px;margin-left: 10px;}</style>
</head>
<body>
<div id="box" >喜悦
</div>
<button class="btn" onclick="capture()">生成图片</button>
<script>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'); 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}`;}
</script>
</body>
</html>

四、友情链接

最近在学习前端画图,可以制作生日祝福贺卡、节日祝福卡片、对联、字帖、字画等。如果您需要,或者感兴趣,欢迎一起交流!

http://www.xdnf.cn/news/12487.html

相关文章:

  • QILSTE 精巧电子元件H4-108FO/5M解析
  • 观测云OaC能力升级,通过Terraform实现配置闭环
  • MySQL基础(一)介绍、下载及安装
  • w384药品管理系统的设计与实现
  • MySQL基础(三)DQL(Data Query Language,数据查询语言)
  • 【第一章:人工智能基础】01.Python基础及常用工具包-(1)Python语法与基础数据类型
  • 开源音乐播放器 MusicFree 项目介绍:自由、纯粹、高度自定义的听歌体验
  • 谷歌云代理商 | 游戏行业专属方案:谷歌云实时多人游戏服务器架构
  • STM32实战: CAN总线数据记录仪设计方案
  • 利用ProtoBuf 实现网络版通讯录
  • Vue基础(14)_列表过滤、列表排序
  • arcpy与扩展模块
  • 灵感枯竭?ai写小说生成器来帮忙
  • 深入理解数字音频:采样率、位深与量化
  • nuScenes 数据集及同类型自动驾驶数据集介绍
  • vue3 按钮级别权限控制
  • 车型库查询接口如何用Java进行调用?
  • 【机械视觉】Halcon—【十、实例—木头检测_充电宝检测_豆子检测】
  • python打卡44天
  • Linux 下的COW机制(copy-on-write)
  • python八股文算法:三数之和
  • 前端~三维地图(cesium)地图遮罩蒙层
  • 货运车辆在高速公路上发生故障,应如何设置警示标志?
  • 山洪径流过程及洪水淹没数值模拟
  • JDK21 虚拟线程原理剖析与性能深度解析
  • 力扣面试150题--克隆图
  • 2025年服装收银系统推荐:助力服装商家高效经营
  • SDC命令详解:使用set_min_capacitance命令进行约束
  • hbuildx运行uzapp项目初始化配置
  • gid1 gid2 profileOwner