HTML5+JavaScript绘制彩虹和云朵

HTML5+JavaScript绘制彩虹和云朵

彩虹,简称虹,是气象中的一种光学现象,当太阳光照射到半空中的水滴,光线被折射及反射,在天空上形成拱形的七彩光谱,由外圈至内圈呈红、橙、黄、绿、蓝、靛、紫七种颜色。事实上彩虹有无数种颜色,比如,在红色和橙色之间还有许多种细微差别的颜色,但为了简便起见,所以只用七种颜色作为区别。

使用JavaScript来操作Canvas,绘制彩虹和云朵。运行效果:

源码如下:

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>彩虹</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #F0FFFF; /* 青白色背景 */
        }
        canvas {
            border: 2px solid #000;
            background-color: #87CEEB; /* 天蓝色 */
        }
    </style>
</head>
<body>
    <canvas id="rainbowCanvas" width="400" height="300"></canvas>

    <script>
        const canvas = document.getElementById('rainbowCanvas');
        const ctx = canvas.getContext('2d');

        function drawRainbow() {
            const centerX = canvas.width / 2;
            const centerY = canvas.height;
            const radius = canvas.height * 0.8;

            // 彩虹颜色
            const colors = [
                '#FF0000', // 红
                '#FF7F00', // 橙
                '#FFFF00', // 黄
                '#00FF00', // 绿
                '#0000FF', // 蓝
                '#4B0082', // 靛
                '#9400D3'  // 紫
            ];

            // 绘制彩虹
            for (let i = colors.length - 1; i >= 0; i--) {
                ctx.beginPath();
                ctx.arc(centerX, centerY, radius - i * 20, Math.PI, 0, false);
                ctx.strokeStyle = colors[i];
                ctx.lineWidth = 20;
                ctx.stroke();
            }

            // 绘制云朵
            function drawCloud(x, y, size) {
                ctx.beginPath();
                ctx.arc(x, y, size, 0, Math.PI * 2);
                ctx.arc(x + size, y - size / 2, size * 0.8, 0, Math.PI * 2);
                ctx.arc(x + size * 2, y, size, 0, Math.PI * 2);
                ctx.fillStyle = 'white';
                ctx.fill();
            }

            drawCloud(50, 50, 30);
            drawCloud(canvas.width - 100, 80, 25);
        }

        drawRainbow();
    </script>
</body>
</html>

下免改进云彩代码,让云彩从左向右不停地移动。源码如下:

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>彩虹与移动的云朵</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #F0FFFF; /* 青白色背景 */
        }
        canvas {
            border: 2px solid #000;
            background-color: #87CEEB; /* 天蓝色 */
        }
    </style>
</head>
<body>
    <canvas id="rainbowCanvas" width="400" height="300"></canvas>

    <script>
        // 获取canvas元素和2D绘图上下文
        const canvas = document.getElementById('rainbowCanvas');
        const ctx = canvas.getContext('2d');

        // 定义云朵对象数组,每个云朵包含位置、大小和速度信息
        const clouds = [
            { x: 50, y: 50, size: 30, speed: 0.5 },
            { x: canvas.width - 100, y: 80, size: 25, speed: 0.3 }
        ];

        // 绘制彩虹的函数
        function drawRainbow() {
            const centerX = canvas.width / 2;
            const centerY = canvas.height;
            const radius = canvas.height * 0.8;

            // 定义彩虹的颜色数组
            const colors = [
                '#FF0000', // 红
                '#FF7F00', // 橙
                '#FFFF00', // 黄
                '#00FF00', // 绿
                '#0000FF', // 蓝
                '#4B0082', // 靛
                '#9400D3'  // 紫
            ];

            // 从外到内绘制彩虹的每一道颜色
            for (let i = colors.length - 1; i >= 0; i--) {
                ctx.beginPath();
                ctx.arc(centerX, centerY, radius - i * 20, Math.PI, 0, false);
                ctx.strokeStyle = colors[i];
                ctx.lineWidth = 20;
                ctx.stroke();
            }
        }

        // 绘制单个云朵的函数
        function drawCloud(x, y, size) {
            ctx.beginPath();
            // 绘制三个部分组成的云朵形状
            ctx.arc(x, y, size, 0, Math.PI * 2);
            ctx.arc(x + size, y - size / 2, size * 0.8, 0, Math.PI * 2);
            ctx.arc(x + size * 2, y, size, 0, Math.PI * 2);
            ctx.fillStyle = 'white';
            ctx.fill();
        }

        // 更新云朵位置的函数
        function updateClouds() {
            clouds.forEach(cloud => {
                // 移动云朵
                cloud.x += cloud.speed;
                // 如果云朵完全移出画布右侧,将其移回左侧
                if (cloud.x > canvas.width + cloud.size * 2) {
                    cloud.x = -cloud.size * 2;
                }
            });
        }

        // 主绘制函数,用于动画循环
        function draw() {
            // 清除整个画布
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            // 绘制彩虹
            drawRainbow();
            // 绘制所有云朵
            clouds.forEach(cloud => drawCloud(cloud.x, cloud.y, cloud.size));
            // 更新云朵位置
            updateClouds();
            // 请求下一帧动画
            requestAnimationFrame(draw);
        }

        // 开始动画循环
        draw();
    </script>
</body>
</html>

其中,requestAnimationFrame 是一个现代浏览器提供的用于优化动画性能的 JavaScript 方法,来创建平滑的动画。它允许您告诉浏览器您希望执行一个动画,并请求浏览器在下一次重绘之前调用指定的函数来更新动画。这个方法的主要目的是为了创建更加流畅和高效的动画。使用方法:

function animate() {

// 更新动画状态

// ...

// 请求下一帧

requestAnimationFrame(animate);

}

// 开始动画循环

animate();

相关推荐
JIngJaneIL5 小时前
助农惠农服务平台|助农服务系统|基于SprinBoot+vue的助农服务系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·助农惠农服务平台
云外天ノ☼5 小时前
待办事项全栈实现:Vue3 + Node.js (Koa) + MySQL深度整合,构建生产级任务管理系统的技术实践
前端·数据库·vue.js·mysql·vue3·koa·jwt认证
gihigo19985 小时前
使用JavaScript和Node.js构建简单的RESTful API
javascript·node.js·restful
一位搞嵌入式的 genius5 小时前
前端实战开发(三):Vue+Pinia中三大核心问题解决方案!!!
前端·javascript·vue.js·前端实战
塞纳河畔的歌5 小时前
保姆级教程 | 麒麟系统安装Edge浏览器
前端·edge
前端.火鸡5 小时前
Vue 3.5 新API解析:响应式革命、SSR黑科技与开发体验飞跃
javascript·vue.js·科技
多睡觉觉5 小时前
数据字典:从"猜谜游戏"到"优雅编程"的奇幻之旅
前端
嗝屁小孩纸5 小时前
开发集成热门小游戏(vue+js)
前端·javascript·vue.js
赛博切图仔5 小时前
深入理解 package.json:前端项目的 “身份证“
前端·javascript
UIUV5 小时前
JavaScript 学习笔记:深入理解 map() 方法与面向对象特性
前端·javascript·代码规范