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

HTML5和JS实现明媚月色效果

HTML5和JS实现明媚月色效果

先给出效果图:

源码如下:

<!DOCTYPE html>
<html>
<head><title>明媚月光效果</title><style>body {margin: 0;overflow: hidden;background-color: #000; /* 添加一个深色背景以便看到月光效果 */}#moonlightCanvas {position: absolute;top: 0;left: 0;}</style>
</head>
<body><canvas id="moonlightCanvas"></canvas><script>window.addEventListener("load", function() {function drawMoonlight() {var canvas = document.getElementById("moonlightCanvas");var ctx = canvas.getContext("2d");canvas.width = window.innerWidth;canvas.height = window.innerHeight;var centerX = canvas.width / 2;var centerY = canvas.height / 2;var moonRadius = 50; // 月亮的半径,您可以根据需要调整大小// 绘制星星function drawStars() {var starCount = 50; // 星星的数量for (var i = 0; i < starCount; i++) {var x = Math.random() * canvas.width;var y = Math.random() * canvas.height;var starRadius = Math.random() * 2.5; // 星星的大小ctx.beginPath();ctx.arc(x, y, starRadius, 0, Math.PI * 2, false);ctx.fillStyle = "yellow";ctx.fill();}for (var i = 0; i < starCount; i++) {var x = Math.random() * canvas.width;var y = Math.random() * canvas.height;var starRadius = Math.random() * 2.5; // 星星的大小ctx.beginPath();ctx.arc(x, y, starRadius, 0, Math.PI * 2, false);ctx.fillStyle = "white";ctx.fill();}}drawStars(); // 绘制星星// 绘制月亮ctx.beginPath();ctx.arc(centerX, centerY, moonRadius, 0, 2 * Math.PI, false);ctx.fillStyle = "rgba(255, 255, 255, 1)"; // 不透明的白色ctx.fill();// 绘制明媚月光效果var gradient = ctx.createRadialGradient(centerX, centerY, moonRadius, centerX, centerY, canvas.width);gradient.addColorStop(0, "rgba(173, 216, 230, 0.3)"); // 浅蓝色半透明gradient.addColorStop(1, "rgba(255, 255, 255, 0)"); // 完全透明ctx.fillStyle = gradient;ctx.fillRect(0, 0, canvas.width, canvas.height);}drawMoonlight();});</script>
</body>
</html>

绘制一个代表月亮的圆形,月亮的半径设置为了一个固定值(moonRadius = 50;),可以根据自己的需要调整这个值。然后在它的周围添加了一个更大的渐变来模拟散发的月光效果。你可以尝试修改渐变的颜色或透明度。drawStars函数来绘制星星,这个函数通过在画布上随机位置画上小点来模拟星星。starCount变量控制星星的数量,而starRadius变量控制每个星星的大小。

http://www.lryc.cn/news/275995.html

相关文章:

  • Django5+DRF序列化
  • 什么是编译程序和解释程序
  • 文档审阅批注的合并和对比
  • 广义零样本学习综述的笔记
  • java每日一题——输出9x9乘法表(答案及编程思路)
  • Android 车联网——基础简介(一)
  • 自动驾驶货车编队行驶系统功能规范
  • javafx
  • 玩转贝启科技BQ3588C开源鸿蒙系统开发板 —— 编译构建及此过程中的踩坑填坑(3)
  • SQL ORDER BY 关键字
  • 多线程-生产者消费者模型
  • 解压命令之一 gzip
  • 力扣:438. 找到字符串中所有字母异位词 题解
  • QT 高DPI解决方案
  • SLB、DMZ、Nginx、Ingress、Gateway、Kibana和Grafana
  • 【已解决】Invalid bound statement (not found)
  • 汽车信息安全--芯片厂、OEM安全启动汇总(1)
  • 气膜建筑:舒适、智能、可持续
  • 【C语言】一种状态超时阻塞循环查询的办法
  • 【leetcode】力扣热门之回文链表【简单难度】
  • 【MySQL】ALL函数的巧用 以及 排序(order by)巧用 sum(条件表达式) 语法
  • Debezium发布历史49
  • 数据结构——队列(Queue)
  • uniapp微信小程序投票系统实战 (SpringBoot2+vue3.2+element plus ) -后端架构搭建
  • 两种方式实现mysql截取年月日
  • WPF 使用矢量字体图标
  • 编程语言的语法糖,你了解多少?
  • MySQL中FLUSH TABLES命令语法
  • 如何在小米4A刷OpenWRT系统并通过cpolar实现公网访问本地路由器
  • Spring学习之——事务控制