Canvas 倾斜矩形绘制波浪效果

使用的AI模型:

KIMI思考版

对话编程步骤:

画矩形

使用canvas画1个矩形,长1000,宽40,线性渐变颜色从上到下是#c6d2ff和#d3ddfd,

然后倾斜-45度

增加动画

矩形从左上角往右下角移动滑动10秒,前3秒透明度从1到0,高度从5到80

增加重复

每隔0.5秒钟,重复画以上矩形

编程结果:

编程代码:

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas循环动画矩形</title>
    <style>
        body {
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f0f0f0;
        }
        canvas {
            border: 1px solid #ccc;
            background-color: #fff;
        }
    </style>
</head>
<body>
    <canvas id="myCanvas" width="1200" height="800"></canvas>

    <script>
        const canvas = document.getElementById('myCanvas');
        const ctx = canvas.getContext('2d');
        
        const animationDuration = 10000; // 每个动画持续10秒(修改这里)
        const spawnInterval = 1000;      // 每1秒生成一个新矩形
        
        // 存储所有活跃的动画
        let activeAnimations = [];
        let lastSpawnTime = 0;
        
        // 起点和终点
        const startX = 100;
        const startY = 100;
        const endX = canvas.width - 100;
        const endY = canvas.height - 100;
        
        // 尺寸
        const width = 1000;
        const startHeight = 5;
        const endHeight = 40;
        
        function spawnAnimation(currentTime) {
            activeAnimations.push({
                startTime: currentTime
            });
        }
        
        function animate(currentTime) {
            // 清空画布
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // 检查是否需要生成新动画(每1秒)
            if (currentTime - lastSpawnTime >= spawnInterval) {
                spawnAnimation(currentTime);
                lastSpawnTime = currentTime;
            }
            
            // 更新并绘制所有活跃动画
            activeAnimations = activeAnimations.filter(anim => {
                const elapsed = currentTime - anim.startTime;
                
                // 如果动画已完成(超过10秒),移除
                if (elapsed > animationDuration) {
                    return false;
                }
                
                const progress = elapsed / animationDuration;
                
                // 计算当前位置
                const currentX = startX + (endX - startX) * progress;
                const currentY = startY + (endY - startY) * progress;
                
                // 计算当前高度(5到40)
                const currentHeight = startHeight + (endHeight - startHeight) * progress;
                
                // 计算透明度:前3秒从1到0,后7秒保持0
                let opacity;
                if (elapsed <= 3000) {
                    opacity = 1 - (elapsed / 3000);
                } else {
                    opacity = 0;
                }
                
                // 保存状态
                ctx.save();
                
                // 移动并旋转-45度
                ctx.translate(currentX, currentY);
                ctx.rotate(-Math.PI / 4);
                ctx.globalAlpha = opacity;
                
                // 创建渐变(从上到下)
                const gradient = ctx.createLinearGradient(0, -currentHeight/2, 0, currentHeight/2);
                gradient.addColorStop(0, '#c6d2ff');
                gradient.addColorStop(1, '#d3ddfd');
                ctx.fillStyle = gradient;
                
                // 绘制矩形(中心对齐)
                ctx.fillRect(-width/2, -currentHeight/2, width, currentHeight);
                
                ctx.restore();
                
                return true; // 保留在数组中继续动画
            });
            
            requestAnimationFrame(animate);
        }
        
        // 立即开始第一个
        lastSpawnTime = performance.now();
        spawnAnimation(lastSpawnTime);
        requestAnimationFrame(animate);
    </script>
</body>
</html>

更多:

实名认证接口整理_实名人身核验

语音合成接口整理_各平台语音合成接口

C# 时间格式日期格式使用合集

相关推荐
We་ct1 分钟前
LeetCode 35. 搜索插入位置:二分查找的经典应用
前端·算法·leetcode·typescript·个人开发
左耳咚2 分钟前
Claude Code 中的 SubAgent
前端·ai编程·claude
FPGA小迷弟4 分钟前
高频时钟设计:FPGA 多时钟域同步与时序收敛实战方案
前端·学习·fpga开发·verilog·fpga
IT古董4 分钟前
【前端】企业级前端调试体系设计(含日志埋点 + Eruda 动态注入 + Sentry)
前端·sentry
gis开发9 分钟前
cesium 中添加鹰眼效果
前端·javascript
大鹏说大话10 分钟前
Java 锁膨胀机制深度解析:从偏向锁到重量级锁的进化之路
开发语言·c#
IT猿手13 分钟前
基于 ZOH 离散化与增量 PID 的四旋翼无人机轨迹跟踪控制研究,MATLAB代码
开发语言·算法·matlab·无人机·动态路径规划·openclaw
IT猿手14 分钟前
基于控制障碍函数(Control Barrier Function, CBF)的无人机编队三维动态避障路径规划,MATLAB代码
开发语言·matlab·无人机·动态路径规划·无人机编队
huaweichenai17 分钟前
java的时间操作介绍
java·开发语言
前端付豪17 分钟前
Memory V1:让 AI 记住你的关键信息
前端·后端·llm