首页 > 其他分享 >中秋节快乐简单html页面

中秋节快乐简单html页面

时间:2024-09-14 13:01:55浏览次数:1  
标签:canvas const 中秋节 random ctx width html Math 页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>中秋快乐</title>
<style>
    @font-face {
        font-family: 'HeiTi';
        src: url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@900&display=swap');
    }
    body, html {
        margin: 0;
        padding: 0;
        overflow: hidden;
        background: url('zhongqiu1.jpg') no-repeat center center fixed;
        background-size: cover;
    }
    canvas {
        display: block;
    }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const backgroundImage = new Image();
    backgroundImage.src = 'zhongqiu1.jpg';

    let backgroundLoaded = false;

    backgroundImage.onload = () => {
        ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
        backgroundLoaded = true;
    };

    class Spark {
        constructor(x, y, speedX, speedY, color) {
            this.x = x;
            this.y = y;
            this.speedX = speedX;
            this.speedY = speedY;
            this.color = color;
            this.alpha = 1;
            this.fade = 0.02;
        }

        update() {
            this.x += this.speedX;
            this.y += this.speedY;
            this.speedX *= 0.98;
            this.speedY *= 0.98;
            this.speedY += 0.1; // gravity
            this.alpha -= this.fade;
        }

        draw() {
            ctx.save();
            ctx.globalAlpha = this.alpha;
            ctx.beginPath();
            ctx.arc(this.x, this.y, 2, 0, Math.PI * 2);
            ctx.fillStyle = this.color;
            ctx.fill();
            ctx.restore();
        }
    }

    class Firework {
        constructor(x, y, targetX, targetY) {
            this.x = x;
            this.y = y;
            this.targetX = targetX;
            this.targetY = targetY;
            this.sparks = [];
            this.exploded = false;
            this.speed = 2;
            this.color = this.randomColor();
        }

        randomColor() {
            const r = Math.floor(Math.random() * 256);
            const g = Math.floor(Math.random() * 256);
            const b = Math.floor(Math.random() * 256);
            return `rgb(${r}, ${g}, ${b})`;
        }

        update() {
            if (!this.exploded) {
                const dx = this.targetX - this.x;
                const dy = this.targetY - this.y;
                const distance = Math.sqrt(dx * dx + dy * dy);

                if (distance < 5) {
                    this.exploded = true;
                    for (let i = 0; i < 100; i++) {
                        const angle = Math.random() * 2 * Math.PI;
                        const speed = Math.random() * 5;
                        const sparkX = this.x;
                        const sparkY = this.y;
                        const sparkSpeedX = Math.cos(angle) * speed;
                        const sparkSpeedY = Math.sin(angle) * speed;
                        this.sparks.push(new Spark(sparkX, sparkY, sparkSpeedX, sparkSpeedY, this.randomColor()));
                    }
                } else {
                    this.x += dx / distance * this.speed;
                    this.y += dy / distance * this.speed;
                }
            } else {
                this.sparks.forEach(spark => spark.update());
                this.sparks = this.sparks.filter(spark => spark.alpha > 0);
            }
        }

        draw() {
            if (!this.exploded) {
                ctx.beginPath();
                ctx.arc(this.x, this.y, 3, 0, Math.PI * 2);
                ctx.fillStyle = this.color;
                ctx.fill();
            } else {
                this.sparks.forEach(spark => spark.draw());
            }
        }
    }

    const fireworks = [];
    setInterval(() => {
        const x = Math.random() * canvas.width;
        const y = canvas.height;
        const targetX = Math.random() * canvas.width;
        const targetY = Math.random() * canvas.height / 2;
        fireworks.push(new Firework(x, y, targetX, targetY));
    }, 500);

    function drawMoon() {
        const moonX = canvas.width - 150; // 调整X坐标
        const moonY = 150; // 调整Y坐标
        const moonRadius = 50;
        ctx.beginPath();
        ctx.arc(moonX, moonY, moonRadius, 0, Math.PI * 2);
        ctx.fillStyle = 'yellow';
        ctx.fill();
    }

    let textAngle = 0;
    <!-- const colors = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple']; -->
    const colors = ['red', 'orange', 'yellow'];
    let colorIndex = 0;

    function updateTextColor() {
        colorIndex = (colorIndex + 1) % colors.length;
    }

    setInterval(updateTextColor, 1000); // 每1秒更新一次颜色

    function drawText() {
        const text = '中秋节快乐';
        ctx.font = '96px "仿宋"'; // 增大字体大小,并加粗
        ctx.textAlign = 'center';
        const textX = canvas.width / 2;
        const textY = canvas.height / 2;

        // 计算文本宽度和高度
        const metrics = ctx.measureText(text);
        const textWidth = metrics.width;
        const textHeight = 96; // 字体大小

        // 绘制半透明的白色矩形背景
        ctx.save();
        ctx.globalAlpha = 0.09; // 设置透明度
        ctx.fillStyle = 'white';
        ctx.fillRect(textX - textWidth / 2 - 20, textY - textHeight / 2 - 60, textWidth + 60, textHeight + 60);
        ctx.restore();

        // 绘制晃动的文字
        ctx.save();
        ctx.translate(textX, textY);
        ctx.rotate(Math.sin(textAngle) * 0.1); // 以底部为圆心左右晃动
        ctx.translate(-textX, -textY);
        ctx.fillStyle = colors[colorIndex];
        ctx.fillText(text, textX, textY);
        ctx.restore();

        textAngle += 0.05; // 调整晃动速度
    }

    function animate() {
        if (backgroundLoaded) {
            // 绘制半透明黑色背景,保留烟花拖尾效果
            ctx.globalAlpha = 0.1;
            ctx.fillStyle = 'black';
            ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            ctx.globalAlpha = 1;
            
            fireworks.forEach(firework => {
                firework.update();
                firework.draw();
            });
            drawMoon();
            drawText();
            
        }

        requestAnimationFrame(animate);
    }

    animate(); // 启动动画循环
