• HTML5+JavaScript绘制彩虹和云朵


    HTML5+JavaScript绘制彩虹和云朵

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

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

    源码如下:

    1. html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>彩虹title>
    7. <style>
    8. body {
    9. display: flex;
    10. justify-content: center;
    11. align-items: center;
    12. height: 100vh;
    13. margin: 0;
    14. background-color: #F0FFFF; /* 青白色背景 */
    15. }
    16. canvas {
    17. border: 2px solid #000;
    18. background-color: #87CEEB; /* 天蓝色 */
    19. }
    20. style>
    21. head>
    22. <body>
    23. <canvas id="rainbowCanvas" width="400" height="300">canvas>
    24. <script>
    25. const canvas = document.getElementById('rainbowCanvas');
    26. const ctx = canvas.getContext('2d');
    27. function drawRainbow() {
    28. const centerX = canvas.width / 2;
    29. const centerY = canvas.height;
    30. const radius = canvas.height * 0.8;
    31. // 彩虹颜色
    32. const colors = [
    33. '#FF0000', // 红
    34. '#FF7F00', // 橙
    35. '#FFFF00', // 黄
    36. '#00FF00', // 绿
    37. '#0000FF', // 蓝
    38. '#4B0082', // 靛
    39. '#9400D3' // 紫
    40. ];
    41. // 绘制彩虹
    42. for (let i = colors.length - 1; i >= 0; i--) {
    43. ctx.beginPath();
    44. ctx.arc(centerX, centerY, radius - i * 20, Math.PI, 0, false);
    45. ctx.strokeStyle = colors[i];
    46. ctx.lineWidth = 20;
    47. ctx.stroke();
    48. }
    49. // 绘制云朵
    50. function drawCloud(x, y, size) {
    51. ctx.beginPath();
    52. ctx.arc(x, y, size, 0, Math.PI * 2);
    53. ctx.arc(x + size, y - size / 2, size * 0.8, 0, Math.PI * 2);
    54. ctx.arc(x + size * 2, y, size, 0, Math.PI * 2);
    55. ctx.fillStyle = 'white';
    56. ctx.fill();
    57. }
    58. drawCloud(50, 50, 30);
    59. drawCloud(canvas.width - 100, 80, 25);
    60. }
    61. drawRainbow();
    62. script>
    63. body>
    64. html>

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

    1. html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>彩虹与移动的云朵title>
    7. <style>
    8. body {
    9. display: flex;
    10. justify-content: center;
    11. align-items: center;
    12. height: 100vh;
    13. margin: 0;
    14. background-color: #F0FFFF; /* 青白色背景 */
    15. }
    16. canvas {
    17. border: 2px solid #000;
    18. background-color: #87CEEB; /* 天蓝色 */
    19. }
    20. style>
    21. head>
    22. <body>
    23. <canvas id="rainbowCanvas" width="400" height="300">canvas>
    24. <script>
    25. // 获取canvas元素和2D绘图上下文
    26. const canvas = document.getElementById('rainbowCanvas');
    27. const ctx = canvas.getContext('2d');
    28. // 定义云朵对象数组,每个云朵包含位置、大小和速度信息
    29. const clouds = [
    30. { x: 50, y: 50, size: 30, speed: 0.5 },
    31. { x: canvas.width - 100, y: 80, size: 25, speed: 0.3 }
    32. ];
    33. // 绘制彩虹的函数
    34. function drawRainbow() {
    35. const centerX = canvas.width / 2;
    36. const centerY = canvas.height;
    37. const radius = canvas.height * 0.8;
    38. // 定义彩虹的颜色数组
    39. const colors = [
    40. '#FF0000', // 红
    41. '#FF7F00', // 橙
    42. '#FFFF00', // 黄
    43. '#00FF00', // 绿
    44. '#0000FF', // 蓝
    45. '#4B0082', // 靛
    46. '#9400D3' // 紫
    47. ];
    48. // 从外到内绘制彩虹的每一道颜色
    49. for (let i = colors.length - 1; i >= 0; i--) {
    50. ctx.beginPath();
    51. ctx.arc(centerX, centerY, radius - i * 20, Math.PI, 0, false);
    52. ctx.strokeStyle = colors[i];
    53. ctx.lineWidth = 20;
    54. ctx.stroke();
    55. }
    56. }
    57. // 绘制单个云朵的函数
    58. function drawCloud(x, y, size) {
    59. ctx.beginPath();
    60. // 绘制三个部分组成的云朵形状
    61. ctx.arc(x, y, size, 0, Math.PI * 2);
    62. ctx.arc(x + size, y - size / 2, size * 0.8, 0, Math.PI * 2);
    63. ctx.arc(x + size * 2, y, size, 0, Math.PI * 2);
    64. ctx.fillStyle = 'white';
    65. ctx.fill();
    66. }
    67. // 更新云朵位置的函数
    68. function updateClouds() {
    69. clouds.forEach(cloud => {
    70. // 移动云朵
    71. cloud.x += cloud.speed;
    72. // 如果云朵完全移出画布右侧,将其移回左侧
    73. if (cloud.x > canvas.width + cloud.size * 2) {
    74. cloud.x = -cloud.size * 2;
    75. }
    76. });
    77. }
    78. // 主绘制函数,用于动画循环
    79. function draw() {
    80. // 清除整个画布
    81. ctx.clearRect(0, 0, canvas.width, canvas.height);
    82. // 绘制彩虹
    83. drawRainbow();
    84. // 绘制所有云朵
    85. clouds.forEach(cloud => drawCloud(cloud.x, cloud.y, cloud.size));
    86. // 更新云朵位置
    87. updateClouds();
    88. // 请求下一帧动画
    89. requestAnimationFrame(draw);
    90. }
    91. // 开始动画循环
    92. draw();
    93. script>
    94. body>
    95. html>

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

    function animate() {
        // 更新动画状态
        // ...

        // 请求下一帧
        requestAnimationFrame(animate);
    }

    // 开始动画循环
    animate();

  • 相关阅读:
    安卓RecycleView包含SeekBar点击列表底部圆形阴影处理
    PostgreSQL数据库高级sql总结2
    JAVA8接口使用问题
    Vue 3的革命性新特性:深入了解Composition API
    基于Vue3在线商城(Vue3+VueCLI+VueRouter+vuex+axios+Bootstrap)
    无刷电机FOC硬件方案
    STM32-遥感数据处理
    8.10 - 软件运维
    MegLab 新能力“老番动漫超画质”上线,支持渣画质一键焕新
    56、springboot ------ RESTful服务及RESTful接口设计
  • 原文地址:https://blog.csdn.net/cnds123/article/details/141086789