• 用HTML5实现动画


    用HTML5实现动画

    要在HTML5中实现动画,可以使用以下几种方法:CSS动画、使用元素和JavaScript来实现动画、使用JavaScript动画库。重点介绍前两种。

    一、CSS动画

    CSS3 动画:使用CSS3的动画属性和关键帧(keyframes)来创建动画效果。通过定义动画的开始状态、结束状态和过渡效果,可以实现平滑的动画效果。

    先看一个简单的例子:

    1. <html>
    2. <head>
    3. <meta charset="UTF-8" />
    4. <title>在HTML5中用CSS3实现简单动画title>
    5. <style>
    6. .box {
    7. width: 100px;
    8. height: 100px;
    9. background-color: red;
    10. animation: myAnimation 2s infinite;
    11. }
    12. @keyframes myAnimation {
    13. 0% { transform: translateX(0px); }
    14. 50% { transform: translateX(200px); }
    15. 100% { transform: translateX(0px); }
    16. }
    17. style>
    18. head>
    19. <body>
    20. <div class="box">div>
    21. body>
    22. html>

    我这里命名为:CSS3简单动画.html

    用浏览器打开,运行效果:

    下面给出一个小车动画

    由两部分组成:

    HTML文件和CSS文件,为方便使用,我将这两个文件放在同一文件夹中。

    HTML文件,我这里命名为:CSS3小车动画.html,源码如下:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>在HTML5中用CSS3实现动画title>
    6. <link rel="stylesheet" type="text/css" href="car.css">
    7. head>
    8. <body>
    9. <div id="container">
    10. <div id="car">
    11. <div id="chassis">div>
    12. <div id="backtire" class="tire">
    13. <div class="hr">div>
    14. <div class="vr">div>
    15. div>
    16. <div id="fronttire" class="tire">
    17. <div class="hr">div>
    18. <div class="vr">div>
    19. div>
    20. div>
    21. <div id="grass">div>
    22. div>
    23. body>
    24. html>

    CSS文件,我这里命名为:car.css,源码如下:

    1. /*定义动画:从-400px的位置移动到1600px的位置 */
    2. @keyframes carAnimation {
    3. 0% { left: -400px; } /* 指定初始位置*/
    4. 100% { left: 1600px; } /* 指定最终位置*/
    5. }
    6. #car {
    7. position: absolute;
    8. width: 400px;
    9. height: 210px;
    10. top: 300px;
    11. left: 50px;
    12. animation: carAnimation 10s infinite linear;
    13. }
    14. #chassis {
    15. position: absolute;
    16. width: 400px;
    17. height: 130px;
    18. background: #FF9900;
    19. border: 2px solid #FF6600;
    20. }
    21. .tire {
    22. position: absolute;
    23. bottom: 0;
    24. height: 120px;
    25. width: 120px;
    26. border-radius: 60px;
    27. background: #0099FF;
    28. border: 1px solid #3300FF;
    29. animation: tyreAnimation 10s infinite linear;
    30. }
    31. #fronttire {
    32. right: 20px;
    33. }
    34. #backtire {
    35. left: 20px;
    36. }
    37. @keyframes tyreAnimation {
    38. 0% { transform: rotate(0); }
    39. 100% { transform: rotate(1800deg); }
    40. }
    41. #grass {
    42. position: absolute;
    43. width: 100%;
    44. height: 130px;
    45. bottom: 0;
    46. background: linear-gradient(bottom, #33CC00, #66FF22);
    47. }
    48. .hr {
    49. position: absolute;
    50. background: #3300FF;
    51. height: 2px;
    52. width: 100%;
    53. top: 60px;
    54. }
    55. .vr {
    56. position: absolute;
    57. background: #3300FF;
    58. width: 2px;
    59. height: 100%;
    60. left: 60px;
    61. top: 0;
    62. }

    我这里命名为:CSS3简单动画.html

    用浏览器打开,运行效果:

    二、使用元素和JavaScript来实现动画

    先看一个简单的例子:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>在HTML5中用canvas+JS简单动画title>
    6. head>
    7. <body>
    8. <canvas id="myCanvas" width="400" height="200">canvas>
    9. <script>
    10. var canvas = document.getElementById("myCanvas");
    11. var ctx = canvas.getContext("2d");
    12. var x = 0;
    13. var dx = 2; // 方块的移动速度以及方向
    14. function draw() {
    15. ctx.clearRect(0, 0, canvas.width, canvas.height);
    16. ctx.fillRect(x, 50, 50, 50);
    17. // 更新方块的位置
    18. x += dx;
    19. // 如果方块触碰到画布的右边缘或左边缘,反转方向
    20. if (x + 50 > canvas.width || x < 0) {
    21. dx = -dx;
    22. }
    23. requestAnimationFrame(draw);
    24. }
    25. draw();
    26. script>
    27. body>
    28. html>

    我这里命名为:canvas+JS简单动画.html

    运行效果:

    下面给出一个小车动画

    由两部分组成

    HTML文件和JavaScript文件,为方便使用,我将这两个文件放在同一文件夹中。

    HTML文件,我这里命名为:canvas+JS小车动画.html,源码如下:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>在HTML5中用canvas+JS小车动画title>
    6. <style>
    7. body {
    8. margin: 0;
    9. overflow: hidden;
    10. }
    11. canvas {
    12. display: block;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <canvas id="canvas">canvas>
    18. <script src="car.js">script>
    19. body>
    20. html>

    JavaScript文件,我这里命名为:car.js,源码如下:

    1. const canvas = document.getElementById('canvas');
    2. const ctx = canvas.getContext('2d');
    3. // Set canvas full screen
    4. canvas.width = window.innerWidth;
    5. canvas.height = window.innerHeight-120; //
    6. const car = {
    7. x: 50,
    8. y: canvas.height - 180, //
    9. width: 200,
    10. height: 100,
    11. wheelRadius: 40,
    12. wheelOffset: 25,
    13. wheelRotation: 0
    14. };
    15. function drawCar(x, y, width, height, wheelRadius, wheelOffset, wheelRotation) {
    16. // Draw car body
    17. ctx.fillStyle = 'orange';
    18. ctx.fillRect(x, y, width, height);
    19. // Draw wheels
    20. const wheelPositions = [
    21. { x: x + wheelOffset, y: y + height },
    22. { x: x + width - wheelOffset, y: y + height }
    23. ];
    24. wheelPositions.forEach(wheelPos => {
    25. ctx.save();
    26. ctx.translate(wheelPos.x, wheelPos.y);
    27. ctx.rotate(wheelRotation);
    28. // Draw wheel
    29. ctx.beginPath();
    30. ctx.arc(0, 0, wheelRadius, 0, Math.PI * 2);
    31. ctx.fillStyle = 'blue';
    32. ctx.fill();
    33. // Draw spokes
    34. ctx.beginPath();
    35. ctx.moveTo(-wheelRadius, 0);
    36. ctx.lineTo(wheelRadius, 0);
    37. ctx.moveTo(0, -wheelRadius);
    38. ctx.lineTo(0, wheelRadius);
    39. ctx.strokeStyle = 'white';
    40. ctx.lineWidth = 4;
    41. ctx.stroke();
    42. ctx.restore();
    43. });
    44. }
    45. function animate() {
    46. ctx.clearRect(0, 0, canvas.width, canvas.height);
    47. // Draw ground
    48. ctx.fillStyle = 'green';
    49. ctx.fillRect(0, canvas.height - 50, canvas.width, 50);
    50. // Update wheel rotation
    51. car.wheelRotation += 0.05;
    52. // Draw car
    53. drawCar(car.x, car.y, car.width, car.height, car.wheelRadius, car.wheelOffset, car.wheelRotation);
    54. // Move car
    55. car.x += 2;
    56. if (car.x > canvas.width) {
    57. car.x = -car.width;
    58. }
    59. requestAnimationFrame(animate);
    60. }
    61. animate();

    用浏览器打开,效果如下:

    修改上面源码,将两个文件合二为一,并添加几个控制按钮“暂停/继续”、“快”、“慢”,并实现相关功能:

    点击pauseResumeBtn按钮会切换动画的暂停和继续状态。

    点击speedUpBtn按钮会增加小车的速度。

    点击speedDownBtn按钮会减慢小车的速度,但速度不能小于1。

    源码如下:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>在HTML5中用canvas+JS小车可控动画title>
    6. <style>
    7. body {
    8. margin: 0;
    9. overflow: hidden;
    10. }
    11. canvas {
    12. display: block;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <canvas id="canvas">canvas>
    18. <button id="pauseResumeBtn">暂停/继续button>
    19. <button id="speedUpBtn">button>
    20. <button id="speedDownBtn">button>
    21. <script>
    22. const canvas = document.getElementById('canvas');
    23. const ctx = canvas.getContext('2d');
    24. // Set canvas full screen
    25. canvas.width = window.innerWidth;
    26. canvas.height = window.innerHeight - 120; //
    27. const car = {
    28. x: 50,
    29. y: canvas.height - 180, //
    30. width: 200,
    31. height: 100,
    32. wheelRadius: 40,
    33. wheelOffset: 25,
    34. wheelRotation: 0,
    35. speed: 2
    36. };
    37. let isPaused = false;
    38. function drawCar(x, y, width, height, wheelRadius, wheelOffset, wheelRotation) {
    39. // Draw car body
    40. ctx.fillStyle = 'orange';
    41. ctx.fillRect(x, y, width, height);
    42. // Draw wheels
    43. const wheelPositions = [
    44. { x: x + wheelOffset, y: y + height },
    45. { x: x + width - wheelOffset, y: y + height }
    46. ];
    47. wheelPositions.forEach(wheelPos => {
    48. ctx.save();
    49. ctx.translate(wheelPos.x, wheelPos.y);
    50. ctx.rotate(wheelRotation);
    51. // Draw wheel
    52. ctx.beginPath();
    53. ctx.arc(0, 0, wheelRadius, 0, Math.PI * 2);
    54. ctx.fillStyle = 'blue';
    55. ctx.fill();
    56. // Draw spokes
    57. ctx.beginPath();
    58. ctx.moveTo(-wheelRadius, 0);
    59. ctx.lineTo(wheelRadius, 0);
    60. ctx.moveTo(0, -wheelRadius);
    61. ctx.lineTo(0, wheelRadius);
    62. ctx.strokeStyle = 'white';
    63. ctx.lineWidth = 4;
    64. ctx.stroke();
    65. ctx.restore();
    66. });
    67. }
    68. function animate() {
    69. ctx.clearRect(0, 0, canvas.width, canvas.height);
    70. // Draw ground
    71. ctx.fillStyle = 'green';
    72. ctx.fillRect(0, canvas.height - 50, canvas.width, 50);
    73. // Update wheel rotation
    74. car.wheelRotation += 0.05;
    75. // Draw car
    76. drawCar(car.x, car.y, car.width, car.height, car.wheelRadius, car.wheelOffset, car.wheelRotation);
    77. // Move car
    78. car.x += car.speed;
    79. if (car.x > canvas.width) {
    80. car.x = -car.width;
    81. }
    82. if (!isPaused) {
    83. requestAnimationFrame(animate);
    84. }
    85. }
    86. // Button event listeners
    87. document.getElementById('pauseResumeBtn').addEventListener('click', function() {
    88. isPaused = !isPaused;
    89. if (!isPaused) {
    90. animate();
    91. }
    92. });
    93. document.getElementById('speedUpBtn').addEventListener('click', function() {
    94. car.speed += 1;
    95. });
    96. document.getElementById('speedDownBtn').addEventListener('click', function() {
    97. if (car.speed > 1) {
    98. car.speed -= 1;
    99. }
    100. });
    101. animate();
    102. script>
    103. body>
    104. html>

    我这里保存命名为:canvas+JS小车可控动画.html

    用浏览器打开,效果如下:

    三、使用JavaScript动画库

    使用JavaScript动画库(如Anime.js、Velocity.js、Three.js等)可以更方便地创建复杂的动画效果,我没有深入学习了解,在此就不介绍了。

    附录:

    CSS3动画详解(图文教程) https://www.cnblogs.com/qianguyihao/p/8435182.html

    anime.js 简单入门教程https://www.cnblogs.com/joyco773/p/10734850.html

    Velocity.js 中文文档https://www.cnblogs.com/guandekuan/p/6643988.html

  • 相关阅读:
    安卓案例:利用URLConnection下载音乐
    【Mapbox GL JS 入门】Map 对象及其属性
    BBS项目分布搭建二(个人站点相关)
    市政管理学考试复习资料
    ReactNative进阶(四十三)iOS项目Xcode本地联调
    vue3 ref和reactive使用watch属性的方法和区别
    非零基础自学Java (老师:韩顺平) 第15章 泛型 15.6 自定义泛型
    Oracle 简介与 Docker Compose部署
    现金储备超400亿的小鹏,进入中途蓄力时刻
    解决一次Cannot read properties of null (reading ‘length‘)的问题:
  • 原文地址:https://blog.csdn.net/cnds123/article/details/136118232