当前位置:   article > 正文

用HTML5实现灯笼效果_canvas 画灯笼

canvas 画灯笼

本文介绍了两种实现效果:一种使用画布(canvas)标签/元素,另一种不用画布(canvas)标签/元素主要使用CSS实现。

使用画布(canvas)标签/元素实现,下面,在画布上,用JavaScript画两个红灯笼,并且红灯笼左右来回移动的源码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>红灯笼</title>
  5. <style>
  6. canvas {
  7. border: 1px solid black;
  8. background-color: blue; /*设置画布的背景色为蓝色*/
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <!-- 设置画布宽高是500*300,单位是 px。 -->
  14. <canvas id="canvas" width="500" height="300"></canvas>
  15. <script>
  16. var canvas = document.getElementById('canvas');
  17. var context = canvas.getContext('2d');
  18. var angle1 = 0;
  19. var angle2 = 0; // 使第二个灯笼的初始角度与第一个灯笼不同
  20. function drawLantern(x, y, radius, angle) {
  21. x += Math.sin(angle) * 20;
  22. // Draw the suspension line
  23. context.beginPath();
  24. context.moveTo(x, y - radius / 4);
  25. context.lineTo(x, 0);
  26. context.stroke();
  27. // Draw the top
  28. context.beginPath();
  29. context.arc(x, y, radius / 4, 0, Math.PI * 2);
  30. context.fillStyle = 'gold';
  31. context.fill();
  32. context.stroke();
  33. // Draw the main body
  34. context.beginPath();
  35. context.arc(x, y, radius, 0, Math.PI * 2);
  36. context.fillStyle = 'red';
  37. context.fill();
  38. context.stroke();
  39. // Draw the bottom
  40. context.beginPath();
  41. context.arc(x, y + radius / 2, radius / 4, 0, Math.PI * 2);
  42. context.fillStyle = 'gold';
  43. context.fill();
  44. context.stroke();
  45. // Draw the tassel
  46. context.beginPath();
  47. context.moveTo(x, y + radius / 2);
  48. context.lineTo(x, y + radius * 1.2);
  49. context.moveTo(x - radius / 6, y + radius / 2);
  50. context.lineTo(x - radius / 6, y + radius * 1.2);
  51. context.moveTo(x + radius / 6, y + radius / 2);
  52. context.lineTo(x + radius / 6, y + radius * 1.2);
  53. context.stroke();
  54. }
  55. function animate() {
  56. context.clearRect(0, 0, canvas.width, canvas.height);
  57. context.fillStyle = 'blue';
  58. context.fillRect(0, 0, canvas.width, canvas.height);
  59. drawLantern(200, 200, 50, angle1);
  60. drawLantern(300, 200, 50, angle2);
  61. angle1 += 0.05;
  62. angle2 += 0.05;
  63. requestAnimationFrame(animate);
  64. }
  65. animate();
  66. </script>
  67. </body>
  68. </html>

运行效果如下:

下面是另一种实现主要使用CSS实现,源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>红灯笼效果</title>
  6. <!-- 灯笼代码↓,放在 head标签内-->
  7. <div class="deng-box2">
  8. <div class="deng">
  9. <div class="xian">
  10. </div>
  11. <div class="deng-a">
  12. <div class="deng-b">
  13. <div class="deng-t"></div>
  14. </div>
  15. </div>
  16. <div class="shui shui-a">
  17. <div class="shui-c">
  18. </div>
  19. <div class="shui-b"></div>
  20. </div>
  21. </div>
  22. </div>
  23. <div class="deng-box3">
  24. <div class="deng">
  25. <div class="xian">
  26. </div>
  27. <div class="deng-a">
  28. <div class="deng-b">
  29. <div class="deng-t"></div>
  30. </div>
  31. </div>
  32. <div class="shui shui-a">
  33. <div class="shui-c"></div>
  34. <div class="shui-b">
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. <div class="deng-box1">
  40. <div class="deng">
  41. <div class="xian">
  42. </div>
  43. <div class="deng-a">
  44. <div class="deng-b">
  45. <div class="deng-t"></div>
  46. </div>
  47. </div>
  48. <div class="shui shui-a">
  49. <div class="shui-c"></div>
  50. <div class="shui-b"></div>
  51. </div>
  52. </div>
  53. </div>
  54. <div class="deng-box">
  55. <div class="deng">
  56. <div class="xian">
  57. </div>
  58. <div class="deng-a">
  59. <div class="deng-b">
  60. <div class="deng-t"></div>
  61. </div>
  62. </div>
  63. <div class="shui shui-a">
  64. <div class="shui-c">
  65. </div>
  66. <div class="shui-b"></div>
  67. </div>
  68. </div>
  69. </div>
  70. <style type="text/css">
  71. .deng-box {
  72. position: fixed;
  73. top: -40px;
  74. right: 150px;
  75. z-index: 9999;
  76. pointer-events: none;
  77. }
  78. .deng-box1 {
  79. position: fixed;
  80. top: -30px;
  81. right: 10px;
  82. z-index: 9999;
  83. pointer-events: none
  84. }
  85. .deng-box2 {
  86. position: fixed;
  87. top: -40px;
  88. left: 150px;
  89. z-index: 9999;
  90. pointer-events: none
  91. }
  92. .deng-box3 {
  93. position: fixed;
  94. top: -30px;
  95. left: 10px;
  96. z-index: 9999;
  97. pointer-events: none
  98. }
  99. .deng-box1 .deng,
  100. .deng-box3 .deng {
  101. position: relative;
  102. width: 120px;
  103. height: 90px;
  104. margin: 50px;
  105. background: #d8000f;
  106. background: rgba(216, 0, 15, .8);
  107. border-radius: 50% 50%;
  108. -webkit-transform-origin: 50% -100px;
  109. -webkit-animation: swing 5s infinite ease-in-out;
  110. box-shadow: -5px 5px 30px 4px #fc903d
  111. }
  112. .deng {
  113. position: relative;
  114. width: 120px;
  115. height: 90px;
  116. margin: 50px;
  117. background: #d8000f;
  118. background: rgba(216, 0, 15, .8);
  119. border-radius: 50% 50%;
  120. -webkit-transform-origin: 50% -100px;
  121. -webkit-animation: swing 3s infinite ease-in-out;
  122. box-shadow: -5px 5px 50px 4px #fa6c00
  123. }
  124. .deng-a {
  125. width: 100px;
  126. height: 90px;
  127. background: #d8000f;
  128. background: rgba(216, 0, 15, .1);
  129. margin: 12px 8px 8px 8px;
  130. border-radius: 50% 50%;
  131. border: 2px solid #dc8f03
  132. }
  133. .deng-b {
  134. width: 45px;
  135. height: 90px;
  136. background: #d8000f;
  137. background: rgba(216, 0, 15, .1);
  138. margin: -4px 8px 8px 26px;
  139. border-radius: 50% 50%;
  140. border: 2px solid #dc8f03
  141. }
  142. .xian {
  143. position: absolute;
  144. top: -20px;
  145. left: 60px;
  146. width: 2px;
  147. height: 20px;
  148. background: #dc8f03
  149. }
  150. .shui-a {
  151. position: relative;
  152. width: 5px;
  153. height: 20px;
  154. margin: -5px 0 0 59px;
  155. -webkit-animation: swing 4s infinite ease-in-out;
  156. -webkit-transform-origin: 50% -45px;
  157. background: orange;
  158. border-radius: 0 0 5px 5px
  159. }
  160. .shui-b {
  161. position: absolute;
  162. top: 14px;
  163. left: -2px;
  164. width: 10px;
  165. height: 10px;
  166. background: #dc8f03;
  167. border-radius: 50%
  168. }
  169. .shui-c {
  170. position: absolute;
  171. top: 18px;
  172. left: -2px;
  173. width: 10px;
  174. height: 35px;
  175. background: orange;
  176. border-radius: 0 0 0 5px
  177. }
  178. .deng:before {
  179. position: absolute;
  180. top: -7px;
  181. left: 29px;
  182. height: 12px;
  183. width: 60px;
  184. content: " ";
  185. display: block;
  186. z-index: 999;
  187. border-radius: 5px 5px 0 0;
  188. border: solid 1px #dc8f03;
  189. background: orange;
  190. background: linear-gradient(to right, #dc8f03, orange, #dc8f03, orange, #dc8f03)
  191. }
  192. .deng:after {
  193. position: absolute;
  194. bottom: -7px;
  195. left: 10px;
  196. height: 12px;
  197. width: 60px;
  198. content: " ";
  199. display: block;
  200. margin-left: 20px;
  201. border-radius: 0 0 5px 5px;
  202. border: solid 1px #dc8f03;
  203. background: orange;
  204. background: linear-gradient(to right, #dc8f03, orange, #dc8f03, orange, #dc8f03)
  205. }
  206. .deng-t {
  207. font-family: 黑体, Arial, Lucida Grande, Tahoma, sans-serif;
  208. font-size: 3.2rem;
  209. color: #dc8f03;
  210. font-weight: 700;
  211. line-height: 85px;
  212. text-align: center
  213. }
  214. .night .deng-box,
  215. .night .deng-box1,
  216. .night .deng-t {
  217. background: 0 0 !important
  218. }
  219. @-moz-keyframes swing {
  220. 0% {
  221. -moz-transform: rotate(-10deg)
  222. }
  223. 50% {
  224. -moz-transform: rotate(10deg)
  225. }
  226. 100% {
  227. -moz-transform: rotate(-10deg)
  228. }
  229. }
  230. @-webkit-keyframes swing {
  231. 0% {
  232. -webkit-transform: rotate(-10deg)
  233. }
  234. 50% {
  235. -webkit-transform: rotate(10deg)
  236. }
  237. 100% {
  238. -webkit-transform: rotate(-10deg)
  239. }
  240. }
  241. </style>
  242. <!-- 灯笼代码↑-->
  243. </head>
  244. <body>
  245. </body>
  246. </html>

运行效果如下:

附录

HTML网页添加喜庆气氛的诸多方法https://blog.csdn.net/cnds123/article/details/128865230

用CSS画了个灯笼https://juejin.cn/post/7051370971932033038

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/85292
推荐阅读
相关标签
  

闽ICP备14008679号