</script>
</body>
</html>

然后把zhongqiu1.jpg替换成自己的,和html放在一个目录下,效果如下:

标签:canvas,const,中秋节,random,ctx,width,html,Math,页面
From: https://www.cnblogs.com/brian-sun/p/18413773

相关文章

  • PbootCMS模板自动生成当前页面二维码
    在PBootCMS中,qrcode 标签用于生成对应文本的二维码图片。这对于产品列表页或详情页为每个产品生成二维码非常有用。以下是详细的使用说明和示例代码。1. qrcode 标签的基本用法参数说明string=*:指定生成二维码的文本内容。2.示例代码生成产品详情页的二维码假设你需......
  • PbootCMS网站后台登录页面样式怎么修改
    如果你需要修改PBootCMS后台的样式,通常你需要编辑的是与后台视图相关的HTML、CSS和JavaScript文件。根据提供的信息,后台样式相关的文件通常位于如下路径:路径位置:根目录/apps/admin/view/default/在这个目录下,你可以找到index.html文件,该文件包含了后台界面的基本结构。如果你......
  • PbootCMS访问页面出现PHP Fatal error: Allowed memory size of 13421
    当访问PbootCMS页面时出现 PHPFatalerror:Allowedmemorysizeof13421 的错误,通常是由于PHP的内存限制过低导致的。这个错误表明PHP脚本在运行过程中耗尽了分配给它的内存。解决方案增加PHP内存限制检查PHP配置文件(php.ini)在脚本中动态增加内存限制详......
  • 对HTML基础详细拓展
    1.什么是HMTL?HTML是用来描述网页的一种语言。HTML指的是超文本标记语言:HyperTextMarkupLanguageHTML不是一种编程语言,而是一种标记语言标记语言是一套标记标签(markuptag)HTML使用标记标签来描述网页HTML文档包含了HTML标签及文本内容HTML文档也叫做web页面......
  • HTML·第二章 网页制作的排版方法
    目录2.1文字与段落排版2.1.1段落标签2.1.2标题标签2.1.3换行标签2.1.4水平线标签2.1.5预格式化标签2.1.6缩排标签2.1.7案例2.2超链接2.2.1超链接简介2.2.2超链接的应用2.3图像2.3.1网页图像的格式及使用要点2.3.2图像标签2.3.3图像超链接2.3.4......
  • 微信小程序开发系列10----页面配置--事件冒泡和阻止
       下图点击里面,外面的事件也触发  场景:广告点击先看广告,之后跳转到功能页面 会冒泡的事件源码获取方式(免费):(1)登录-注册:http://resources.kittytiger.cn/(2)签到获取积分(3)搜索:8-wxmleventMp事件冒泡和阻止......
  • 大模型API的响应内容(markdown语法)在Html中显示实例
    获取大模型API的响应内容的函数返回returnresponse.choices[0].messagefromzhipuaiimportZhipuAIdefget_response_from_model(question):client=ZhipuAI(api_key='your_api_key')response=client.chat.completions.create(model='glm-4-plus&......
  • 微信小程序开发系列9----页面配置--事件-参数传递
       图点击里面,外面的事件也触发  场景:广告点击先看广告,之后跳转到功能页面 会冒泡的事件 源码获取方式(免费):(1)登录-注册:http://resources.kittytiger.cn/(2)签到获取积分(3)搜索:7-wxmleventparameter事件-参数传递......
  • 一个简单的个人导航页html源码
    <!DOCTYPEhtml><htmllang="zh-cn"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>xxx个人导航页</title><......
  • HTML5中的enctype和formenctype有什么区别?
    enctype是HTML5中用于指定表单数据编码方式的属性。它决定了表单数据在提交到服务器时的编码格式。常见的enctype值包括application/x-www-form-urlencoded、multipart/form-data和text/plain等。不同的enctype值适用于不同的表单数据类型和提交方式。在实际开发中,需要根......